1. 程式人生 > >自定義gradle,讓程式碼更簡潔,讓簽名更安全

自定義gradle,讓程式碼更簡潔,讓簽名更安全

文章目錄

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
            }
        }
    }