学习GRADLE

来源:互联网 发布:淘宝nike旗舰店真假 编辑:程序博客网 时间:2024/05/17 05:59

gradle

依赖最新版本

GRADLE可以做到不依赖具体某个版本的库,而是每次从repo拉取最新的库到本地做编译。

dependencies {    compile 'com.google.code.gson:gson:2.2.1'}

如果不想依赖具体的库,想每次拉取最新的库,那么,可以写成这样:

dependencies {    compile 'com.google.code.gson:gson:2.2.+'}

还可以这样写:

dependencies {    compile 'com.google.code.gson:gson:+'}

文件编码配置

如果导入一个windows下编写的项目,而代码中有中文注释,采用GBK, GB18030等编码方式时,编译会报错,可以采用如下方式统一项目的编码

allprojects {    repositories {        jcenter()    }    tasks.withType(JavaCompile) {        options.encoding = "UTF-8"    }}

配置签名信息

签名信息属于敏感信息,建议不要写死放到gradle脚本中,而是写到一个单独的配置文件里,而且这个配置文件不要同步到版本管理系统上,而是由本地维护,防止在版本管理平台上泄漏敏感信息。建议签名信息内容写到gradle.properties或者local.properties文件里,这样,gradle脚本可以直接引用,如果是放在一个自定义的文件中,gradle脚本需要提供相应的代码来读取文件的内容。 文件内容参考如下:

RELEASE_KEY_PASSWORD=androidRELEASE_KEY_ALIAS=androidreleasekeyRELEASE_STORE_PASSWORD=androidRELEASE_STORE_FILE=../resources/release.keystoreDEBUG_KEY_PASSWORD=androidDEBUG_KEY_ALIAS=androiddebugkeyDEBUG_STORE_PASSWORD=androidDEBUG_STORE_FILE=../resources/debug.keystore

gradle脚本引用代码参考:

android {    signingConfigs {        debug {            storeFile file(DEBUG_STORE_FILE)            storePassword DEBUG_STORE_PASSWORD            keyAlias DEBUG_KEY_ALIAS            keyPassword DEBUG_KEY_PASSWORD        }        release {            storeFile file(RELEASE_STORE_FILE)            storePassword RELEASE_STORE_PASSWORD            keyAlias RELEASE_KEY_ALIAS            keyPassword RELEASE_KEY_PASSWORD        }    }}

如果签名信息没有放到gradle.properties或者local.properties文件里,那就需要自己写代码读取咯,假设签名信息放在signing.properties文件中, 文件内容可以参考上面,读取文件的代码放入gradle脚本中就可以了,参考代码如下

def File propFile = new File('signing.properties')if (propFile.canRead()) {    def Properties props = new Properties()    props.load(new FileInputStream(propFile))    if (props != null && props.containsKey('RELEASE_STORE_FILE') && props.containsKey('RELEASE_STORE_PASSWORD') &&            props.containsKey('RELEASE_KEY_ALIAS') && props.containsKey('RELEASE_KEY_PASSWORD')) {        android.signingConfigs.release.storeFile = file(props['RELEASE_STORE_FILE'])        android.signingConfigs.release.storePassword = props['RELEASE_STORE_PASSWORD']        android.signingConfigs.release.keyAlias = props['RELEASE_KEY_ALIAS']        android.signingConfigs.release.keyPassword = props['RELEASE_KEY_PASSWORD']        println 'all good to go'    } else {        android.buildTypes.release.signingConfig = null        println 'signing.properties found but some entries are missing'    }} else {    println 'signing.properties not found'    android.buildTypes.release.signingConfig = null}

构建参数设置

AndroidManifest占位符,BuildConfig以及资源配置

根据版本类型和构建变种定义不同的变量值供程序引用

manifestPlaceholders = [APP_KEY:"release"]buildConfigField "String", "EMAIL", "\"release@android.studio.com\""resValue "string", "content_main", "Hello world from release!"

buildConfigField支持Java中基本数据类型,如果是字符串,记得加转义后的双引号 resValue支持res/values下的资源定义,字符串无需加转义后的双引号

删除unaligned apk

 applicationVariants.all { variant ->                variant.outputs.each { output ->                    def outputFile = output.outputFile                    if (outputFile != null && outputFile.name.endsWith('.apk')) {                        // 输出apk名称为yujia_v1.0_wandoujia.apk                        def fileName = "yujia_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"                        output.outputFile = new File(outputFile.parent, fileName)                    }                    if (output.zipAlign != null) {                        output.zipAlign.doLast {                            output.zipAlign.inputFile.delete()                        }                    }                }            }

lint选项开关
lint会按默认选项会做严格检查,遇到包错误会终止构建过程,可以用如下开关关掉这个选项,不过最好是重视下lint的输出,有问题及时修复,避免潜在问题

android {    lintOptions {        abortOnError false    }}

依赖库按构建目标定制
不同的依赖库可以按构建目标做定制,比如freemium的变种集成了广告,就可以这样写

dependencies {freemiumCompile 'com.google.android.gms:ads:7.8.0'
0 0
原创粉丝点击