No1:

指定共享库

<uses-library
android:name="com.google.android.maps"
android:required="true"/>

No2:

android除了标准的sdk,还存在两种库

1)add-on库:位于add-ons目录下,大部分是第三方厂商或者公司开发的

2)optional可选库:位于platforms/android-xx/optional目录下,一般是为了兼容旧版本的API,比如HttpClient的org.apache.http.legacy

第一种Android Gradle会自动解析并添加到classpath里,第二种就不会

No3:

android{
useLibrary 'org.apache.http.legacy'
}

No4:

批量修改生成的apk文件名

android{
compileSdkVersion 23
buildToolsVersion "23.0.1"
useLibrary 'org.apache.http.legacy' defaultConfig{
applicationId "org.flysnow.app.example92"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes{
release{
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
zipAlignEnabled true
}
}
productFlavors{
google{ }
}
applicationVariants.all{
variant->variant.outputs.each{
output->if(output.outputFile!=null && output.outputFile.name.endsWith('.apk') && 'release'.equals(variant.buildType.name)){
def flavorName = variant.flavorName.startsWith("_")?variant.flavorName.substring(1) : variant.flavorName
def apkFile = new File(output.outputFile.getParent(),"Example92_${flavorName}_v${variant.versionName}_${buildTime()}.apk")
output.outputFile = apkFile
}
}
}
} def buildTime{
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
return formattedDate
}

每一个ApplicationVariant至少有一个或多个outputs输出

No5:

Android支持基于文件的模块化,就是apply from

version.gradle文件

ext{
appVersionCode = 1
appVersionName = "1.0.0"
}

build.gradle文件中引用

apply from: 'version.gradle'

No6:

获取当前的tag名称

git describe --abbrev=0 --tags

No7:

动态从git tag中获取应用的版本名称

def getAppVersionName(){
def stdout = new ByteArrayOutputStream()
exec{
commandLine 'git','describe','--abbrev=0','-tags'
standardOutput = stdout
}
return stdout.toString()
}

调用

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" defaultConfig{
applicationId "org.flysnow.app.example93"
minSdkVersion 14
targetSdkVersion 23
versionCode appVersionCode
versionName getAppVersionName()
}
}

No8:

以git tag的数量作为其版本号

def getAppVersionCode(){
def stdout = new ByteArrayOutputStream()
exec{
commandLine 'git','tag','--list'
standardOutput = stdout
}
return stdout.toString().split("\n").size()
}

调用

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" defaultConfig{
applicationId "org.flysnow.app.example93"
minSdkVersion 14
targetSdkVersion 23
versionCode getAppVersionCode()
versionName getAppVersionName()
}
}

No9:

隐藏签名文件信息(签名文件放在服务器,兼容debug打包)

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" signingConfigs{
def appStoreFile = System.getenv("STORE_FILE")
def appStorePassword = System.getenv("STORE_PASSWORD")
def appKeyAlias = System.getenv("KEY_ALIAS")
def appKeyPassword = System.getenv("KEY_PASSWORD") //当不能从环境变量里获取到签名信息的时候,就使用项目中带的debug签名
if(!appStoreFile||!appStorePassword||!appKeyAlias||!appKeyPassword){
appStoreFile = "debug.keystore"
appStorePassword = "android"
appKeyAlias = "androiddebugkey"
appKeyPassword = "android"
}
release{
storeFile file(appStoreFile)
storePassword appStorePassword
keyAlias appKeyAlias
keyPassword appKeyPassword
}
}
}

No10:

动态配置AndroidManifest文件

ManifestPlaceholders是ProductFlavor的一个属性,是一个Map类型,所以我们可以同时配置很多个占位符

<meta-data android:value="${UMENG_CHANNEL}" android:name="UMENG_CHANNEL">

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" productFlavors{
google{
manifestPlaceholders.put("UMENG_CHANNEL","google")
}
baidu{
manifestPlaceholders.put("UMENG_CHANNEL","baidu")
}
}
}

