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

来源:互联网 发布:在ubuntu上下载软件 编辑:程序博客网 时间:2024/05/22 03:12

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

配置划分:

  1. defaultConfig 默认配置
  2. signingConfigs 签名信息配置
  3. buildTypes
  4. productFlavors
  5. 打包apk重命名
  6. dependencies 依赖配置

详细配置:

1,defaultConfig 默认配置

defaultConfig {   //项目包名配置   applicationId "com.wildcreek.demo"    minSdkVersion 16   targetSdkVersion 19   versionCode 14   versionName "1.0.14"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2,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']  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

签名配置文件signing.properties:

KEYSTORE_FILE = release.keystoreKEY_ALIAS = wildcreekKEYSTORE_PWD= xxxKEY_PWD= xxxDEBUG_KEYSTORE= debug.keystoreDEBUG_ALIAS= androiddebugkeyDEBUG_KEYSTORE_PWD= androidDEBUG_KEY_PWD= android
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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\"")    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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)         }     } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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")}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Github gist:

完整配置参见:

https://gist.github.com/fcf96ba9d265c09694fb24fd5ed34ee7

阅读全文
0 0