手记

Android Gradle signing 编译打包apk的几种办法

Android Gradle signing 编译打包apk的几种办法1.通常打包signing办法

通常情况下我们编译打包都是手动进行的。 

2.在Gradle内部配置signing办法

其实我们可以通过设置Build Variants来进行buildTypes设置使用Gradle自动进行签名打包。

比如Build Variants设置成release选项 
接着就开始配置信息设置了设置之后大概是这样的

    signingConfigs {
        release {
            keyAlias 'xxxx'
            keyPassword '**********'
            storeFile file('../xxxx/release.jks')
            storePassword '**********'
        }
    }

     buildTypes {
        release {
            signingConfig signingConfigs.release
        }
     }

3.加载外部配置文件signing办法

可是这样当你提交文件的时候,signingConfigs配置的详细信息如密码就提交到服务器了,这样不太友好。其实我们可以通过调用配置文件的方式来解决这个问题。

首先新建配置文件名称为signing.properties

RELEASE_STORE_FILE=../xxxx/release.jks
RELEASE_STORE_PASSWORD=**********RELEASE_KEY_ALIAS=xxxx
RELEASE_KEY_PASSWORD=**********

然后把signing.properties添加到提交忽略文件中如.gitignore

# Ignore gradle filessigning.properties

接着在./app/build.gradle文件中配置加载配置文件信息信息

/**
 * 加载签名配置文件
 */def loadSigningConfigs() {    def Properties props = new Properties()    def propFile = file('../signing.properties')    if (propFile.canRead()) {
        props.load(new FileInputStream(propFile))        if (props != null && props.containsKey('RELEASE_STORE_FILE') && props.containsKey('RELEASE_STORE_PASSWORD') &&
                props.containsKey('RELEASE_KEY_ALIAS') && props.containsKey('RELEASE_KEY_PASSWORD')) {
            android.signingConfigs.release.storeFile = file(props['RELEASE_STORE_FILE'])
            android.signingConfigs.release.storePassword = props['RELEASE_STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['RELEASE_KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['RELEASE_KEY_PASSWORD']
        } else {
            android.buildTypes.release.signingConfig = null
        }
    } else {
        android.buildTypes.release.signingConfig = null
    }
}

最后调用配置信息就可以了

android {
    signingConfigs {
        release {
        }
    }
    loadSigningConfigs()
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

原文链接:http://www.apkbus.com/blog-873055-76386.html

0人推荐
随时随地看视频
慕课网APP