Skip to content

Commit 34ddd7e

Browse files
author
Petter Hesselberg
authored
Fix bundling issue, update versions, add launch options (#11)
Fix the bundling issue by adding intermediate assets folder to source set Pass launch options to display build type (debug/release) Update to React Native 0.59.0; add 64-bit support Read "bundle in debug" from gradle.properties Generate BuildConfig.USE_DEVELOPER_SUPPORT Improve documentation Additional nullability annotations
1 parent 0e190f8 commit 34ddd7e

File tree

10 files changed

+460
-194
lines changed

10 files changed

+460
-194
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ The `TextInput` box appears only in the Android version. Since both platforms us
5353
* If it complains about connecting to the dev server, run `adb reverse tcp:8081 tcp:8081`
5454
* If it crashes while opening the ReactNative controls, try to modify the following phone settings:
5555
**Android Settings -> Apps -> Settings once again (the gear) to go to Configure Apps view -> Draw over other apps -> Allow React Native Android Activity Demo to draw over other apps**. (The demo app *should* ask for this automatically, though.)
56-
* To embed the bundle in the apk (and not have to run the packager), do two changes:
57-
* In `MainApplication`, make `getUseDeveloperSupport` return `false`.
58-
* In `app/build.gradle`, set `bundleInDebug: true`.
56+
* To embed the bundle in the apk (and not have to run the packager), set `bundleInDebug=true` in `android/gradle.properties`.
5957

6058
### iOS
6159

android/app/build.gradle

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,72 @@ import com.android.build.OutputFile
1414
* as expected, because the following workaround doesn't apply in that case:
1515
* https://github.com/facebook/react-native/pull/21408
1616
*/
17+
18+
def bundleInDebug = hasProperty("bundleInDebug") && Boolean.valueOf(property("bundleInDebug"))
19+
def useDevSupport = !bundleInDebug
20+
1721
project.ext.react = [
18-
// The name of the generated asset file containing your JS bundle
19-
// bundleAssetName: "index.android.bundle",
22+
// The name of the generated asset file containing your JS bundle
23+
// bundleAssetName: "index.android.bundle",
2024

21-
// The entry file for bundle generation
22-
// entryFile: "index.android.js",
25+
// The entry file for bundle generation
26+
// entryFile: "index.android.js",
2327

24-
// Whether to bundle JS and assets in debug mode. Set this to false if you want to load
25-
// JavaScript from a bundler, true if you want to embed the bundle in the apk.
26-
bundleInDebug: false,
28+
// Whether to bundle JS and assets in debug mode. Set this to false if you want to load
29+
// JavaScript from a bundler, true if you want to embed the bundle in the apk.
30+
// Do not change it here; change the property value in gradle.properties instead.
31+
bundleInDebug : bundleInDebug,
2732

28-
// Whether to bundle JS and assets in release mode
29-
bundleInRelease: true,
33+
// Whether to bundle JS and assets in release mode
34+
bundleInRelease: true,
3035

31-
// Whether to bundle JS and assets in another build variant (if configured).
32-
// See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
33-
// The configuration property can be in the following formats
34-
// 'bundleIn${productFlavor}${buildType}'
35-
// 'bundleIn${buildType}'
36-
// bundleInFreeDebug: true,
37-
// bundleInPaidRelease: true,
38-
// bundleInBeta: true,
36+
// Whether to bundle JS and assets in another build variant (if configured).
37+
// See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
38+
// The configuration property can be in the following formats
39+
// 'bundleIn${productFlavor}${buildType}'
40+
// 'bundleIn${buildType}'
41+
// bundleInBeta: true,
3942

40-
// The root of your project, i.e. where "package.json" lives
41-
// root: "../../",
43+
// The root of your project, i.e. where "package.json" lives
44+
// root: "../../",
4245

43-
// Where to put the JS bundle asset in debug mode
44-
// jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
46+
// Where to put the JS bundle asset in debug mode
47+
// jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
4548

46-
// Where to put the JS bundle asset in release mode
47-
// jsBundleDirRelease: "$buildDir/intermediates/assets/release",
49+
// Where to put the JS bundle asset in release mode
50+
// jsBundleDirRelease: "$buildDir/intermediates/assets/release",
4851

49-
// Where to put drawable resources / React Native assets, e.g. the ones you use via
50-
// require('./image.png')), in debug mode
51-
// resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
52+
// Where to put drawable resources / React Native assets, e.g. the ones you use via
53+
// require('./image.png')), in debug mode
54+
// resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
5255

53-
// Where to put drawable resources / React Native assets, e.g. the ones you use via
54-
// require('./image.png')), in release mode
55-
// resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
56+
// Where to put drawable resources / React Native assets, e.g. the ones you use via
57+
// require('./image.png')), in release mode
58+
// resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
5659

57-
// By default the gradle tasks are skipped if none of the JS files or assets change; this means
58-
// that we don't look at files in android/ or ios/ to determine whether the tasks are up to
59-
// date; if you have any other folders that you want to ignore for performance reasons (gradle
60-
// indexes the entire tree), add them here. Alternatively, if you have JS files in android/
61-
// for example, you might want to remove it from here.
62-
// inputExcludes: ["android/**", "ios/**"],
60+
// By default the gradle tasks are skipped if none of the JS files or assets change; this means
61+
// that we don't look at files in android/ or ios/ to determine whether the tasks are up to
62+
// date; if you have any other folders that you want to ignore for performance reasons (gradle
63+
// indexes the entire tree), add them here. Alternatively, if you have JS files in android/
64+
// for example, you might want to remove it from here.
65+
// inputExcludes: ["android/**", "ios/**"],
6366

64-
// Override which node gets called and with what additional arguments
65-
// nodeExecutableAndArgs: ["node"],
67+
// Override which node gets called and with what additional arguments
68+
// nodeExecutableAndArgs: ["node"],
6669

67-
// Supply additional arguments to the packager
68-
// extraPackagerArgs: []
70+
// Supply additional arguments to the packager
71+
// extraPackagerArgs: []
6972
]
7073

7174
apply from: "../../node_modules/react-native/react.gradle"
7275

7376
/**
74-
* Set this to true to create two separate APKs instead of one:
75-
* - An APK that only works on ARM devices
76-
* - An APK that only works on x86 devices
77-
* The advantage is the size of the APK is reduced by about 4MB.
77+
* Set this to true to create separate APKs instead of one:
78+
* - An APK that only works on 32-bit ARM devices
79+
* - An APK that only works on 64-bit ARM devices
80+
* - An APK that only works on 32-bit x86 devices
81+
* - An APK that only works on 64-bit x86 devices
82+
* This reduces the size of the APK.
7883
* Upload all the APKs to the Play Store and people will download
7984
* the correct one based on the CPU architecture of their device.
8085
*/
@@ -89,13 +94,14 @@ android {
8994
compileSdkVersion 28
9095

9196
defaultConfig {
92-
applicationId "com.demo.activity"
97+
applicationId 'com.demo.activity'
9398
minSdkVersion 23
99+
compileSdkVersion 28
94100
targetSdkVersion 28
95-
versionCode 4
96-
versionName "2.2"
101+
versionCode 5
102+
versionName "5.0"
97103
ndk {
98-
abiFilters 'armeabi-v7a', 'x86'
104+
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
99105
}
100106
}
101107

@@ -104,14 +110,18 @@ android {
104110
reset()
105111
enable enableSeparateBuildPerCPUArchitecture
106112
universalApk false // If true, also generate a universal APK
107-
include 'armeabi-v7a', 'x86'
113+
include 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
108114
}
109115
}
110116

111117
buildTypes {
118+
debug {
119+
buildConfigField 'boolean', 'USE_DEVELOPER_SUPPORT', "$useDevSupport"
120+
}
112121
release {
113122
minifyEnabled enableProguardInReleaseBuilds
114-
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
123+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
124+
buildConfigField 'boolean', 'USE_DEVELOPER_SUPPORT', 'false'
115125
}
116126
}
117127

@@ -120,20 +130,34 @@ android {
120130
variant.outputs.each { output ->
121131
// For each separate APK per architecture, set a unique version code as described here:
122132
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
123-
def versionCodes = ["armeabi-v7a": 1, "x86": 2]
133+
def versionCodes = ['armeabi-v7a': 1, 'arm64-v8a': 2, 'x86': 3, 'x86_64': 4]
124134
def abi = output.getFilter(OutputFile.ABI)
125135
if (abi != null) { // null for the universal-debug, universal-release variants
126136
output.versionCodeOverride =
127137
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
128138
}
129139
}
130140
}
141+
142+
// This is necessary to include the JavaScript bundle in the APK:
143+
sourceSets {
144+
main {
145+
debug {
146+
if (bundleInDebug) {
147+
assets.srcDirs += "$buildDir/intermediates/assets/debug"
148+
}
149+
}
150+
release {
151+
assets.srcDirs += "$buildDir/intermediates/assets/release"
152+
}
153+
}
154+
}
131155
}
132156

