如何使用终端从bash脚本构建release apk

时间:2018-01-09 09:00:10

标签: android bash macos unix

这是我试图从终端生成发布apk的脚本。我有这个脚本来生成发布apk。但我很困惑在哪里放置脚本中给出的keystore.jks,用户名和密码。我在mac中执行这个脚本。此处$1值为app。脚本名称为assemble.bash。

    LATEST_BUILD_TOOLS=$ANDROID_HOME/build-tools/$(ls $ANDROID_HOME/build-tools | sort -n | tail -1)
ZIPALIGN=$LATEST_BUILD_TOOLSzipalign
APKSIGNER=$LATEST_BUILD_TOOLSapksigner

# define where we the built artifact is going to end up and where
# to put the signed apk if the artifact is an apk.
APK_DIRECTORY=$1/build/outputs/apk
AAR_DIRECTORY=$1/build/outputs/aar
UNSIGNED_APK_PATH=$APK_DIRECTORY/$1-release-unsigned.apk
APK_PATH=$APK_DIRECTORY/$1-release.apk

# assemble unsigned release apk
./gradlew -p $1 clean assembleRelease

if [ -d "$APK_DIRECTORY" ] ; then

  # prepare the unsigned release apk for signing
  $ZIPALIGN -v -p 4 $UNSIGNED_APK_PATH $APK_PATH

  # sign the release apk
  $APKSIGNER sign --ks /.keystore/keystore.jks --ks-pass file:/.keystore/ks-pass $APK_PATH

  # delete the unsigned release apk
  rm $UNSIGNED_APK_PATH

  echo "SUCCESS: Release APK has been assembled"

1 个答案:

答案 0 :(得分:0)

修改您的应用build.gradle,如:

...    
signingConfigs {

   release {
       storeFile file(RELEASE_STORE_FILE)
       storePassword RELEASE_STORE_PASSWORD
       keyAlias RELEASE_KEY_ALIAS
       keyPassword RELEASE_KEY_PASSWORD
   }
}

buildTypes {
        release {
            signingConfig signingConfigs.release
        }
}
....

修改/创建您的~/.gradle/gradle.properties,如:

RELEASE_STORE_FILE={/path/to/keystore}
RELEASE_STORE_PASSWORD=*****
RELEASE_KEY_ALIAS=*****
RELEASE_KEY_PASSWORD=*****

最后:

./gradlew assembleRelease将发布apk版本。

将签名配置分隔为gradle.properties将确保您的私钥,别名和密码不会泄露到您的版本控制中。