Skip to content

Commit 1c09662

Browse files
authored
chore(deps):Upgrade 0.66.1 到 0.69.1 的 android 环境 (#218)
1 parent 062f16a commit 1c09662

File tree

7 files changed

+112
-14
lines changed

7 files changed

+112
-14
lines changed

template/template/android/app/build.gradle

+105-12
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,19 @@ def jscFlavor = 'org.webkit:android-jsc:+'
114114
/**
115115
* Whether to enable the Hermes VM.
116116
*
117-
* This should be set on project.ext.react and mirrored here. If it is not set
117+
* This should be set on project.ext.react and that value will be read here. If it is not set
118118
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
119119
* and the benefits of using Hermes will therefore be sharply reduced.
120120
*/
121121
def enableHermes = project.ext.react.get("enableHermes", false);
122122

123123
/**
124-
* Architectures to build native code for in debug.
124+
* Architectures to build native code for.
125125
*/
126-
def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures")
126+
def reactNativeArchitectures() {
127+
def value = project.getProperties().get("reactNativeArchitectures")
128+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
129+
}
127130

128131
android {
129132
ndkVersion rootProject.ext.ndkVersion
@@ -136,13 +139,82 @@ android {
136139
targetSdkVersion rootProject.ext.targetSdkVersion
137140
versionCode 1
138141
versionName "1.0"
142+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
143+
144+
if (isNewArchitectureEnabled()) {
145+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
146+
externalNativeBuild {
147+
ndkBuild {
148+
arguments "APP_PLATFORM=android-21",
149+
"APP_STL=c++_shared",
150+
"NDK_TOOLCHAIN_VERSION=clang",
151+
"GENERATED_SRC_DIR=$buildDir/generated/source",
152+
"PROJECT_BUILD_DIR=$buildDir",
153+
"REACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
154+
"REACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
155+
"NODE_MODULES_DIR=$rootDir/../node_modules"
156+
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
157+
cppFlags "-std=c++17"
158+
// Make sure this target name is the same you specify inside the
159+
// src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
160+
targets "helloworld_appmodules"
161+
}
162+
}
163+
if (!enableSeparateBuildPerCPUArchitecture) {
164+
ndk {
165+
abiFilters (*reactNativeArchitectures())
166+
}
167+
}
168+
}
139169
}
170+
171+
if (isNewArchitectureEnabled()) {
172+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
173+
externalNativeBuild {
174+
ndkBuild {
175+
path "$projectDir/src/main/jni/Android.mk"
176+
}
177+
}
178+
def reactAndroidProjectDir = project(':ReactAndroid').projectDir
179+
def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
180+
dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
181+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
182+
into("$buildDir/react-ndk/exported")
183+
}
184+
def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
185+
dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
186+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
187+
into("$buildDir/react-ndk/exported")
188+
}
189+
afterEvaluate {
190+
// If you wish to add a custom TurboModule or component locally,
191+
// you should uncomment this line.
192+
// preBuild.dependsOn("generateCodegenArtifactsFromSchema")
193+
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
194+
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
195+
196+
// Due to a bug inside AGP, we have to explicitly set a dependency
197+
// between configureNdkBuild* tasks and the preBuild tasks.
198+
// This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
199+
configureNdkBuildRelease.dependsOn(preReleaseBuild)
200+
configureNdkBuildDebug.dependsOn(preDebugBuild)
201+
reactNativeArchitectures().each { architecture ->
202+
tasks.findByName("configureNdkBuildDebug[${architecture}]")?.configure {
203+
dependsOn("preDebugBuild")
204+
}
205+
tasks.findByName("configureNdkBuildRelease[${architecture}]")?.configure {
206+
dependsOn("preReleaseBuild")
207+
}
208+
}
209+
}
210+
}
211+
140212
splits {
141213
abi {
142214
reset()
143215
enable enableSeparateBuildPerCPUArchitecture
144216
universalApk false // If true, also generate a universal APK
145-
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
217+
include (*reactNativeArchitectures())
146218
}
147219
}
148220
signingConfigs {
@@ -156,11 +228,6 @@ android {
156228
buildTypes {
157229
debug {
158230
signingConfig signingConfigs.debug
159-
if (nativeArchitectures) {
160-
ndk {
161-
abiFilters nativeArchitectures.split(',')
162-
}
163-
}
164231
}
165232
release {
166233
// Caution! In production, you need to generate your own keystore file.
@@ -190,6 +257,7 @@ android {
190257

191258
dependencies {
192259
implementation fileTree(dir: "libs", include: ["*.jar"])
260+
193261
//noinspection GradleDynamicVersion
194262
implementation "com.facebook.react:react-native:+" // From node_modules
195263

@@ -209,14 +277,31 @@ dependencies {
209277
}
210278

211279
if (enableHermes) {
212-
def hermesPath = "../../node_modules/hermes-engine/android/";
213-
debugImplementation files(hermesPath + "hermes-debug.aar")
214-
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+
}
215284
} else {
216285
implementation jscFlavor
217286
}
218287
}
219288

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+
220305
// Run this once to be able to run the application with BUCK
221306
// puts all compile dependencies into folder libs for BUCK to use
222307
task copyDownloadableDepsToLibs(type: Copy) {
@@ -225,3 +310,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
225310
}
226311

227312
apply 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+
}

template/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java renamed to template/template/android/app/src/debug/java/com/awesomeproject/ReactNativeFlipper.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* directory of this source tree.
66
*/
77
package com.helloworld;
8+
89
import android.content.Context;
910
import com.facebook.flipper.android.AndroidFlipperClient;
1011
import com.facebook.flipper.android.utils.FlipperUtils;
@@ -23,15 +24,18 @@
2324
import com.facebook.react.bridge.ReactContext;
2425
import com.facebook.react.modules.network.NetworkingModule;
2526
import okhttp3.OkHttpClient;
27+
2628
public class ReactNativeFlipper {
2729
public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
2830
if (FlipperUtils.shouldEnableFlipper(context)) {
2931
final FlipperClient client = AndroidFlipperClient.getInstance(context);
32+
3033
client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()));
3134
client.addPlugin(new ReactFlipperPlugin());
3235
client.addPlugin(new DatabasesFlipperPlugin(context));
3336
client.addPlugin(new SharedPreferencesFlipperPlugin(context));
3437
client.addPlugin(CrashReporterPlugin.getInstance());
38+
3539
NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
3640
NetworkingModule.setCustomClientBuilder(
3741
new NetworkingModule.CustomClientBuilder() {
@@ -42,6 +46,7 @@ public void apply(OkHttpClient.Builder builder) {
4246
});
4347
client.addPlugin(networkFlipperPlugin);
4448
client.start();
49+
4550
// Fresco Plugin needs to ensure that ImagePipelineFactory is initialized
4651
// Hence we run if after all native modules have been initialized
4752
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
@@ -65,4 +70,4 @@ public void run() {
6570
}
6671
}
6772
}
68-
}
73+
}

template/template/android/app/src/main/java/com/helloworld/MainActivity.java renamed to template/template/android/app/src/main/java/com/awesomeproject/MainActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MainActivity extends ReactActivity {
1212
*/
1313
@Override
1414
protected String getMainComponentName() {
15-
return "HelloWorld";
15+
return "helloworld";
1616
}
1717

1818
/**

0 commit comments

Comments
 (0)