Skip to content

Commit 14b344d

Browse files
author
Marek Fořt
committed
Run Android fixture app with Fabric
1 parent c70861b commit 14b344d

13 files changed

Lines changed: 207 additions & 56 deletions

android/build.gradle

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ apply plugin: 'com.android.library'
22

33
apply plugin: 'kotlin-android'
44

5+
def isNewArchitectureEnabled() {
6+
// To opt-in for the New Architecture, you can either:
7+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
8+
// - Invoke gradle with `-newArchEnabled=true`
9+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
10+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
11+
}
12+
13+
if (isNewArchitectureEnabled()) {
14+
apply plugin: 'com.facebook.react'
15+
}
16+
517
def _ext = rootProject.ext
618

719
def _reactNativeVersion = _ext.has('reactNative') ? _ext.reactNative : '+'
@@ -28,6 +40,15 @@ android {
2840
debug.java.srcDirs += 'src/debug/kotlin'
2941
test.java.srcDirs += 'src/test/kotlin'
3042
androidTest.java.srcDirs += 'src/androidTest/kotlin'
43+
44+
if (isNewArchitectureEnabled()) {
45+
main.java.srcDirs += 'src/fabric/java'
46+
} else {
47+
// this folder also includes files from codegen so the library can compile with
48+
// codegen turned off
49+
// TODO: This won't work for now!!!!
50+
main.java.srcDirs += 'src/paper/java'
51+
}
3152
}
3253

