本文主要讲解以最简单的方式打包安卓apk
生成签名文件
- 打开
AndroidStudio
>菜单选择build
>Generate Signed APK
- 在打开的界面点击
Next
,会弹出下边的界面,选择Create new
-
按下图填写
- 点击
ok
,此时签名文件已生成,点击next>Finish
(这里选的是release
即正式包,如果打测试包,Build Type
选择releaseStaging
,同时下边的配置也要放在对应的releaseStaging中)
用签名文件打包apk
- gradle配置
- 将生成的文件复制到
android/app
下
- 修改项目中的
gradle.properties
文件,添加如下代码
MYAPP_RELEASE_STORE_FILE=appRN.jks
MYAPP_RELEASE_KEY_ALIAS=appRN_alias
MYAPP_RELEASE_STORE_PASSWORD=xizhu211
MYAPP_RELEASE_KEY_PASSWORD=xizhu211
- 修改
android/app/build.gradle
文件,添加如下代码
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
新版要添加matchingFallbacks
buildTypes {
debug {
//这行没有的话本地运行会报错,提示[CODEPUSH_KEY]不存在,因为默认[react-native run-android]运行的是debug版本
buildConfigField "String", "CODEPUSH_KEY", '""'
matchingFallbacks = ['release', 'releaseStaging', 'debug']
}
releaseStaging {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
buildConfigField "String", "CODEPUSH_KEY", '"6hiQqZTB8NoJpvxxmVJE-oXOa6jJSJt39eDX4"'
matchingFallbacks = ['release', 'releaseStaging', 'debug']
}
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
buildConfigField "String", "CODEPUSH_KEY", '"HS9QpxZ2okNt28Eqa-g_EyAj8Cl9HkKh5gDQ4"'
matchingFallbacks = ['release', 'releaseStaging', 'debug']
}
}
- 建立.sh文件,添加如下代码,执行即可。
cd android/
./gradlew assembleRelease
open app/build/outputs/apk
参考文档
https://developer.android.com/studio/publish/app-signing
http://blog.csdn.net/developer_jiangqq/article/details/50525976/