133157
dependencies {
134-
implementation fileTree(dir: "libs", include: ["*.jar"])
158+
implementation fileTree(dir: 'libs', include: ['*.jar'])
135159
implementation 'com.android.support:appcompat-v7:28.0.0'
136-
implementation('com.facebook.react:react-native:0.58.6') {
160+
implementation('com.facebook.react:react-native:0.59.9') {
137161
exclude group: 'com.android.support'
138162
}
139163
}

android/app/src/main/java/com/demo/activity/ActivityStarterReactPackage.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.demo.activity;
22

3+
import android.support.annotation.NonNull;
4+
35
import com.facebook.react.ReactPackage;
46
import com.facebook.react.bridge.NativeModule;
57
import com.facebook.react.bridge.ReactApplicationContext;
@@ -15,16 +17,18 @@
1517
*/
1618
final class ActivityStarterReactPackage implements ReactPackage {
1719

20+
@NonNull
1821
@Override
19-
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
22+
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
2023
List<NativeModule> modules = new ArrayList<>();
2124
modules.add(new ActivityStarterModule(reactContext));
2225
modules.add(new EventEmitterModule(reactContext));
2326
return modules;
2427
}
2528

29+
@NonNull
2630
@Override
27-
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
31+
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
2832
return Collections.emptyList();
2933
}
3034
}

