CC's blog

CC的技术博客 专注于 it 互联网的技术爱好者

Android Studio apk 自动签名打包

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.example.bear.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        release {
            storeFile
            storePassword
            keyAlias
            keyPassword
        }
    }
    //调用配置文件
    getSigningProperties()


    buildTypes {
        release {
            minifyEnabled true //混淆
            zipAlignEnabled true //Zipalign优化
            shrinkResources true // 移除无用的resource文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //注意要添加签名
            //signingConfig signingConfigs.release
            //自定义apk名字
            signingConfig signingConfigs.release

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    output.outputFile = new File(
                            output.outputFile.parent,
                            "应用前缀-release-${defaultConfig.versionName}-${releaseTime()}.apk".toLowerCase())
                }
            }

        }
    }


}

<!--more-->

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.+'
    compile 'com.android.support:design:25.+'
}



//获取时间
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

//读取签名配置文件 这个需要放在android{}外面
def getSigningProperties(){
             android.signingConfigs.release.storeFile = file('./signKey/android.keystore')
            android.signingConfigs.release.storePassword = 15223245
            android.signingConfigs.release.keyAlias = 'test'
            android.signingConfigs.release.keyPassword = 15223245

}
none

添加新评论