它会把AndroidManifest文件中所有占位符变量为UMENG_CHANNEL的内容替换为manifestPlaceholders中对应的value的值

缩写

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" productFlavors{
google{}
baidu{}
} productFlavors.all{
flavor->manifestPlaceholders.put("UMENG_CHANNEL",name)
}
}

No11:

自定义你的BuildConfig

public final class BuildConfig{
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "org.flysnow.app.example96";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "baidu";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";
}

获取当前包名

context.getPackageName()
//更方便,性能更快
BuildConfig.APPLICATION_ID

注:DEBUG常量在debug模式下的值是true,在release模式下自动变为false

新增一些常量

public void buildConfigField(@NonNull String type,@NonNull String name,@NonNull String value){}

第一个参数type是要生成字段的类型,第二个参数name是要生成字段的常量名字,第三个参数value是要生成字段的常量值。最终生成字段格式:<type> <name> = <value>

例:安装渠道包的时候打开相应的网页

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" defaultConfig{
applicationId "org.flysnow.app.example96"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName '1.0.0'
} productFlavors{
google{
buildConfigField 'String','WEB_URL','"http://www.google.com"'
}
baidu{
buildConfigField 'String','WEB_URL','"http://www.baidu.com"'
}
}
}

BuildConfig类就会生成相应的常量

public static final String WEB_URL = "http://www.baidu.com";

我们通过BuildConfig.WEB_URL使用即可,在打包的时候,Android Gradle会帮我们自动生成不同的值

No12:

动态添加自定义的资源

res/Values文件夹里可以使用xml方式定义,还可以在我们的Android Gradle中定义,实现这一功能的正是resValue方法

public void resValue(@NonNull String type,@NonNull String name,@NonNull String name,@NonNull String value){
ClassField alreadyPresent = getResValues().get(name);
if(alreadyPresent != null){
String flavorName = getName();
if(BuilderConstants.MAIN.equals(flavorName)){
logger.info("DefaultConfig:resValue '{}' value is being replaced: {} -> {}",name,alreadyPresent.getValue(),value);
}else{
logger.info("ProductFlavor({}):resValue'{}' value is being replaced:{} -> {}",flavorName,name,alreadyPresent.getValue(),value);
}
}
addResValue(AndroidBuilder.createClassField(type,name,value));
}

例:

android{
compileSdkVersion 23
buildToolsVersion "23.0.1"
productFlavors{
google{
resValue 'string','channel_tips','google渠道欢迎你'
}
baidu{
resValue 'string','channel_tips','baidu渠道欢迎你'
}
}
}

生成

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="channel_tips" translatable="false">baidu渠道欢迎你</string>
</resources>

No13:

Java编译选项

compileOptions来对Java编译选项进行配置

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" compileOptions{
encoding = 'utf-8'
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
}
}

注:scourceCompatibility是配置Java源代码的编译级别,比如1.5/1.6这样

targetCompatibility是配置生成的Java字节码的版本

No14:

adb操作选项配置--adbOptions

public class AdbOptions implements com.android.builder.model.AdbOptions{
int timeOutInMs;
List<String> installOptions; @Override
public int getTimeOutInMs(){
return timeOutInMs;
} public void timeOutInMs(int timeOutInMs){
this.timeOutInMs(timeOutInMs);
} public void timeOutInMs(int timeOutInMs){
setTimeOutInMs(timeOutInMs);
} @Override
public Collection<String> getInstallOptions(){
return installOptions;
} public void setInstallOptions(String option){
installOptions = ImmutableList.of(option);
} public void setInstallOptions(String... options){
installOptions = ImmutableList.copyOf(options);
} public void installOptions(String option){
installOptions = ImmutableList.of(option);
} public void installOptions(String... options){
installOptions = ImmutableList.copyOf(options);
}
}

使用

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" adbOptions{
timeOutInMs = 5*1000//秒
installOptions '-r','-s'
}
}

adb install有六个选项:

-l:锁定该应用程序

-r:替换已存在的应用程序,就是强制安装

-t:允许测试包

