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

来源:互联网 发布:比尔盖茨编程水平 编辑:程序博客网 时间:2024/06/05 05:25

配置划分:

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

详细配置:

1,defaultConfig 默认配置

?
1
2
3
4
5
6
7
8
defaultConfig {
   //项目包名配置
   applicationId"com.wildcreek.demo"
   minSdkVersion16
   targetSdkVersion19
   versionCode14
   versionName"1.0.14"
}

2,signingConfigs 签名信息配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

?
1
2
3
4
5
6
7
8
9
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
2
3
4
<code>   注意:
  1.minifyEnabled 会对代码进行混淆和压缩,shrinkResources 会对比R文件对无用资源进行删除
  2.minifyEnabled 设置为true时shrinkResources 的设置才会生效
</code>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<code>buildTypes {
  release {
    debuggabletrue
    minifyEnabledtrue//启用Proguard
    shrinkResourcestrue//是否清理无用资源,依赖于minifyEnabled
    zipAlignEnabledtrue//是否启用zipAlign压缩
    signingConfig signingConfigs.release
    proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.txt'
  }
  debug {
    debuggabletrue
    minifyEnabledfalse //不启用Proguard
    shrinkResourcesfalse//是否清理无用资源,依赖于minifyEnabled
    zipAlignEnabledfalse//是否启用zipAlign压缩
    signingConfig signingConfigs.debug
  }
}</code>

4,productFlavors

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

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

5,打包apk重命名

?
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
<code>def releaseTime() {
    returnnew 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"
             }elseif(variant.flavorName.equals("shandong")){
                 fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_release.apk"
             }
             output.outputFile = newFile(outputFile.parent, fileName)
         }elseif(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"
             }elseif(variant.flavorName.equals("shandong")){
                 fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_debug.apk"
             }
             output.outputFile = newFile(outputFile.parent, fileName)
         }
     }
 }</code>

6,dependencies 依赖配置

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

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

Github gist:

阅读全文
0 0
原创粉丝点击