Android Studio配置Gradle(包括signingConfigs、buildTypes和productFlavors等)

笔者目前使用的Gradle配置描述如下:
配置划分:

  • defaultConfig 默认配置
  • signingConfigs 签名信息配置
  • buildTypes
  • productFlavors
  • 打包apk重命名
  • dependencies 依赖配置

详细配置:

defaultConfig 默认配置

defaultConfig {
   //项目包名配置
   applicationId "com.wildcreek.demo" 
   minSdkVersion 16
   targetSdkVersion 19
   versionCode 14
   versionName "1.0.14"
}

signingConfigs 签名信息配置

signingConfigs {
  release {//发布版本的签名配置
      storeFile file(props['KEYSTORE_FILE'])
      keyAlias props['KEY_ALIAS']
      storePassword props['KEYSTORE_PWD']
      keyPassword props['KEY_PWD']
  }
  debug {//调试版本的签名配置
      storeFile file(props['DEBUG_KEYSTORE'])
      keyAlias props['DEBUG_ALIAS']
      storePassword props['DEBUG_KEYSTORE_PWD']
      keyPassword props['DEBUG_KEY_PWD']
  }
}

签名配置文件signing.properties:

KEYSTORE_FILE = release.keystore
KEY_ALIAS = wildcreek
KEYSTORE_PWD= xxx
KEY_PWD= xxx

DEBUG_KEYSTORE= debug.keystore
DEBUG_ALIAS= androiddebugkey
DEBUG_KEYSTORE_PWD= android
DEBUG_KEY_PWD= android

3, buildTypes

注意:
1 .minifyEnabled 会对代码进行混淆和压缩,shrinkResources 会对比R文件对无用资源进行删除
2.minifyEnabled 设置为true时shrinkResources 的设置才会生效

buildTypes {
  release {
    debuggable true
    minifyEnabled true //启用Proguard
    shrinkResources true //是否清理无用资源,依赖于minifyEnabled
    zipAlignEnabled true //是否启用zipAlign压缩
    signingConfig signingConfigs.release
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
  }
  debug {
    debuggable true
    minifyEnabled false  //不启用Proguard
    shrinkResources false //是否清理无用资源,依赖于minifyEnabled
    zipAlignEnabled false //是否启用zipAlign压缩
    signingConfig signingConfigs.debug
  }
}

4,productFlavors

自定义BuildConfig,工程编译后会生成BuildConfig类,该类会包含自定义字段。
假设工程包含beijing和shandong两个productFlavors,且拥有各自不同的服务器等配置信息。

productFlavors{
    beijing{
        buildConfigField("boolean", "IS_LOCAL", "false")
        buildConfigField("String", "SERVER_NAME", "\"BJ\"")
        buildConfigField("String", "SERVER_HOST", "\"http://xxx.xxx.xxx.xx:8080\"")
        buildConfigField("String", "LOGIN_API", "\"login\"")
    }
    shandong{
        buildConfigField("boolean", "IS_LOCAL", "false")
        buildConfigField("String", "SERVER_NAME", "\"SD\"")
        buildConfigField("String", "SERVER_HOST", "\"http://xxx.xxx.xxx.xx:8080\"")
        buildConfigField("String", "LOGIN_API", "\"loginsd\"")
    }
}

5,打包apk重命名

def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
applicationVariants.all { variant ->
     variant.outputs.each { output ->
         def outputFile = output.outputFile
         if (variant.buildType.name.equals('release')) {
              def fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_release.apk"
             if(variant.flavorName.equals("beijing")){
                 fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_beijing_release.apk"
             }else if(variant.flavorName.equals("shandong")){
                 fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_release.apk"
             } 
             output.outputFile = new File(outputFile.parent, fileName)
         }else if(variant.buildType.name.equals('debug')){
             def fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_debug.apk"
             if(variant.flavorName.equals("beijing")){
                 fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_beijing_debug.apk"
             }else if(variant.flavorName.equals("shandong")){
                 fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_debug.apk"
             }
             output.outputFile = new File(outputFile.parent, fileName)
         }
     }
 }

6,dependencies 依赖配置

不同buildTypes 和productFlavors 依赖不同的jar的配置情况

 dependencies {
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:support-v4:22.2.1'
    compile 'com.google.code.gson:gson:2.2.1'
    compile 'net.robinx:lib.blur:1.0.1'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
    compile fileTree(dir: 'libs', exclude:["*mtclib*.jar","*peerconnection*.jar"], include: "*.jar")
    beijingCompile files("libs/mtclib_0908_bj.jar")
    beijingCompile files("libs/libjingle_peerconnection_bj.jar")
    shandongCompile files("libs/mtclib_170110_sd.jar")
    shandongCompile files("libs/libjingle_peerconnection_sd.jar")
}

Github gist:

完整配置参见:

https://gist.github.com/fcf96ba9d265c09694fb24fd5ed34ee7

转:原文

推荐阅读更多精彩内容