Android 使用 Gradle 打包 - 签名配置

序言

最近正好在项目里用到了 Gradle 打包的配置,顺便总结一下。 一般的 apk 打包类型分为 debug 和 release:debug 开启日志,不进行混淆,用于测试;release 关闭日志,开启混淆,用于正式发布。在 Android Studio 里面,生成的 debug 包使用了 AS 默认的签名,而 release 包没有签名。如果我们需要定制,比如生成 debug 和 release 包后,自动进行签名,就需要在 gradle 配置文件里面加上 apk 签名的配置。

android {
    ......
    // 配置 release 的签名信息
    signingConfigs {
        release {
            storeFile
            storePassword
            keyAlias
            keyPassword
        }
    }

    // 读取签名配置
    getSigningProperties()

    buildTypes {
        // debug 和 release 使用同样的签名
        debug {
            signingConfig signingConfigs.release
        }

        release {
            minifyEnabled true
            shrinkResources true
            zipAlignEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            // 修改生成的 apk 文件名,输出 apk 名称:MyApp_v1.0.0_2017-11-10_debug.apk
            applicationVariants.all { variant ->
                def suffix
                if (variant.buildType.name == 'release') {
                    suffix = 'release'
                } else {
                    suffix = 'debug'
                }
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "MyApp_v${defaultConfig.versionName}_${releaseTime()}_${suffix}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }
    ......
}

// 读取签名配置
def getSigningProperties() {
    def propFile = file('../signing.properties')
    if (propFile.exists() && propFile.canRead()) {
        def props = new Properties()
        props.load(new FileInputStream(propFile))
        if (props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
                props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
            android.signingConfigs.release.storeFile = file('../' + props['STORE_FILE'])
            android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
        } else {
            println 'signing.properties are found but some entries are missed!'
            android.buildTypes.release.signingConfig = null
        }
    } else {
        println 'signing.properties are not found!'
        android.buildTypes.release.signingConfig = null
    }
}

// 定义打包时间
static def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
复制代码

其中,签名配置文件(signing.properties)和签名文件(.jks)存放在项目工程的根目录,它们不需要加入版本控制。这是 signing.properties,可以根据需要自己修改。

STORE_FILE=RichiePersonal.jks
STORE_PASSWORD=ExamplePassword
KEY_ALIAS=ExampleKeyAlias
KEY_PASSWORD=ExampleKeyPassword
复制代码

最后,我们通过 Gradle 命令就可以完成打包工作,生成的 apk 文件名像是这样:MyApp_v1.0.0_2017-11-10_debug.apk。 命令格式:gradlew project:task

gradlew app:assembleDebug

或者

gradlew app:assembleRelease

Gradle 构建的功能还是蛮强大的,从签名的配置就能看出,此外还有自定义构建类型、多工程全局配置、多渠道打包等待,这里就不多介绍了,感兴趣的朋友请自行了解。

转载于:https://juejin.im/post/5c938f726fb9a071090d619c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值