-s:把应用程序安装到SD卡上

-d:允许进行降级安装,就是安装的程序比手机上带的版本低

-g:为该应用授予所有运行时的权限

No15:

Dex选项配置--dexOptions{}

package com.android.builder.core;
import com.android.annotation.Nullable; public interface DexOptions{
boolean getIncremental();
boolean getPreDexLibraries();
boolean getJumboMode();
@Nullable
String getJavaMaxHeapSize();
@Nullable
Integer getThreadCount();
}

incremental用来配置是否启用dx的增量模式

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" dexOptions{
incremental true
}
}

javaMaxHeapSize配置执行dx命令时为其分配的最大堆内存,主要用来解决执行dx时内存不够用的情况

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" dexOptions{
javaMaxHeapSize '4g'
}
}

jumboMode用来配置是否开启jumbo模式,解决函数数量超过65535个的问题

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" dexOptions{
jumboMode true
}
}

preDexLibraries用来配置是否预执行dex Libraries库工程,开启后会大大提高增量构建的速度,不过可能会影响clean构建的速度

threadCount用来配置Android Gradle运行dx命令时使用的线程数量,适当提高dx的效率

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" dexOptions{
threadCount 2
}
}

No16:

app方法数为什么有65535这个限制?

Dalvik虚拟机在执行DEX文件的时候,它使用了short这个类型来索引DEX文件中的方法,所以单个DEX文件可以被定义的方法最多只能是65535个。

No17:

Android 5.0之后,使用了ART的运行时方式,可以天然支持app有多个DEX文件。ART在安装app的时候执行预编译,把多个DEX文件合并成一个oat文件执行。

No18:

突破65535方法限制

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" defaultConfig{
applicationId "org.flysnow.app.example911"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName '1.0.0'
//启用multidex
multiDexEnabled true
}
}
dependencies{
compile fileTree(dir: 'libs',include:['*.jar'])
compile 'com.android.support:multidex:1.0.1'
}

1)如果没有自定义Application,直接使用MultiDexApplication

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.flysnow.app.example911">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>

2)如果有自定义Application,直接继承MultiDexApplication即可

3)如果自定义Application继承了其他第三方提供的Application,就需要重写attachBaseContext方法

public class Example911Application extends Application{
@Override
protected void attachBaseContext(Context base){
super.attachBaseContext(base);
MultiDex.install(this);
}
}

因为继承MultiDexApplication也是通过重写attachBaseContext方法然后MultiDex.install(this)方法实现的

No19:

自动清理未使用的资源--Resource Shrinking

