Skip to content

Commit 0dfbde9

Browse files
authored
Merge pull request #4 from hudl/refactor-newarch
refactor: adding turbo module support
2 parents e684cb7 + 15b7cd1 commit 0dfbde9

35 files changed

+811
-697
lines changed

android/build.gradle

+30-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ buildscript {
1414
}
1515
}
1616

17+
if (isNewArchitectureEnabled()) {
18+
apply plugin: "com.facebook.react"
19+
}
1720
apply plugin: 'com.android.library'
1821
apply plugin: 'kotlin-android'
1922

@@ -25,6 +28,14 @@ def getExtOrIntegerDefault(name) {
2528
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['AndroidSystemBars_' + name]).toInteger()
2629
}
2730

31+
def isNewArchitectureEnabled() {
32+
// To opt-in for the New Architecture, you can either:
33+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
34+
// - Invoke gradle with `-newArchEnabled=true`
35+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
36+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
37+
}
38+
2839
android {
2940
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
3041
buildToolsVersion getExtOrDefault('buildToolsVersion')
@@ -33,7 +44,17 @@ android {
3344
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
3445
versionCode 1
3546
versionName "1.0"
36-
47+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
48+
}
49+
50+
sourceSets {
51+
main {
52+
if (isNewArchitectureEnabled()) {
53+
java.srcDirs += ['src/newarch']
54+
} else {
55+
java.srcDirs += ['src/oldarch']
56+
}
57+
}
3758
}
3859

3960
buildTypes {
@@ -128,3 +149,11 @@ dependencies {
128149
api 'com.facebook.react:react-native:+'
129150
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
130151
}
152+
153+
if (isNewArchitectureEnabled()) {
154+
react {
155+
jsRootDir = file("../src/")
156+
libraryName = "rnsystembars"
157+
codegenJavaPackageName = "com.hudl.rn.systembars.generated"
158+
}
159+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import android.app.Activity
2+
import com.facebook.react.bridge.UiThreadUtil.runOnUiThread
3+
4+
object AndroidSystemBarsImpl {
5+
const val NAME = "AndroidSystemBars"
6+
7+
fun setSystemUIVisibility(activity: Activity?, visibility: Int) {
8+
runOnUiThread {
9+
activity?.window?.decorView?.systemUiVisibility = visibility
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
package com.hudl.rn.systembars
22

3-
import com.facebook.react.ReactPackage
3+
import com.facebook.react.TurboReactPackage
44
import com.facebook.react.bridge.NativeModule
55
import com.facebook.react.bridge.ReactApplicationContext
6-
import com.facebook.react.uimanager.ViewManager
6+
import com.facebook.react.module.model.ReactModuleInfo
7+
import com.facebook.react.module.model.ReactModuleInfoProvider
78

8-
class AndroidSystemBarsPackage : ReactPackage {
9-
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
10-
return listOf(AndroidSystemBarsModule(reactContext))
11-
}
9+
class AndroidSystemBarsPackage : TurboReactPackage() {
10+
11+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12+
return if (name == AndroidSystemBarsImpl.NAME) {
13+
AndroidSystemBarsModule(reactContext)
14+
} else null
15+
}
16+
17+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
18+
return ReactModuleInfoProvider {
19+
val isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
1220

13-
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
14-
return emptyList()
21+
hashMapOf(
22+
AndroidSystemBarsImpl.NAME to ReactModuleInfo(
23+
AndroidSystemBarsImpl.NAME,
24+
AndroidSystemBarsImpl.NAME,
25+
false,
26+
false,
27+
true,
28+
false,
29+
isTurboModule
30+
)
31+
)
1532
}
33+
}
1634
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.hudl.rn.systembars
2+
3+
import com.facebook.react.bridge.ReactApplicationContext
4+
import com.hudl.rn.systembars.generated.NativeAndroidSystemBarsSpec
5+
6+
class AndroidSystemBarsModule(reactContext: ReactApplicationContext) :
7+
NativeAndroidSystemBarsSpec(reactContext) {
8+
override fun getName() = AndroidSystemBarsImpl.NAME
9+
10+
override fun setSystemUIVisibility(visibility: Double) {
11+
AndroidSystemBarsImpl.setSystemUIVisibility(currentActivity, visibility.toInt())
12+
}
13+
}

android/src/main/java/com/hudl/rn/systembars/AndroidSystemBarsModule.kt android/src/oldarch/com/hudl/rn/systembars/AndroidSystemBarsModule.kt

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ package com.hudl.rn.systembars
33
import com.facebook.react.bridge.ReactApplicationContext
44
import com.facebook.react.bridge.ReactContextBaseJavaModule
55
import com.facebook.react.bridge.ReactMethod
6-
import com.facebook.react.bridge.UiThreadUtil.runOnUiThread
76

87
class AndroidSystemBarsModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
9-
override fun getName() = "AndroidSystemBars"
8+
override fun getName() = AndroidSystemBarsImpl.NAME
109

1110
@ReactMethod
1211
fun setSystemUIVisibility(visibility: Int) {
13-
runOnUiThread {
14-
currentActivity?.window?.decorView?.systemUiVisibility = visibility
15-
}
12+
AndroidSystemBarsImpl.setSystemUIVisibility(currentActivity, visibility)
1613
}
1714
}

example/.watchmanconfig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

example/android/app/_BUCK

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# To learn about Buck see [Docs](https://buckbuild.com/).
2+
# To run your application with Buck:
3+
# - install Buck
4+
# - `npm start` - to start the packager
5+
# - `cd android`
6+
# - `keytool -genkey -v -keystore keystores/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"`
7+
# - `./gradlew :app:copyDownloadableDepsToLibs` - make all Gradle compile dependencies available to Buck
8+
# - `buck install -r android/app` - compile, install and run application
9+
#
10+
11+
load(":build_defs.bzl", "create_aar_targets", "create_jar_targets")
12+
13+
lib_deps = []
14+
15+
create_aar_targets(glob(["libs/*.aar"]))
16+
17+
create_jar_targets(glob(["libs/*.jar"]))
18+
19+
android_library(
20+
name = "all-libs",
21+
exported_deps = lib_deps,
22+
)
23+
24+
android_library(
25+
name = "app-code",
26+
srcs = glob([
27+
"src/main/java/**/*.java",
28+
]),
29+
deps = [
30+
":all-libs",
31+
":build_config",
32+
":res",
33+
],
34+
)
35+
36+
android_build_config(
37+
name = "build_config",
38+
package = "com.example",
39+
)
40+
41+
android_resource(
42+
name = "res",
43+
package = "com.example",
44+
res = "src/main/res",
45+
)
46+
47+
android_binary(
48+
name = "app",
49+
keystore = "//android/keystores:debug",
50+
manifest = "src/main/AndroidManifest.xml",
51+
package_type = "debug",
52+
deps = [
53+
":app-code",
54+
],
55+
)

example/android/app/build.gradle

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: "com.android.application"
22

33
import com.android.build.OutputFile
4+
import org.apache.tools.ant.taskdefs.condition.Os
45

56
/**
67
* The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
@@ -15,7 +16,9 @@ import com.android.build.OutputFile
1516
* // the name of the generated asset file containing your JS bundle
1617
* bundleAssetName: "index.android.bundle",
1718
*
18-
* // the entry file for bundle generation
19+
* // the entry file for bundle generation. If none specified and
20+
* // "index.android.js" exists, it will be used. Otherwise "index.js" is
21+
* // default. Can be overridden with ENTRY_FILE environment variable.
1922
* entryFile: "index.android.js",
2023
*
2124
* // https://reactnative.dev/docs/performance#enable-the-ram-format
@@ -37,7 +40,7 @@ import com.android.build.OutputFile
3740
* // bundleInBeta: true,
3841
*
3942
* // whether to disable dev mode in custom build variants (by default only disabled in release)
40-
* // for AndroidSystemBarsExample: to disable dev mode in the staging build type (if configured)
43+
* // for example: to disable dev mode in the staging build type (if configured)
4144
* devDisabledInStaging: true,
4245
* // The configuration property can be in the following formats
4346
* // 'devDisabledIn${productFlavor}${buildType}'
@@ -64,7 +67,7 @@ import com.android.build.OutputFile
6467
* // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
6568
* // date; if you have any other folders that you want to ignore for performance reasons (gradle
6669
* // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
67-
* // for AndroidSystemBarsExample, you might want to remove it from here.
70+
* // for example, you might want to remove it from here.
6871
* inputExcludes: ["android/**", "ios/**"],
6972
*
7073
* // override which node gets called and with what additional arguments
@@ -77,7 +80,6 @@ import com.android.build.OutputFile
7780

7881
project.ext.react = [
7982
enableHermes: true, // clean and rebuild if changing
80-
entryFile: "index.tsx",
8183
]
8284

8385
apply from: "../../node_modules/react-native/react.gradle"
@@ -100,7 +102,7 @@ def enableProguardInReleaseBuilds = false
100102
/**
101103
* The preferred build flavor of JavaScriptCore.
102104
*
103-
* For AndroidSystemBarsExample, to use the international variant, you can use:
105+
* For example, to use the international variant, you can use:
104106
* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
105107
*
106108
* The international variant includes ICU i18n library and necessary data
@@ -129,16 +131,17 @@ def reactNativeArchitectures() {
129131

130132
android {
131133
ndkVersion rootProject.ext.ndkVersion
134+
132135
compileSdkVersion rootProject.ext.compileSdkVersion
133136

134137
defaultConfig {
135-
applicationId "com.example.hudlrnsystembars"
138+
applicationId "com.example"
136139
minSdkVersion rootProject.ext.minSdkVersion
137140
targetSdkVersion rootProject.ext.targetSdkVersion
138141
versionCode 1
139142
versionName "1.0"
140143
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
141-
144+
142145
if (isNewArchitectureEnabled()) {
143146
// We configure the NDK build only if you decide to opt-in for the New Architecture.
144147
externalNativeBuild {
@@ -155,7 +158,11 @@ android {
155158
cppFlags "-std=c++17"
156159
// Make sure this target name is the same you specify inside the
157160
// src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
158-
targets "hudlrnsystembars_appmodules"
161+
targets "example_appmodules"
162+
// Fix for windows limit on number of character in file paths and in command lines
163+
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
164+
arguments "NDK_APP_SHORT_COMMANDS=true"
165+
}
159166
}
160167
}
161168
if (!enableSeparateBuildPerCPUArchitecture) {
@@ -165,7 +172,7 @@ android {
165172
}
166173
}
167174
}
168-
175+
169176
if (isNewArchitectureEnabled()) {
170177
// We configure the NDK build only if you decide to opt-in for the New Architecture.
171178
externalNativeBuild {
@@ -190,6 +197,7 @@ android {
190197
// preBuild.dependsOn("generateCodegenArtifactsFromSchema")
191198
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
192199
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
200+
193201
// Due to a bug inside AGP, we have to explicitly set a dependency
194202
// between configureNdkBuild* tasks and the preBuild tasks.
195203
// This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
@@ -234,6 +242,7 @@ android {
234242
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
235243
}
236244
}
245+
237246
// applicationVariants are e.g. debug, release
238247
applicationVariants.all { variant ->
239248
variant.outputs.each { output ->
@@ -257,15 +266,17 @@ dependencies {
257266
//noinspection GradleDynamicVersion
258267
implementation "com.facebook.react:react-native:+" // From node_modules
259268

260-
261269
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
270+
262271
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
263272
exclude group:'com.facebook.fbjni'
264273
}
274+
265275
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
266276
exclude group:'com.facebook.flipper'
267277
exclude group:'com.squareup.okhttp3', module:'okhttp'
268278
}
279+
269280
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
270281
exclude group:'com.facebook.flipper'
271282
}
@@ -278,8 +289,6 @@ dependencies {
278289
} else {
279290
implementation jscFlavor
280291
}
281-
282-
implementation project(':react-native-system-bars')
283292
}
284293

285294
if (isNewArchitectureEnabled()) {
@@ -313,4 +322,4 @@ def isNewArchitectureEnabled() {
313322
// - Invoke gradle with `-newArchEnabled=true`
314323
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
315324
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
316-
}
325+
}

example/android/app/build_defs.bzl

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Helper definitions to glob .aar and .jar targets"""
2+
3+
def create_aar_targets(aarfiles):
4+
for aarfile in aarfiles:
5+
name = "aars__" + aarfile[aarfile.rindex("/") + 1:aarfile.rindex(".aar")]
6+
lib_deps.append(":" + name)
7+
android_prebuilt_aar(
8+
name = name,
9+
aar = aarfile,
10+
)
11+
12+
def create_jar_targets(jarfiles):
13+
for jarfile in jarfiles:
14+
name = "jars__" + jarfile[jarfile.rindex("/") + 1:jarfile.rindex(".jar")]
15+
lib_deps.append(":" + name)
16+
prebuilt_jar(
17+
name = name,
18+
binary_jar = jarfile,
19+
)
+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
55
* directory of this source tree.
66
*/
7-
package com.example.hudlrnsystembars;
7+
package com.example;
88

99
import android.content.Context;
1010
import com.facebook.flipper.android.AndroidFlipperClient;

example/android/app/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.hudlrnsystembars">
2+
package="com.example">
33

44
<uses-permission android:name="android.permission.INTERNET" />
55

0 commit comments

Comments
 (0)