Android Gradle使用详解

来源:互联网 发布:wear软件下载 编辑:程序博客网 时间:2024/06/06 00:51

转自:http://blog.csdn.net/qq_19711823/article/details/51351120


现在Android开发已经基本向Android Studio看齐,既然使用Android Studio开发,就必须掌握Gradle的使用,使用Gradle打包apk已经成为当前主流趋势,方便了开发者进行构建不同的应用版本,以完成不同的需求。 
一、统一配置项目属性 
1.Android配置

android {    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION    defaultConfig {        minSdkVersion project.ANDROID_BUILD_MIN_SDK_VERSION        targetSdkVersion project.ANDROID_BUILD_TARGET_SDK_VERSION        versionCode Integer.parseInt(project.VERSION_CODE)        versionName project.VERSION_NAME    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

如上述代码中所述: 
上面两句分别配置了项目的Sdk版本和编译Tools版本,在下面的defaultConfig 中配置了minSdkVersion 、targetSdkVersion 、versionCode 和versionName,相信大家这些都看得懂这些名词的意思,然后看他们对应的值,project.VERSION_NAME,这个又是在哪设置的呢,看下面:

VERSION_NAME=1.0.0VERSION_CODE=100ANDROID_BUILD_TARGET_SDK_VERSION=23ANDROID_BUILD_MIN_SDK_VERSION=14ANDROID_BUILD_TOOLS_VERSION=23.0.2ANDROID_BUILD_SDK_VERSION=23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这些属性在项目的gradle.properties中设置

2.版本配置

android{    buildTypes {        debug {            buildConfigField "boolean", "LOG_DEBUG", "true"         }        release {            buildConfigField "boolean", "LOG_DEBUG", "false"            //混淆开关            minifyEnabled true            //是否zip对齐            zipAlignEnabled true            //是否打开debuggable开关            debuggable false            //是否打开jniDebuggable开关            jniDebuggable false            // 移除无用的resource文件            shrinkResources true        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

可以看到有配置debug开关,混淆之类的,都是可统一配置的。

3.渠道配置

productFlavors {        DemoTest {            buildConfigField "boolean", "URL_PRODUCT", "false"            applicationId "cn.example.demo.debug"            manifestPlaceholders = [                    GETUI_APP_ID    : project.GETUI_APP_ID,                    GETUI_APP_KEY   : project.GETUI_APP_KEY,                    GETUI_APP_SECRET: project.GETUI_APP_SECRET,                    PACKAGE_NAME    : applicationId            ]        }        DemoProduct {            buildConfigField "boolean", "URL_PRODUCT", "true"            applicationId "cn.example.demo"            manifestPlaceholders = [                    GETUI_APP_ID    : project.GETUI_APP_ID_RELEASE,                    GETUI_APP_KEY   : project.GETUI_APP_KEY_RELEASE,                    GETUI_APP_SECRET: project.GETUI_APP_SECRET_RELEASE,                    PACKAGE_NAME    : applicationId            ]        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

这里就是配置了不同版本的渠道名,渠道的配置还是跟Android配置一样,在gradle.properties中

#个推debug appid信息GETUI_APP_ID=sssssssssssssssssssssGETUI_APP_KEY=sssssssssssssssssssssGETUI_APP_SECRET=sssssssssssssssssssss#个推release版本appid 信息GETUI_APP_ID_RELEASE=xxxxxxxxxxxxxxxxxxxGETUI_APP_KEY_RELEASE=xxxxxxxxxxxxxxxxxxxGETUI_APP_SECRET_RELEASE=xxxxxxxxxxxxxxxxxxx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.签名信息配置 
对于签名相关的信息,直接写在gradle当然不好,特别是一些开源项目,可以添加到gradle.properties:

RELEASE_KEY_PASSWORD=xxxxRELEASE_KEY_ALIAS=xxxRELEASE_STORE_PASSWORD=xxxRELEASE_STORE_FILE=../.keystore/xxx.jks
  • 1
  • 2
  • 3
  • 4

然后在build.gradle中引用即可:

android {    signingConfigs {        release {            storeFile file(RELEASE_STORE_FILE)            storePassword RELEASE_STORE_PASSWORD            keyAlias RELEASE_KEY_ALIAS            keyPassword RELEASE_KEY_PASSWORD        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5.自定义导出的APK名称配置

android {    // rename the apk with the version name    applicationVariants.all { variant ->        variant.outputs.each { output ->            output.outputFile = new File(                    output.outputFile.parent,                    "ganchai-${variant.buildType.name}-${variant.versionName}-                                                                      ${variant.productFlavors[0].name}.apk".toLowerCase())        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

二、混淆 
混淆能让反编译的代码可读性变的很差,防止别人盗取代码,而且还能显著的减少APK包的大小。 
1.第一个技巧 
首先,除了默认的混淆配置(android-sdk/tools/proguard/proguard-android.txt), 自己的代码肯定是要自己配置的:

## 位于module下的proguard-rules.pro############################################## 主程序不能混淆的代码 ##############################################-dontwarn com.example.**-keep class com.example.** { *; }################################################ 不优化泛型和反射 ###############################################-keepattributes Signature
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

接下来是麻烦的第三方库,一般来说,如果是极光推的话,它的包名是cn.jpush, 添加如下代码即可:

-dontwarn cn.jpush.**-keep class cn.jpush.** { *; }
  • 1
  • 2

其他的第三库也是如此,或者把这些第三方库反编译出来,通过包名去混淆。

三、动态设置一些额外信息 
假如想把当前的编译时间、编译的机器、最新的commit版本添加到apk,而这些信息又不好写在代码里,强大的gradle给了我们机会:

android {    defaultConfig {        resValue "string", "build_time", buildTime()        resValue "string", "build_host", hostName()        resValue "string", "build_revision", revision()    }}def buildTime() {    return new Date().format("yyyy-MM-dd HH:mm:ss")}def hostName() {    return System.getProperty("user.name") + "@" + InetAddress.localHost.hostName}def revision() {    def code = new ByteArrayOutputStream()    exec {        commandLine 'git', 'rev-parse', '--short', 'HEAD'        standardOutput = code    }    return code.toString()}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

述代码实现了动态的添加了3个字符串资源: build_time、build_host、build_revision, 然后在其他地方可像如引用字符串一样使用如下:

// 在Activity里调用getString(R.string.build_time)  // 输出build时间getString(R.string.build_host)  // 输出电脑的用户名和PC名getString(R.string.build_revision) // 输出最后一次commit
  • 1
  • 2
  • 3
  • 4

四、小结 

android打包因为groovy语言的强大,变的强大的同时必然也变的复杂。Gradle还是很复杂的,目前只是会使用而已。。