android{
compileSdkVersion 23
buildToolsVersion "23.0.1" buildTypes{
release{
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}

避免误删--keep(文件位置:res/raw/keep.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"/>

只用中文的语言资源

android{
compileSdkVersion 23
buildToolsRevision "23.0.1" defaultConfig{
applicationId "org.flysnow.app.example912"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName '1.0.0'
resConfigs 'zh'
}
}

动清理资源只是在打包的时候,不打包到apk中,并没有删除工程中的资源

《Gradle权威指南》--Android Gradle高级自定义的更多相关文章

  1. 《Gradle权威指南》--Gradle插件

    No1: 应用插件 apply plugin:'java' apply plugin:org.gradle.api.plugins.JavaPlugin apply plugin:JavaPlugin ...

  2. 《gradle权威指南》--Gradle入门

    No1: Window下搭建Gradle:添加GRADLE_HOME环境变量,然后把GRADLE_HOME\bin添加到PATH系统变量里保存即可.完成后打开CMD,运行gradle -v来验证 No ...

  3. 《Gradle权威指南》--Gradle构建脚本基础

    No1: 设置文件默认名是setting.gradle,放在根目录下,大多数作用都是为了配置子工程 No2: 一个Project包含很多个Task.Task就是一个操作,一个原子性的操作.其实它是Pr ...

  4. 《Gradle权威指南》--Gradle任务

    No1: 多种方式创建任务 def Task ex41CreateTask1 = task(ex41CreateTask1) ex41CreateTask1.doLast{ println " ...

  5. 读书笔记--Android Gradle权威指南(上)

    本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 最近看了一本书<Android Gradle 权威指南>,对于 Gradle 理解又更深了,但不想过段时间就又忘光了,所 ...

  6. 读书笔记--Android Gradle权威指南(下)

    前言 最近看了一本书<Android Gradle 权威指南>,收获挺多,就想着来记录一些读书笔记,方便后续查阅. 本篇内容是基于上一篇:读书笔记--Android Gradle权威指南( ...

  7. Gradle系列之Android Gradle高级配置

    本篇文章主要在之前学习的基础上,从实际开发的角度学习如何对 Android Gradle 来进行自定义以满足不同的开发需求,下面是 Gradle 系列的几篇文章: Gradle系列之初识Gradle ...

  8. Gradle系列之Android Gradle插件

    原文发于微信公众号 jzman-blog,欢迎关注交流. 通过前面几篇文章学习了 Gradle 基础知识以及 Gradle 插件相关的知识,关于 Gradle 及其插件相关知识请先阅读下面几篇文章: ...

  9. Gradle系列之Android Gradle基础配置

    原文发于微信公众号 jzman-blog,欢迎关注交流. 通过前面几篇文章学习了 Gradle 基础知识以及 Gradle 插件相关的知识,关于 Gradle 及其插件相关知识请先阅读下面几篇文章: ...

  10. Android Gradle 构建工具(Android Gradle Build Tools)是什么?

    转载地址:http://mrfu.me/android/2015/07/17/New_Android_Gradle_Build_Tools/ 译者地址:[翻]一览新的 Android Gradle 构 ...

随机推荐

  1. luogu P3960 列队

    传送门 因为\(Splay\)可以\(O(logn)\)维护区间,所以直接对每一行维护第一个元素到倒数第二个元素的\(Splay\),最后一列维护一个\(Splay\),每次把选出来的点删掉,然后把那 ...

  2. django(一)验证码

    这里讲讲在django中使用第三方插件验证码的流程. 一. 先安装pillow, 通过 python -m pip install pillow 二.安装完后,在官方网站上看操作过程.地址:pillo ...

  3. Database学习 - mysql 数据库 多表/复合/子 查询

    多表查询 多表查询,基本规则,通过两表有关联字段的进行条件匹配查询 内连接查询 方式一: SELECT 查看字段名[,查看字段名] FROM 一表名,二表名 WHERE 一/二表.字段 = 一/二表. ...

  4. python 入门基础24 元类、单例模式

    内容目录: 一.元类 二.单例模式 一.元类 1 什么是元类: 源自一句话:在python中,一切皆对象,而对象都是由类实例化得到的 class OldboyTeacher: def __init__ ...

  5. string替换字符串,路径的斜杠替换为下划线

    场景 替换某个路径的所有"\"为"_". 很多时候取证需要把恶意代码文件取回来,然后清除. 如果在D:\WEB\模板制作插件\需要覆盖\CodeColoring ...

  6. DBSCAN密度聚类

    1. 密度聚类概念 DBSCAN(Density-Based Spatial Clustering of Applications with Noise,具有噪声的基于密度的聚类方法)是一种很典型的密 ...

  7. Sql 插入自定义主键

    在遇到数据库设计是自增的主键,且需要插入自定义的主键Id时,这个时候如果直接Insert的话,将会发生错误,错误提示信息: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'XXX' ...

  8. 阿里云服务器搭建FTP

    操作系统:Windows Server 2008 R2企业版. 首先,创建一个用户组:ftpUsers,创建一个用户:ftpAdmin.并将ftpAdmin隶属于ftpUsers组 其次,需要安装ft ...

  9. 安装installshield问题

    install designer中 general information 选择setup languages shortcuts编辑  开始  中显示目录 文件路径 C:\Program Files ...

  10. Project Euler Problem8

    Largest product in a series Problem 8 Find the greatest product of five consecutive digits in the 10 ...