Skip to content

Commit da5335e

Browse files
Sunbreaksatya164
authored andcommitted
feat: backport android of new arch project
1 parent 8795a94 commit da5335e

File tree

1 file changed

+61
-1
lines changed
  • packages/create-react-native-library/templates/native-common/android

1 file changed

+61
-1
lines changed

packages/create-react-native-library/templates/native-common/android/build.gradle

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ apply plugin: "com.android.library"
2727
apply plugin: "kotlin-android"
2828
<% } -%>
2929

30+
<% if (project.view && (project.arch === "new" || project.arch === "mixed")) { -%>
31+
import groovy.json.JsonSlurper
32+
33+
// https://github.com/callstack/react-native-builder-bob/discussions/359
34+
def registrationCompat = {
35+
def reactNativeManifest = file("$projectDir/../node_modules/react-native/package.json").exists()
36+
? file("$projectDir/../node_modules/react-native/package.json") // developer mode, to run example app
37+
: file("$projectDir/../../react-native/package.json")
38+
def reactNativeVersion = new JsonSlurper().parseText(reactNativeManifest.text).version as String
39+
// Fabric was introduced at [email protected], full CMake support were introduced at [email protected]
40+
// Use Android.mk for compatibility with [email protected]/0.69
41+
reactNativeVersion.matches('(0.68.*|0.69.*)')
42+
}()
43+
44+
def codegenViewLibraryName = "<%- project.name + 'View' -%>"
45+
def codegenViewModuleName = {
46+
// Autolink for Fabric uses codegenConfig.name in package.json since [email protected]
47+
// Use codegenViewLibraryName for compatibility with [email protected]/0.69
48+
def libraryManifestJson = new JsonSlurper().parseText(file("$projectDir/../package.json").text)
49+
registrationCompat ? codegenViewLibraryName : libraryManifestJson.codegenConfig.name
50+
}()
51+
<% } -%>
52+
53+
def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
54+
3055
if (isNewArchitectureEnabled()) {
3156
apply plugin: "com.facebook.react"
3257
}
@@ -40,7 +65,7 @@ def getExtOrIntegerDefault(name) {
4065
}
4166

4267
android {
43-
<% if (project.cpp) { -%>
68+
<% if (project.cpp || (project.view && (project.arch === "new" || project.arch === "mixed"))) { -%>
4469
ndkVersion getExtOrDefault("ndkVersion")
4570
<% } -%>
4671
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
@@ -49,13 +74,35 @@ android {
4974
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
5075
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
5176
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
77+
<% if (project.view && (project.arch === "new" || project.arch === "mixed")) { -%>
78+
buildConfigField "String", "CODEGEN_MODULE_REGISTRATION", (isNewArchitectureEnabled() && registrationCompat ? "\"${codegenViewModuleName}_registration\"" : "null")
79+
<% } -%>
5280
<% if (project.cpp) { -%>
5381
externalNativeBuild {
5482
cmake {
5583
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
5684
abiFilters "x86", "x86_64", "armeabi-v7a", "arm64-v8a"
5785
}
5886
}
87+
<% } else if (project.view && (project.arch === "new" || project.arch === "mixed")) { -%>
88+
if (isNewArchitectureEnabled() && registrationCompat) {
89+
def reactAndroidProject = project(':ReactAndroid')
90+
externalNativeBuild {
91+
ndkBuild {
92+
arguments "APP_PLATFORM=android-21",
93+
"APP_STL=c++_shared",
94+
"NDK_TOOLCHAIN_VERSION=clang",
95+
"GENERATED_SRC_DIR=$buildDir/generated/source", // for react_codegen_* in this library's codegen/jni
96+
"PROJECT_BUILD_DIR=${appProject.buildDir}", // for REACT_NDK_EXPORT_DIR in ReactAndroid's Android-prebuilt.mk
97+
"REACT_ANDROID_DIR=${reactAndroidProject.projectDir}",
98+
"REACT_ANDROID_BUILD_DIR=${reactAndroidProject.buildDir}",
99+
"CODEGEN_MODULE_NAME=$codegenViewModuleName"
100+
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
101+
cppFlags "-std=c++17"
102+
targets "${codegenViewModuleName}_registration"
103+
}
104+
}
105+
}
59106
<% } -%>
60107
}
61108
<% if (project.cpp) { -%>
@@ -64,6 +111,15 @@ android {
64111
path "CMakeLists.txt"
65112
}
66113
}
114+
<% } else if (project.view && (project.arch === "new" || project.arch === "mixed")) { -%>
115+
if (isNewArchitectureEnabled() && registrationCompat) {
116+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
117+
externalNativeBuild {
118+
ndkBuild {
119+
path "Android.mk"
120+
}
121+
}
122+
}
67123
<% } -%>
68124
buildTypes {
69125
release {
@@ -130,7 +186,11 @@ dependencies {
130186
if (isNewArchitectureEnabled()) {
131187
react {
132188
jsRootDir = file("../src/")
189+
<% if (project.view && (project.arch === "new" || project.arch === "mixed")) { %>
190+
libraryName = codegenViewLibraryName
191+
<% } else { -%>
133192
libraryName = "<%- project.view ? project.name + 'View' : project.name -%>"
193+
<% } -%>
134194
codegenJavaPackageName = "com.<%- project.package -%>"
135195
}
136196
}

0 commit comments

Comments
 (0)