自定义gradle,让代码更简洁,让签名更安全

242 阅读1分钟

文章目录

rootproject下自定义gradle文件(config.gradle)

ext.versions = [
        // 本地仓库版本
//        compileSdkVersion: 26,
//        buildToolsVersion: "26.0.3",
//        minSdkVersion    : 18,
//        targetSdkVersion : 26,
        compileSdkVersion: 26,
        buildToolsVersion: "26.0.3",
        minSdkVersion    : 18,
        targetSdkVersion : 26,
        versionCode      : 4,
        versionName      : "1.2.1",
        supportVersion   : "25.3.0"
]

// app 相关信息总配置
ext.appConfig = [
        appId            : 'dingshi.com.hibook',
        betaName         : '@string/app_name',
        proName          : '@string/app_name',
//        keyAlias     : 'hibook',
//        keyPassword  : 'hibook',
//        storeFile    : 'hibook.jks',
//        storePassword: 'hibook',
        GETUI_APP_ID     : "PhmRY1Uy8A9VrK2L2H8Qz3",
        GETUI_APP_KEY    : "VXJjCzXD0M7UqsPzFbXtT7",
        GETUI_APP_SECRET : "ZmO60vhutt6NxMauNZJjl8",
//      EASEMOB_DEBUG    : "1191180110115352#hello-book-dev",
        EASEMOB_DEBUG    : "1191180110115352#hello-book",
        EASEMOB_RELEASE  : "1191180110115352#hello-book",
        LOCALHOST_DEBUG  : "\"http://api.linkbooker.com/\"",
//      LOCALHOST_DEBUG  : "\"http://testapi.linkbooker.com/\"",
        LOCALHOST_RELEASE: "\"http://api.linkbooker.com/\""

]

ext.channel = [
        HIBOOK_TEST_NAME: "test_channel",
        Baidu           : "Baidu",
        Xiaomi          : "Xiaomi",
        Huawei          : "huawei",
        Vivo            : "vivo",
        Oppo            : "oppo",
        Tencent         : "tencent",
        Server          : "server"
]

gradle.properties中设置签名信息

签名文件在主module的root下,也就是项目中,这种不提倡,也不安全

KEY_ALIAS=hibook
KEY_PASSWORD=hibook
STORE_FILE=hibook.jks
STORE_PASSWORD=hibook

安全的做法就是将签名文件放在本地或者服务器上

org.gradle.parallel=true
KEY_ALIAS =netdemo
KEY_PASSWORD=123456
STORE_FILE= C:/Users/ytf/netdemo.jks
STORE_PASSWORD=123456

app下的module中的build.gradle引入依赖

apply plugin: 'com.android.application'
apply from: "$rootDir/config.gradle"

android {
    signingConfigs {
        config {
          keyAlias appConfig.keyAlias
	keyPassword appConfig.keyPassword
	storeFile file(appConfig.storeFile)
	storePassword appConfig.storePassword
        }
    }
    compileSdkVersion versions.compileSdkVersion
    defaultConfig {
        applicationId appConfig.appId
        minSdkVersion versions.minSdkVersion
        targetSdkVersion versions.targetSdkVersion
        versionCode versions.versionCode
        versionName versions.versionName
        flavorDimensions "versionCode" // flavor dimension 它的维度就是该版本号,统一维度,
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            buildConfigField "String", "LOCALHOST", appConfig.LOCALHOST_RELEASE
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable true
            jniDebuggable true
            renderscriptDebuggable true
            zipAlignEnabled true
        }
        debug {
            debuggable true
            jniDebuggable true
            signingConfig signingConfigs.config
            renderscriptDebuggable true
            minifyEnabled true
            pseudoLocalesEnabled true
            zipAlignEnabled true
        }
    }
    buildToolsVersion versions.buildToolsVersion
//    dexOptions {
//        incremental true
//    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

//    flavorDimensions "versionCode" // flavor dimension 它的维度就是该版本号,统一维度,
    productFlavors {
//       一下括号中的  dimension "versionCode"可以删除
        Alia {

        }
        Xiaomi {

        }
        Huawei {

        }
        Vivo {

        }
        Oppo {

        }
        Tencent {
//            dimension "versionCode"
        }

    }

    productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: flavor.name]
    }



    //***AS3.0版本 设置apk名称***
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            if (variant.name.endsWith("Debug")) {
                //debug包
                outputFileName = "$applicationId _v${defaultConfig.versionName}_${getDate()}_code${defaultConfig.versionCode}_debug.apk"
            } else {
                //release包
                variant.outputs.each { output ->
                    def appName = 'athesismentor'
                    def oldFile = output.outputFile
                    def buildName
                    def releaseApkName

                    variant.productFlavors.each { product ->
                        buildName = product.name
                    }

                    releaseApkName = appName + '-' + buildName + '-' + getDate() + '_release.apk'
//                output.outputFile = new File(oldFile.parent, releaseApkName)
                    outputFileName =releaseApkName
//                  outputFileName = "$applicationId _v${defaultConfig.versionName}_code${defaultConfig.versionCode}_${getDate()}_release.apk"
                }

            }
        }
    }
}
//获取时间戳
static def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyy-MM-dd-HH_mm')
    return formattedDate
}


configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion versions.supportVersion
            }
        }
    }
}
dependencies {
。。。
}

AndroidStudio3.0以上自定义打包apk的名字

就是从上边代码抄录出来画个重点
在android目录下

//***AS3.0版本 设置apk名称***
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            if (variant.name.endsWith("Debug")) {
                //debug包
                outputFileName = "$applicationId _v${defaultConfig.versionName}_${getDate()}_code${defaultConfig.versionCode}_debug.apk"
            } else {
                //release包
                variant.outputs.each { output ->
                    def appName = 'athesismentor'
                    def oldFile = output.outputFile
                    def buildName
                    def releaseApkName

                    variant.productFlavors.each { product ->
                        buildName = product.name
                    }

                    releaseApkName = appName + '-' + buildName + '-' + getDate() + '_release.apk'
//                output.outputFile = new File(oldFile.parent, releaseApkName)
                    outputFileName =releaseApkName
//                  outputFileName = "$applicationId _v${defaultConfig.versionName}_code${defaultConfig.versionCode}_${getDate()}_release.apk"
                }

            }
        }
    }

在android目录外边

//获取时间戳
static def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyy-MM-dd-HH_mm')
    return formattedDate
}


configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion versions.supportVersion
            }
        }
    }