android/app/src/main/java/com/demo/activity/MainActivity.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.widget.Toast;
99

1010
import com.facebook.react.ReactActivity;
11+
import com.facebook.react.ReactActivityDelegate;
1112
import com.facebook.react.ReactInstanceManager;
1213
import com.facebook.react.ReactNativeHost;
1314
import com.facebook.react.devsupport.interfaces.DevOptionHandler;
@@ -23,16 +24,33 @@ public final class MainActivity extends ReactActivity {
2324
/**
2425
* Returns the name of the main component registered from JavaScript.
2526
* This is used to schedule rendering of the component.
27+
* Because this class overrides {@link #createReactActivityDelegate()}, we don't really need
28+
* to override this.
2629
*/
2730
@Override
28-
@Nullable
2931
protected String getMainComponentName() {
3032
return "ActivityDemoComponent";
3133
}
3234

35+
/**
36+
* We override to provide launch options that we can read in JavaScript (see buildType).
37+
*/
38+
@Override
39+
protected ReactActivityDelegate createReactActivityDelegate() {
40+
return new ReactActivityDelegate(this, getMainComponentName()) {
41+
@Override
42+
protected Bundle getLaunchOptions() {
43+
Bundle launchOptions = new Bundle();
44+
launchOptions.putString("buildType", BuildConfig.BUILD_TYPE);
45+
return launchOptions;
46+
}
47+
};
48+
}
49+
3350
/**
3451
* Demonstrates how to add a custom option to the dev menu.
3552
* https://stackoverflow.com/a/44882371/3968276
53+
* This only works from the debug build with dev options enabled.
3654
*/
3755
@Override
3856
@CallSuper

android/app/src/main/java/com/demo/activity/MainApplication.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
import java.util.Arrays;
1414
import java.util.List;
1515

16+
/**
17+
* Base class for maintaining global application state -- in this case, the {@link ReactNativeHost}.
18+
*/
1619
public final class MainApplication extends Application implements ReactApplication {
1720

1821
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
1922
@Override
2023
public boolean getUseDeveloperSupport() {
21-
return BuildConfig.DEBUG;
24+
return BuildConfig.USE_DEVELOPER_SUPPORT;
2225
}
2326

2427
@Override
@@ -30,12 +33,21 @@ protected List<ReactPackage> getPackages() {
3033
}
3134
};
3235

36+
/**
37+
* Get the {@link ReactNativeHost} for this app.
38+
*/
3339
@Override
3440
@NonNull
3541
public ReactNativeHost getReactNativeHost() {
3642
return mReactNativeHost;
3743
}
3844

45+
/**
46+
* Called when the application is starting, before any activity, service,
47+
* or receiver objects (excluding content providers) have been created.
48+
*
49+
* <p>This implementation loads the React Native JNI libraries.</p>
50+
*/
3951
@Override
4052
@CallSuper
4153
public void onCreate() {

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
google()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.4.0'
9+
classpath 'com.android.tools.build:gradle:3.4.1'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

android/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryErro
1919

2020
android.useDeprecatedNdk=true
2121
android.debug.obsoleteApi=true
22+
23+
bundleInDebug=true

index.android.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default class ActivityDemoComponent extends Component {
5151
return (
5252
<View style={styles.container}>
5353
<Text style={styles.welcome}>
54-
Welcome to React Native!
54+
Welcome to React Native ({this.props.buildType})!
5555
</Text>
5656
<Text style={styles.instructions}>
5757
<Text>To get started, edit </Text>

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{
22
"name": "activity",
3-
"version": "0.0.4",
3+
"repository": {
4+
"type": "git",
5+
"url": "https://github.com/petterh/react-native-android-activity.git"
6+
},
7+
"version": "5.0.0",
48
"private": true,
59
"scripts": {
610
"start": "node node_modules/react-native/local-cli/cli.js start"
711
},
812
"dependencies": {
9-
"react": "^16.8.3",
10-
"react-native": "^0.58.6"
13+
"react": "^16.8.6",
14+
"react-native": "^0.59.9"
1115
},
1216
"devDependencies": {
13-
"babel-loader": "7.1.4",
17+
"babel-loader": "8.0.6",
1418
"babel-plugin-transform-runtime": "6.23.0",
15-
"metro-react-native-babel-preset": "^0.45.0"
19+
"metro-react-native-babel-preset": "^0.54.1"
1620
}
1721
}

0 commit comments

Comments
 (0)