React Native ~ Android Production Build - rohit120582sharma/Documentation GitHub Wiki

Generating Production (Signed) APK

References


Steps


Generating a signing key

First we need to generate a keystore file. Android requires that all apps be digitally signed with a certificate before they can be installed, so to distribute your Android application via Google Play store, you'll need to generate a signed release APK.

You can generate a private signing key using keytool with setting the name of the file, setting an alias, and making this key valid for 10,000 days.

You’ll want to change my-release-key.keystore and my-key-alias to something that makes sense for you.

On Mac, keytool must be run from project root directory.

$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

This command prompts you to set a password for the keystore itself, then your organisational information which is stored within the keystore file. Finally you’re asked for another password. This one is associated with your alias.

It then generates the keystore as a file called my-release-key.keystore.

I’m not sure if this is necessary but keep your keystore file private and never commit it to version control. So add my-release-key.keystore file to .gitignore file.


Setting up gradle variables

Place the my-release-key.keystore file under the android/secure directory in your project folder.

Edit the file android/gradle.properties, and add the following (replace ***** with the correct keystore password, alias and key password)

Make sure gradle.properties does not include org.gradle.configureondemand=true as that will make the release build skip bundling JS and assets into the APK.

MYAPP_RELEASE_STORE_FILE=../secure/my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=*****
MYAPP_RELEASE_KEY_PASSWORD=*****

Adding signing config

Edit the file android/app/build.gradle in your project folder, and add the signing config:

...
android {
	...
	defaultConfig { ... }
	signingConfigs {
		release {
			if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
				storeFile file(MYAPP_RELEASE_STORE_FILE)
				storePassword MYAPP_RELEASE_STORE_PASSWORD
				keyAlias MYAPP_RELEASE_KEY_ALIAS
				keyPassword MYAPP_RELEASE_KEY_PASSWORD
			}
		}
	}
	buildTypes {
		release {
			...
			signingConfig signingConfigs.release
		}
	}
}
...

Updating version code and name

Increment versionCode and versionName in android/app/build.gradle file.

versionCode is an integer used as an internal version number. You should make sure that each successive release of your app uses a greater value. You cannot upload an APK to the Play Store with a versionCode you have already used for a previous version.

...
android {
	...
	defaultConfig {
		applicationId "com.awesomeproject"
		minSdkVersion 16
		targetSdkVersion 22
		versionCode 1
		versionName "1.0"
	}
}
...

Updating "deployment key" for CodePush

If using CodePush, then change deploymentKey according to deploymentName (ex. Production / Staging etc.) in src/main/java/com/{project}/MainApplication.java file in your project:

// new CodePush("deployment-key", getApplicationContext(), BuildConfig.DEBUG)
new CodePush("mjZUbi-Cle5HHzvfRPa1wpbjKPDq4622b879-1c61-4392-ad48", getApplicationContext(), BuildConfig.DEBUG)

Generating the release APK

Finally generate your production apk (app-release.apk) with the following commands in a terminal:

$ cd android
$ ./gradlew clean
$ ./gradlew assembleRelease

Gradle's assembleRelease will bundle all the JavaScript needed to run your app into the APK. If you need to change the way the JavaScript bundle and/or drawable resources are bundled, have a look at android/app/build.gradle to see how you can update it to reflect these changes.

The final APK that you would upload to the Play Store can be found under android/app/build/outputs/apk/app-release.apk.


Testing the release build of your app

Before uploading the release build to the Play Store, make sure you test it thoroughly.

First uninstall any previous version of the app you already have installed. Install it on the device using:

$ cd ..
$ react-native run-android --variant=release

Note that --variant=release is only available if you've set up signing as described above.



Troubleshooting

You may run into issues with Couldn’t follow symbolic link

Remove node_modules and reinstall will eliminate some node packages that causes the problem.


You may also run into uncompiled PNG file passed as argument. Must be compiled first into a .flat file.. error: failed parsing overlays.

Add android.enableAapt2=false to android/gradle.properties


Release APK fails immediately on launch

Refer below for sort description and this link for complete details.

  • Within your projects build.gradle change the buildScripts dependencies classpath to com.android.tools.build:gradle:3.1.4 I wasn't able to get v3.2.0 to work, so I had to downgrade to 3.1.4. This might work with lower versions too, but I haven't tested it.

  • Placing android.enableAapt2=false within the gradle.properties

⚠️ **GitHub.com Fallback** ⚠️