【ReactNative】Mac下分分鐘打包Android apk

時(shí)間:2016-11-20 09:17:07
地址:https://github.com/zhongxia245/blog/issues/52

Mac 下 ReactNative如何打包構(gòu)建Android apk 的應(yīng)用。

總結(jié)

  1. 打包 jsbundle代碼
  2. 生成簽名
  3. 添加簽名到app中
  4. 打包成apk(打包前也可以先運(yùn)行安裝在手機(jī)上測試)

把JS代碼,打包成 index.android.jsbundle

參考文章:ReactNative打離線包-android篇

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

《官網(wǎng)打包APK教程》

一、操作步驟:

1. 生成簽名

生成一個(gè)有效期10000天的證書,證書為: my-release-key.keystore

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

不要把證書提交到版本控制系統(tǒng)

Note: Remember to keep your keystore file private and never commit it to version control.

2. 配置打包時(shí)的簽名

把 證書復(fù)制到 android/app 目錄下

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

在 gradle.properties 里面添加以下配置

gradle.properties 文件是在 android的根目錄下。 官網(wǎng)上的文件位置是錯(cuò)誤的。(或者說,我理解錯(cuò)誤)

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.

3. 添加簽名

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
        }
    }
}
...

4. 減小打包apk的大小

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

5. 發(fā)布前測試應(yīng)用是否有問題

react-native run-android --variant=release

5. 生成apk

生成的apk文件在, android/app/build/outputs/apk/ 下

cd android && ./gradlew assembleRelease

6. apk文件位置

android/app/build/outputs/apk

7. 安裝apk文件

adb install app-release.apk

app-release 是發(fā)布版的apk文件。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容