時間:2016-11-20 09:17:07
地址:https://github.com/zhongxia245/blog/issues/52html
Mac 下 ReactNative如何打包構建Android apk 的應用。該文章還差一個 打包發佈到各個平臺的教程java
參考文章:ReactNative打離線包-android篇react
react-native bundle --entry-file demo/index.js --bundle-output ./android/app/src/main/assets/index.android.jsbundle --platform android --assets-dest ./android/app/src/main/res/ --dev false
《官網打包APK教程》android
生成一個有效期10000天的證書,證書爲: my-release-key.keystoregit
keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
不要把證書提交到版本控制系統github
Note: Remember to keep your keystore file private and never commit it to version control.segmentfault
把 證書複製到 android/app 目錄下react-native
Edit the file ~/.gradle/gradle.properties and add the following (replace ***** with the correct keystore password, alias and key password)app
在 gradle.properties 裏面添加如下配置測試
gradle.properties 文件是在 android的根目錄下。 官網上的文件位置是錯誤的。(或者說,我理解錯誤)
MYAPP_RELEASE_STORE_FILE=my-release-key.keystore MYAPP_RELEASE_KEY_ALIAS=my-key-alias MYAPP_RELEASE_STORE_PASSWORD=***** MYAPP_RELEASE_KEY_PASSWORD=*****
These are going to be global gradle variables, which we can later use in our gradle config to sign our app.
Adding signing config to your app's gradle config
Edit the file android/app/build.gradle in your project folder and add the signing config,
修改 android/app/build.gradle 添加簽名
... 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 signingConfigs.release } } } ...
Enabling Proguard to reduce the size of the APK (optional)
Proguard is a tool that can slightly reduce the size of the APK. It does this by stripping parts of the React Native Java bytecode (and its dependencies) that your app is not using.
IMPORTANT: Make sure to thoroughly test your app if you've enabled Proguard. Proguard often requires configuration specific to each native library you're using. See app/proguard-rules.pro.
修改文件:android/app/build.gradle
To enable Proguard, edit android/app/build.gradle:
/** * Run Proguard to shrink the Java bytecode in release builds. */ def enableProguardInReleaseBuilds = true
react-native run-android --variant=release
生成的apk文件在, android/app/build/outputs/apk/ 下
cd android && ./gradlew assembleRelease
android/app/build/outputs/apk
adb install app-release.apk
app-release 是發佈版的apk文件。