@@ -120,29 +120,102 @@ def jscFlavor = 'org.webkit:android-jsc:+'
120120 */  
121121def  enableHermes =  project. ext. react. get(" enableHermes"  , false );
122122
123+ /** 
124+  * Architectures to build native code for. 
125+  */  
126+ def  reactNativeArchitectures () {
127+     def  value =  project. getProperties(). get(" reactNativeArchitectures"  )
128+     return  value ?  value. split(" ,"  ) :  [" armeabi-v7a"  , " x86"  , " x86_64"  , " arm64-v8a"  ]
129+ }
130+ 
123131android  {
124132    ndkVersion rootProject. ext. ndkVersion
125133
126134    compileSdkVersion rootProject. ext. compileSdkVersion
127135
128-     compileOptions {
129-         sourceCompatibility JavaVersion . VERSION_1_8 
130-         targetCompatibility JavaVersion . VERSION_1_8 
131-     }
132- 
133136    defaultConfig {
134137        applicationId " io.getstream.reactnative.imessageclone" 
135138        minSdkVersion rootProject. ext. minSdkVersion
136139        targetSdkVersion rootProject. ext. targetSdkVersion
137140        versionCode 1 
138141        versionName " 1.0" 
142+ 
143+         buildConfigField " boolean"  , " IS_NEW_ARCHITECTURE_ENABLED"  , isNewArchitectureEnabled(). toString()
144+ 
145+         if  (isNewArchitectureEnabled()) {
146+             //  We configure the NDK build only if you decide to opt-in for the New Architecture.
147+             externalNativeBuild {
148+                 ndkBuild {
149+                     arguments " APP_PLATFORM=android-21"  ,
150+                         " APP_STL=c++_shared"  ,
151+                         " NDK_TOOLCHAIN_VERSION=clang"  ,
152+                         " GENERATED_SRC_DIR=$buildDir  /generated/source"  ,
153+                         " PROJECT_BUILD_DIR=$buildDir  "  ,
154+                         " REACT_ANDROID_DIR=$rootDir  /../node_modules/react-native/ReactAndroid"  ,
155+                         " REACT_ANDROID_BUILD_DIR=$rootDir  /../node_modules/react-native/ReactAndroid/build"  ,
156+                         " NODE_MODULES_DIR=$rootDir  /../node_modules" 
157+                     cFlags " -Wall"  , " -Werror"  , " -fexceptions"  , " -frtti"  , " -DWITH_INSPECTOR=1" 
158+                     cppFlags " -std=c++17" 
159+                     //  Make sure this target name is the same you specify inside the
160+                     //  src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
161+                     targets " imessageclone_appmodules" 
162+                 }
163+             }
164+             if  (! enableSeparateBuildPerCPUArchitecture) {
165+                 ndk {
166+                     abiFilters (* reactNativeArchitectures())
167+                 }
168+             }
169+         }
139170    }
171+ 
172+     if  (isNewArchitectureEnabled()) {
173+         //  We configure the NDK build only if you decide to opt-in for the New Architecture.
174+         externalNativeBuild {
175+             ndkBuild {
176+                 path " $projectDir  /src/main/jni/Android.mk" 
177+             }
178+         }
179+         def  reactAndroidProjectDir =  project(' :ReactAndroid'  ). projectDir
180+         def  packageReactNdkDebugLibs =  tasks. register(" packageReactNdkDebugLibs"  , Copy ) {
181+             dependsOn(" :ReactAndroid:packageReactNdkDebugLibsForBuck"  )
182+             from(" $reactAndroidProjectDir  /src/main/jni/prebuilt/lib"  )
183+             into(" $buildDir  /react-ndk/exported"  )
184+         }
185+         def  packageReactNdkReleaseLibs =  tasks. register(" packageReactNdkReleaseLibs"  , Copy ) {
186+             dependsOn(" :ReactAndroid:packageReactNdkReleaseLibsForBuck"  )
187+             from(" $reactAndroidProjectDir  /src/main/jni/prebuilt/lib"  )
188+             into(" $buildDir  /react-ndk/exported"  )
189+         }
190+         afterEvaluate {
191+             //  If you wish to add a custom TurboModule or component locally,
192+             //  you should uncomment this line.
193+             //  preBuild.dependsOn("generateCodegenArtifactsFromSchema")
194+             preDebugBuild. dependsOn(packageReactNdkDebugLibs)
195+             preReleaseBuild. dependsOn(packageReactNdkReleaseLibs)
196+ 
197+             //  Due to a bug inside AGP, we have to explicitly set a dependency
198+             //  between configureNdkBuild* tasks and the preBuild tasks.
199+             //  This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
200+             configureNdkBuildRelease. dependsOn(preReleaseBuild)
201+             configureNdkBuildDebug. dependsOn(preDebugBuild)
202+             reactNativeArchitectures(). each { architecture  -> 
203+                 tasks. findByName(" configureNdkBuildDebug[${ architecture}  ]"  )?. configure {
204+                     dependsOn(" preDebugBuild"  )
205+                 }
206+                 tasks. findByName(" configureNdkBuildRelease[${ architecture}  ]"  )?. configure {
207+                     dependsOn(" preReleaseBuild"  )
208+                 }
209+             }
210+         }
211+     }
212+ 
140213    splits {
141214        abi {
142215            reset()
143216            enable enableSeparateBuildPerCPUArchitecture
144217            universalApk false   //  If true, also generate a universal APK
145-             include " armeabi-v7a " ,  " x86 " ,  " arm64-v8a " ,  " x86_64 " 
218+             include ( * reactNativeArchitectures()) 
146219        }
147220    }
148221    signingConfigs {
@@ -155,32 +228,32 @@ android {
155228    }
156229    buildTypes {
157230        debug {
158-              signingConfig signingConfigs. debug
159-          }
160-          release {
161-              //  Caution! In production, you need to generate your own keystore file.
162-              //  see https://reactnative.dev/docs/signed-apk-android.
163-              signingConfig signingConfigs. debug
164-              minifyEnabled enableProguardInReleaseBuilds
165-              proguardFiles getDefaultProguardFile(" proguard-android.txt"  ), " proguard-rules.pro" 
166-          }
231+            signingConfig signingConfigs. debug
232+        }
233+        release {
234+            //  Caution! In production, you need to generate your own keystore file.
235+            //  see https://reactnative.dev/docs/signed-apk-android.
236+            signingConfig signingConfigs. debug
237+            minifyEnabled enableProguardInReleaseBuilds
238+            proguardFiles getDefaultProguardFile(" proguard-android.txt"  ), " proguard-rules.pro" 
239+        }
167240    }
168241
169-      //  applicationVariants are e.g. debug, release
170-      applicationVariants. all { variant  -> 
171-          variant. outputs. each { output  -> 
172-              //  For each separate APK per architecture, set a unique version code as described here:
173-              //  https://developer.android.com/studio/build/configure-apk-splits.html
174-              //  Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
175-              def  versionCodes =  [" armeabi-v7a"  : 1 , " x86"  : 2 , " arm64-v8a"  : 3 , " x86_64"  : 4 ]
176-              def  abi =  output. getFilter(OutputFile . ABI )
177-              if  (abi !=  null ) {  //  null for the universal-debug, universal-release variants
178-                  output. versionCodeOverride = 
179-                          defaultConfig. versionCode *  1000  +  versionCodes. get(abi)
180-              }
242+    //  applicationVariants are e.g. debug, release
243+    applicationVariants. all { variant  -> 
244+        variant. outputs. each { output  -> 
245+            //  For each separate APK per architecture, set a unique version code as described here:
246+            //  https://developer.android.com/studio/build/configure-apk-splits.html
247+            //  Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
248+            def  versionCodes =  [" armeabi-v7a"  : 1 , " x86"  : 2 , " arm64-v8a"  : 3 , " x86_64"  : 4 ]
249+            def  abi =  output. getFilter(OutputFile . ABI )
250+            if  (abi !=  null ) {  //  null for the universal-debug, universal-release variants
251+                output. versionCodeOverride = 
252+                        defaultConfig. versionCode *  1000  +  versionCodes. get(abi)
253+            }
181254
182-          }
183-      }
255+        }
256+    }
184257}
185258
186259dependencies  {
@@ -204,19 +277,44 @@ dependencies {
204277    }
205278
206279    if  (enableHermes) {
207-         def  hermesPath =  " ../../node_modules/hermes-engine/android/"  ;
208-         debugImplementation files(hermesPath +  " hermes-debug.aar"  )
209-         releaseImplementation files(hermesPath +  " hermes-release.aar"  )
280+         // noinspection GradleDynamicVersion
281+         implementation(" com.facebook.react:hermes-engine:+"  ) { //  From node_modules
282+             exclude group : ' com.facebook.fbjni' 
283+         }
210284    } else  {
211285        implementation jscFlavor
212286    }
213287}
214288
289+ if  (isNewArchitectureEnabled()) {
290+     //  If new architecture is enabled, we let you build RN from source
291+     //  Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
292+     //  This will be applied to all the imported transtitive dependency.
293+     configurations. all {
294+         resolutionStrategy. dependencySubstitution {
295+             substitute(module(" com.facebook.react:react-native"  ))
296+                     .using(project(" :ReactAndroid"  ))
297+                     .because(" On New Architecture we're building React Native from source"  )
298+             substitute(module(" com.facebook.react:hermes-engine"  ))
299+                     .using(project(" :ReactAndroid:hermes-engine"  ))
300+                     .because(" On New Architecture we're building Hermes from source"  )
301+         }
302+     }
303+ }
304+ 
215305//  Run this once to be able to run the application with BUCK
216306//  puts all compile dependencies into folder libs for BUCK to use
217307task copyDownloadableDepsToLibs (type : Copy ) {
218-     from configurations. compile 
308+     from configurations. implementation 
219309    into ' libs' 
220310}
221311
222312apply from : file(" ../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"  ); applyNativeModulesAppBuildGradle(project)
313+ 
314+ def  isNewArchitectureEnabled () {
315+     //  To opt-in for the New Architecture, you can either:
316+     //  - Set `newArchEnabled` to true inside the `gradle.properties` file
317+     //  - Invoke gradle with `-newArchEnabled=true`
318+     //  - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
319+     return  project. hasProperty(" newArchEnabled"  ) &&  project. newArchEnabled ==  " true" 
320+ }
0 commit comments