3354
defaultConfig {
@@ -36,8 +57,41 @@ android {
3657
versionCode 1
3758
versionName "1.0"
3859
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
60+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
61+
if (isNewArchitectureEnabled()) {
62+
var appProject = rootProject.allprojects.find {it.plugins.hasPlugin('com.android.application')}
63+
externalNativeBuild {
64+
ndkBuild {
65+
arguments "APP_PLATFORM=android-21",
66+
"APP_STL=c++_shared",
67+
"NDK_TOOLCHAIN_VERSION=clang",
68+
"GENERATED_SRC_DIR=${appProject.buildDir}/generated/source",
69+
"PROJECT_BUILD_DIR=${appProject.buildDir}",
70+
"REACT_ANDROID_DIR=${appProject.rootDir}/../node_modules/react-native/ReactAndroid",
71+
"REACT_ANDROID_BUILD_DIR=${appProject.rootDir}/../node_modules/react-native/ReactAndroid/build"
72+
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
73+
cppFlags "-std=c++17"
74+
targets "rnflashlist_modules"
75+
}
76+
}
77+
}
3978
}
4079

80+
if (isNewArchitectureEnabled()) {
81+
externalNativeBuild {
82+
ndkBuild {
83+
path "src/main/jni/Android.mk"
84+
}
85+
}
86+
}
87+
88+
packagingOptions {
89+
// For some reason gradle only complains about the duplicated version of libreact_render libraries
90+
// while there are more libraries copied in intermediates folder of the lib build directory, we exclude
91+
// only the ones that make the build fail (ideally we should only include librngesturehandler_modules but we
92+
// are only allowed to specify exclude patterns)
93+
exclude "**/libreact_render*.so"
94+
4195
lintOptions {
4296
abortOnError false
4397
}
@@ -57,3 +111,7 @@ dependencies {
57111
androidTestImplementation("androidx.test:runner:${_androidTestRunnerVersion}")
58112
androidTestImplementation("androidx.test:rules:${_androidTestRunnerVersion}")
59113
}
114+
115+
react {
116+
jsRootDir = rootProject.file("../src/fabric")
117+
}

fixture/android/app/build.gradle

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ def enableHermes = project.ext.react.get("enableHermes", false);
126126
*/
127127
def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures")
128128

129+
/**
130+
* Architectures to build native code for.
131+
*/
132+
def reactNativeArchitectures() {
133+
def value = project.getProperties().get("reactNativeArchitectures")
134+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
135+
}
136+
129137
android {
130138
testOptions {
131139
unitTests.all {
@@ -144,7 +152,77 @@ android {
144152
versionName "1.0"
145153
testBuildType System.getProperty('testBuildType', 'debug')
146154
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
155+
156+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
157+
158+
if (isNewArchitectureEnabled()) {
159+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
160+
externalNativeBuild {
161+
ndkBuild {
162+
arguments "APP_PLATFORM=android-21",
163+
"APP_STL=c++_shared",
164+
"NDK_TOOLCHAIN_VERSION=clang",
165+
"GENERATED_SRC_DIR=$buildDir/generated/source",
166+
"PROJECT_BUILD_DIR=$buildDir",
167+
"REACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
168+
"REACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
169+
"NODE_MODULES_DIR=$rootDir/../node_modules"
170+
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
171+
cppFlags "-std=c++17"
172+
// Make sure this target name is the same you specify inside the
173+
// src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
174+
targets "flatlistpro_appmodules"
175+
}
176+
}
177+
if (!enableSeparateBuildPerCPUArchitecture) {
178+
ndk {
179+
abiFilters (*reactNativeArchitectures())
180+
}
181+
}
182+
}
183+
}
184+
185+
if (isNewArchitectureEnabled()) {
186+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
187+
externalNativeBuild {
188+
ndkBuild {
189+
path "$projectDir/src/main/jni/Android.mk"
190+
}
191+
}
192+
def reactAndroidProjectDir = project(':ReactAndroid').projectDir
193+
def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
194+
dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
195+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
196+
into("$buildDir/react-ndk/exported")
197+
}
198+
def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
199+
dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
200+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
201+
into("$buildDir/react-ndk/exported")
202+
}
203+
afterEvaluate {
204+
// If you wish to add a custom TurboModule or component locally,
205+
// you should uncomment this line.
206+
// preBuild.dependsOn("generateCodegenArtifactsFromSchema")
207+
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
208+
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
209+
210+
// Due to a bug inside AGP, we have to explicitly set a dependency
211+
// between configureNdkBuild* tasks and the preBuild tasks.
212+
// This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
213+
configureNdkBuildRelease.dependsOn(preReleaseBuild)
214+
configureNdkBuildDebug.dependsOn(preDebugBuild)
215+
reactNativeArchitectures().each { architecture ->
216+
tasks.findByName("configureNdkBuildDebug[${architecture}]")?.configure {
217+
dependsOn("preDebugBuild")
218+
}
219+
tasks.findByName("configureNdkBuildRelease[${architecture}]")?.configure {
220+
dependsOn("preReleaseBuild")
221+
}
222+
}
223+
}
147224
}
225+
148226
splits {
149227
abi {
150228
reset()
@@ -238,3 +316,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
238316
}
239317

240318
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
319+
320+
def isNewArchitectureEnabled() {
321+
// To opt-in for the New Architecture, you can either:
322+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
323+
// - Invoke gradle with `-newArchEnabled=true`
324+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
325+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
326+
}

fixture/android/app/src/main/java/com/flatlistpro/MainApplication.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.List;
1515

1616
import com.facebook.react.bridge.JSIModulePackage;
17-
import com.swmansion.reanimated.ReanimatedJSIModulePackage;
17+
//import com.swmansion.reanimated.ReanimatedJSIModulePackage;
1818

1919
public class MainApplication extends Application implements ReactApplication {
2020

@@ -39,10 +39,10 @@ protected String getJSMainModuleName() {
3939
return "index";
4040
}
4141

42-
@Override
43-
protected JSIModulePackage getJSIModulePackage() {
44-
return new ReanimatedJSIModulePackage(); // <- add
45-
}
42+
// @Override
43+
// protected JSIModulePackage getJSIModulePackage() {
44+
// return new ReanimatedJSIModulePackage(); // <- add
45+
// }
4646
};
4747

4848

fixture/android/app/src/main/java/com/flatlistpro/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected synchronized void maybeLoadOtherSoLibraries() {
3333
if (!sIsSoLibraryLoaded) {
3434
// If you change the name of your application .so file in the Android.mk file,
3535
// make sure you update the name here as well.
36-
SoLoader.loadLibrary("rndiffapp_appmodules");
36+
SoLoader.loadLibrary("flatlistpro_appmodules");
3737
sIsSoLibraryLoaded = true;
3838
}
3939
}

fixture/android/app/src/main/jni/Android.mk

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
THIS_DIR := $(call my-dir)
2+
23
include $(REACT_ANDROID_DIR)/Android-prebuilt.mk
4+
35
# If you wish to add a custom TurboModule or Fabric component in your app you
46
# will have to include the following autogenerated makefile.
57
# include $(GENERATED_SRC_DIR)/codegen/jni/Android.mk
68
include $(CLEAR_VARS)
9+
710
LOCAL_PATH := $(THIS_DIR)
11+
812
# You can customize the name of your application .so file here.
913
LOCAL_MODULE := flatlistpro_appmodules
14+
1015
LOCAL_C_INCLUDES := $(LOCAL_PATH)
1116
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
1217
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
18+
1319
# If you wish to add a custom TurboModule or Fabric component in your app you
14-
# will have to uncomment those lines to include the generated source
20+
# will have to uncomment those lines to include the generated source
1521
# files from the codegen (placed in $(GENERATED_SRC_DIR)/codegen/jni)
1622
#
1723
# LOCAL_C_INCLUDES += $(GENERATED_SRC_DIR)/codegen/jni
1824
# LOCAL_SRC_FILES += $(wildcard $(GENERATED_SRC_DIR)/codegen/jni/*.cpp)
1925
# LOCAL_EXPORT_C_INCLUDES += $(GENERATED_SRC_DIR)/codegen/jni
26+
2027
# Here you should add any native library you wish to depend on.
2128
LOCAL_SHARED_LIBRARIES := \
2229
libfabricjni \
@@ -36,5 +43,7 @@ LOCAL_SHARED_LIBRARIES := \
3643
libruntimeexecutor \
3744
libturbomodulejsijni \
3845
libyoga
46+
3947
LOCAL_CFLAGS := -DLOG_TAG=\"ReactNative\" -fexceptions -frtti -std=c++17 -Wall
40-
include $(BUILD_SHARED_LIBRARY)
48+
49+
include $(BUILD_SHARED_LIBRARY)

fixture/android/app/src/main/jni/MainApplicationTurboModuleManagerDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MainApplicationTurboModuleManagerDelegate
1111
public:
1212
// Adapt it to the package you used for your Java class.
1313
static constexpr auto kJavaDescriptor =
14-
"Lcom/rndiffapp/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate;";
14+
"Lcom/flatlistpro/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate;";
1515
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject>);
1616
static void registerNatives();
1717
std::shared_ptr<TurboModule> getTurboModule(

fixture/android/app/src/main/jni/MainComponentRegistry.cpp renamed to fixture/android/app/src/main/jni/MainComponentsRegistry.cpp

File renamed without changes.

fixture/android/app/src/main/jni/MainComponentRegistry.h renamed to fixture/android/app/src/main/jni/MainComponentsRegistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MainComponentsRegistry
1010
public:
1111
// Adapt it to the package you used for your Java class.
1212
constexpr static auto kJavaDescriptor =
13-
"Lcom/rndiffapp/newarchitecture/components/MainComponentsRegistry;";
13+
"Lcom/flatlistpro/newarchitecture/components/MainComponentsRegistry;";
1414
static void registerNatives();
1515
MainComponentsRegistry(ComponentFactory *delegate);
1616
private:

fixture/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
minSdkVersion = 21
77
compileSdkVersion = 31
88
targetSdkVersion = 31
9-
kotlin_version = "1.4.10"
9+
kotlin_version = "1.6.10"
1010

1111
if (System.properties['os.arch'] == "aarch64") {
1212
// For M1 Users we need to use the NDK 24 which added support for aarch64

fixture/android/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
3636
# your application. You should enable this flag either if you want
3737
# to write custom TurboModules/Fabric components OR use libraries that
3838
# are providing them.
39-
newArchEnabled=false
39+
newArchEnabled=true

0 commit comments

Comments
 (0)