1-: Generate a signature key
You can use the keytool command to generate a private key. On Windows, the keytool command is placed in the bin directory of the JDK (such as C:\Program Files\Java\.x_x\bin). You may need to enter that directory on the command line before you can execute this command. On the mac, go directly to the project root directory and enter the command:
$ keytool -genkey -v -keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
This command will require you to enter the keystore and password for the corresponding key, and then set some release-related information. Finally it generates a keystore file called.
After running the above statement, a separate key should have been generated in the keystore, with a validity period of 10,000 days. The alias after the --alias parameter is what you need to use when signing for the application in the future, so remember to record this alias.
Notice:Please remember to keep your keystore file properly and do not upload it to the repository or other places.
2-: Set gradle variable
Put the file in the android/app folder in your project.
Edit ~/.gradle/ (If you don’t have this file, just create one), add the following code (note that you replace ** with the corresponding password)
Note: ~ means the user directory, such as C:\Users\username on Windows, and /Users/username on Mac.
MYAPP_RELEASE_STORE_FILE= MYAPP_RELEASE_KEY_ALIAS=my-key-alias MYAPP_RELEASE_STORE_PASSWORD=* MYAPP_RELEASE_KEY_PASSWORD=*
The above will be used as global gradle variables
Notes on keystores:
Once you publish your app in the app market (Application Bag, 360, etc.), if you want to modify your signature, you must republish your app with a different package name (this will also lose all downloads and ratings). So be sure to back up your keystore and password.
3-: Add signature to the project's gradle configuration file
Edit android/app/ in your project directory and add the following signature configuration:
android { ... defaultConfig { ... } signingConfigs { release { storeFile file(MYAPP_RELEASE_STORE_FILE) storePassword MYAPP_RELEASE_STORE_PASSWORD keyAlias MYAPP_RELEASE_KEY_ALIAS keyPassword MYAPP_RELEASE_KEY_PASSWORD } } buildTypes { release { ... signingConfig } } }
4-: Generate and issue APK packages
Simply run the following command in the terminal:
$ cd android && ./gradlew assembleRelease
Translator's note: cd android means entering the Android directory (if you are already in the Android directory, you don't need to enter it).
./gradlew assembleRelease is used in the PowerShell environment of macOS, Linux or Windows that executes a script file named gradlew in the current directory, and its running parameter is assembleRelease. Note that this ./ cannot be omitted; while in the traditional CMD command line of Windows, ./ needs to be removed.
Gradle's assemblyRelease parameter will package all the used JavaScript code together and then be built into the APK package. If you want to adjust this behavior (such as js code and default file name or directory structure for static resource packaging, etc.), you can check out the android/app/file.
The generated APK file is located in android/app/build/outputs/apk/, which can be used to publish.
5-: Test the release version of the application
$ cd android && ./gradlew installRelease
Note that the installRelease parameter can only be used after you have completed the above signature configuration. You can now turn off the running packager, because all your code and framework dependencies have been packaged into the apk package and can be run offline.
When switching installations between debug and release versions, there may be an error signature mismatch. At this time, you need to uninstall the previous version before trying to install it.
6-: Enable Proguard code obfuscation to reduce the size of APK files (optional)
Proguard is a Java bytecode obfuscation compression tool that can remove the unused parts of React Native Java (and its dependency library), and ultimately effectively reduce the size of the APK.
Important: After enabling Proguard, you must test your app comprehensively again. Proguard sometimes needs to make some extra configuration for each native library you introduce. See app/file.
To enable Proguard, set the minifyEnabled option to true:
/** * Enable Proguard in release distribution to shrink the Java bytecode in release builds. */ def enableProguardInReleaseBuilds = true
Personal advice:If your project cannot run because it is added to this property, delete this configuration. Because, this really leads to all kinds of strange problems.
The above article RN packaged and released app on Android (detailed explanation) is all the content I share with you. I hope it can give you a reference and I hope you can support me more.