gradle笔记


[TOC]

腾讯内网镜像

包含android sdk studio gradle等各种资源的镜像站点,非常值的仔细看.

介绍的比较好的一篇内网文章,下面很多是参考这篇文章的.

Android sdk内网设定

内网设置可以有很多种,选择其中一种就可以了

  1. 外网也能使用通过bugly的代理, 内网, 外网
  2. 开发网代理 dev-proxy.oa.com:8080,添加内部的addon:http://android.oa.com/android/repository/addon.xml

Android studio内网设定

android的编译环境是配置文件化(脚本化的),也就是通过gui设置的选项都会在相应的脚本上有对应的操作,同理修改了脚本,gui的设置里也会对应的改变.新创建工程时,AS 会给脚本赋默认的值.

andriod studio升级设置

设置android studio自身升级时的代理,比如android studio 2.1 -> android studio 2.2

添加

-Djava.net.preferIPv4Stack=true
-Didea.updates.url=http://android.oa.com/AndroidStudio/patches/updates.xml
-Didea.patches.url=http://android.oa.com/AndroidStudio/patches/

添加这些后通过help里的检查版本更新就会通过上面指定的地址下载了,速度快不少

android studio IDE proxy设置

IDE在settings里面设置,见下面的截图 在这里设置了代理有什么作用呢

1.每次新建工程时会把代理设置添加到工程的gradle.properties里面

如果设置了上面截图中的代理(实际是),在编译过程中gradle版本的下载,插件的下载都会通过该代理,如果设置了gradle-wrapper.properties的distributionUrl=http\://android.oa.com/gradle/gradle-2.14.1-all.zip和build.gradle中的maven url:http://maven.oa.com/nexus/content/groups/androidbuild 上图中的代理也就是gradle.properties里面的不要设置,弹出的对话框选 cancel.

2.如果gradle还没缓存会通过gradle-wrapper.properties的distributionUrl下载,如果设置了代理,会通过该代理下载.

android studio sdk repository设定(sdk升级)

sdk support repository和gradle中repository的关系

通过下面的代码验证

task listrepos << {
    println "app Repositories:"
    project.repositories.each { println "Name: " + it.name + "; url: " + it.url }
}

在project的build.gradle中添加上述代码并执行后的结果

:listrepos
project Repositories:
Name: maven; url: http://maven.oa.com/nexus/content/repositories/android
:app:listrepos
app Repositories:
Name: maven; url: http://maven.oa.com/nexus/content/repositories/android
Name: maven2; url: file:/D:/Android/sdk/extras/android/m2repository/

可见android 的插件会把sdk目录下的extras/android/m2repository/目录加入到local repository里面,这样下面的依赖就会使用sdk中的插件

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}

gradle内网设定

在AS中,gradle自身的版本是会动态更新的,也可以手动的选择,除了gradle之外,gradle的插件也版本和更新机制,还有就是编译过程的依赖库也都是从maven上更新的,所以相应的网络设定也涉及到了这三个部分.

gradle新版本设定

AS会根据设定的版本(File->project struct->project和gradle-wrapper.properties的distributionUrl)去下载对应的gradle版本,一般存储在$home/.gradle/wrapper/dists目录下,同一版本下面还会有用sha1表示的不同目录 这是根据distributionUrl取sha1的hash得到的,这样不同地方下载的同样版本的gradle不会冲突,参加下面的介绍,这个hash算法版本有bug,会导致重复下载.

在GUI中设置了版本会同时修改gralde-wrapper.proprieties对应的文件内容,如果修改了这个文件,在sync时GUI中的settings也会跟着变,官方文档.

gradlew 使用本地的版本

在初次安装android studio初次编译的时候会下载上面设置的gradle的版本,下载地址就是gralde-wrapper.proprieties文件中指定的地址distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip

E:\code\android\ImmersiveMode>gradlew
Downloading file:/E:/code/android/ImmersiveMode/gradle/wrapper/gradle-2.14.1-all.zip
.....................................................................................................
.....................................................................................................
.................
Unzipping C:\Users\timfeng\.gradle\wrapper\dists\gradle-2.14.1-all\a0pkj0448d84bf3tiedrih2al\gradle-2
.14.1-all.zip to C:\Users\timfeng\.gradle\wrapper\dists\gradle-2.14.1-all\a0pkj0448d84bf3tiedrih2al

这个地方有个坑,虽然android studio和gradlew都使用相同的gradle版本缓存目录,上面的C:\Users\timfeng.gradle\wrapper\dists, 但是即使相同的版本,二进制完全一致,也会导致重复下载多次,这个是bug. 考虑到每次下载gradle很费时间,可以用这个方法规避.

Go into the gradle download location C:\Users\Sojimaxi.gradle\wrapper\dists\gradle-2.10-all\78v82fsf226usgvgh7q2ptcvif copy your own copy of gradle-2.10-bin.zip into that directory then delete the gradle-2.10-all.zip.part in that directory.

"D:\Program Files\Java\jdk1.8.0_25\bin\java" -Dgradle.home=C:\Users\timfeng\.gradle\wrapper\dists\gradle-2.14.1-all\8bnwg5hd3w55iofp58khbp6yv\gradle-2.14.1 
"-Dtools.jar=D:\Program Files\Java\jdk1.8.0_25\lib\tools.jar" -Didea.launcher.port=7532 
-Didea.launcher.bin.path=D:\Android\studio\bin -Dfile.encoding=UTF-8 
-classpath C:\Users\timfeng\.gradle\wrapper\dists\gradle-2.14.1-all\8bnwg5hd3w55iofp58khbp6yv\gradle-2.14.1\lib\groovy-all-2.4.4.jar;
C:\Users\timfeng\.gradle\wrapper\dists\gradle-2.14.1-all\8bnwg5hd3w55iofp58khbp6yv\gradle-2.14.1\lib\ant-1.9.6.jar;
.....
C:\Users\timfeng\.gradle\wrapper\dists\gradle-2.14.1-all\8bnwg5hd3w55iofp58khbp6yv\gradle-2.14.1\lib\plugins\ivy-2.2.0.jar;
D:\Android\studio\lib\idea_rt.jar com.intellij.rt.execution.application.AppMain org.gradle.launcher.GradleMain 
--build-file E:\code\android\testAS2\build.gradle printBuildScriptClasspath
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html.
Download http://maven.oa.com/nexus/content/repositories/android/org/jacoco/org.jacoco.core/0.7.4.201502262128/org.jacoco.core-0.7.4.201502262128.pom
Download http://maven.oa.com/nexus/content/repositories/android/org/jacoco/org.jacoco.build/0.7.4.201502262128/org.jacoco.build-0.7.4.201502262128.pom
Download http://maven.oa.com/nexus/content/repositories/android/org/ow2/asm/asm-debug-all/5.0.1/asm-debug-all-5.0.1.pom
Download http://maven.oa.com/nexus/content/repositories/android/org/ow2/asm/asm-parent/5.0.1/asm-parent-5.0.1.pom
Download http://maven.oa.com/nexus/content/repositories/android/org/jacoco/org.jacoco.core/0.7.4.201502262128/org.jacoco.core-0.7.4.201502262128.jar
Download http://maven.oa.com/nexus/content/repositories/android/org/ow2/asm/asm-debug-all/5.0.1/asm-debug-all-5.0.1.jar
Incremental java compilation is an incubating feature.
:printBuildScriptClasspath
C:\Users\timfeng\.gradle\caches\modules-2\files-2.1\com.android.tools.build\gradle\2.1.0\6d96b336cb51a348725e6c366aefd74682dce465\gradle-2.1.0.jar;
....
C:\Users\timfeng\.gradle\caches\modules-2\files-2.1\commons-logging\commons-logging\1.1.1\5043bfebc3db072ed80fbd362e7caf00e885d8ae\commons-logging-1.1.1.jar;
C:\Users\timfeng\.gradle\caches\modules-2\files-2.1\commons-codec\commons-codec\1.4\4216af16d38465bbab0f3dff8efa14204f7a399a\commons-codec-1.4.jar;
C:\Users\timfeng\.gradle\caches\modules-2\files-2.1\com.android.tools.external.lombok\lombok-ast\0.2.3\528b6f8bde3157f17530aa366631f2aad2a6cf9\lombok-ast-0.2.3.jar;
C:\Users\timfeng\.gradle\caches\modules-2\files-2.1\org.abego.treelayout\org.abego.treelayout.core\1.0.1\e31e79cba7a5414cf18fa69f3f0a2cf9ee997b61\org.abego.treelayout.core-1.0.1.jar

BUILD SUCCESSFUL

Total time: 6.726 secs

Process finished with exit code 0

在内网情况下,每次去distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip下载对应的软件包是很费时间的(需要设置代理),腾讯内部有自己的镜像,使用内部镜像速度快很多,强烈建议设置成内部镜像.

修改办法很简单修改gralde-wrapper.proprieties中的distributionUrl,修改为内网的distributionUrl="http://android.oa.com/gradle/gradle-\(gradle.gradleVersion-all.zip",替换\)gradle.gradleVersion为实际使用的版本号.

这样每新建一个工程就修改一次的办法虽然可行,但很麻烦. 修改android studio创建新工程的模板是最好的办法,但没找到位置,网上搜了下也没找到,给google提了个issue也没人理.

参考这篇文章,可以通过在模板中创建一个创建wapper的task来更新distributionUrl. 修改android studio安装目录下的plugins\android\lib\templates\gradle-projects\NewAndroidProject\root\build.gradle.ftl添加下面的task

task wrapper(type: Wrapper) {
    description = 'Generates Gradle wrapper files.'
    group = 'build setup'
    distributionUrl = "http://android.oa.com/gradle/gradle-$gradle.gradleVersion-all.zip"
}

这样每次新建一个工程之后需要执行下这个task,通过这个task更新distributionUrl,其实每次手动的改下也不麻烦,放到模板里,方便查找地址.

gradle插件设定

gradle在编的过程中会下载对应的插件,比如编译android会下载android的插件,编译java会下载java的插件.既然会下载肯定也会有代理的问题.

这里的代理,可以通过IDE的 来设置,实质是修改gradle.properties,官网介绍

# Project-wide Gradle settings.
...

systemProp.http.proxyHost=proxy.company.com
systemProp.http.proxyPort=443
systemProp.http.proxyUser=username
systemProp.http.proxyPassword=password
systemProp.http.auth.ntlm.domain=domain

systemProp.https.proxyHost=proxy.company.com
systemProp.https.proxyPort=443
systemProp.https.proxyUser=username
systemProp.https.proxyPassword=password
systemProp.https.auth.ntlm.domain=domain

对于内网来说使用标准的jcenter()不是一个好选择,即使代理设置对了也很慢.使用内部的镜像才是正确的姿势,使用内部镜像就不需要设置代理了. 参见下面的build.gralde的修改,把jcenter()替换为内网的maven仓库.

buildscript {
    repositories {
        //jcenter()
        maven {
            url 'http://maven.oa.com/nexus/content/groups/androidbuild'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

同理可以把这个加入到创建工程模板中,推荐修改plugins\android\lib\templates\gradle-projects\NewAndroidProject\global.xml.ftl这个文件修改 <global id="mavenUrl" value="http://maven.oa.com/nexus/content/groups/androidbuild" /></globals>

oa上有两个不同的maven地址实测上面用的这个要比较全,更新及时.

另一个maven地址

gradle依赖设定

上面说了gradle插件之类的依赖如何处理,插件下载完就是实质的编译阶段了,这时候就涉及到工程依赖的库了,也就是dependence下载的设置了,同理会有代理镜像之类的,和插件的设置完全一致. 是同一套maven的库,也应该加入到模板里.

allprojects {
    repositories {
        //jcenter()
        maven {
            url 'http://maven.oa.com/nexus/content/groups/androidbuild'
        }
    }
}

从最开始gradle,gradle的插件,gradle的dependence,这些都会在本地缓存下,缓存的目录一般是user home下的.gradle目录.

  • gradle缓存在了.gradle\wrapper\dists
  • 插件和dependence缓存在了.gradle\caches\modules-2\files-2.1\

编译工具(脚本)的作用

一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定, 哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作, 因为 makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。

编译脚本主要需要明确下面几个点:

  1. 如何编译源文件,使用什么命令什么参数(release debug),编译结果命名存放位置
  2. 编译文件的依赖关系,时间戳管理(增量编译)
  3. 本地外地依赖库头文件的位置,链接方式
  4. 目标文件的生成(发布)方式
  5. 其他辅助命令,如clean
edit : main.o kbd.o command.o display.o \
       insert.o search.o files.o utils.o
        cc -o edit main.o kbd.o command.o display.o \
                   insert.o search.o files.o utils.o

main.o : main.c defs.h
        cc -c main.c
kbd.o : kbd.c defs.h command.h
        cc -c kbd.c
command.o : command.c defs.h command.h
        cc -c command.c
display.o : display.c defs.h buffer.h
        cc -c display.c
insert.o : insert.c defs.h buffer.h
        cc -c insert.c
search.o : search.c defs.h buffer.h
        cc -c search.c
files.o : files.c defs.h buffer.h command.h
        cc -c files.c
utils.o : utils.c defs.h
        cc -c utils.c
clean :
        rm edit main.o kbd.o command.o display.o \
           insert.o search.o files.o utils.o
    objects = foo.o bar.o

    all: $(objects)

    $(objects): %.o: %.c
            $(CC) -c $(CFLAGS) $< -o $@

https://developer.android.com/images/tools/studio/build-process_2x.png

http://tools.android.com/tech-docs/new-build-system/build-workflow

gradle简介

gradle是基于groovy的

Groovy是Java平台上设计的面向对象编程语言。这门动态语言拥有类似Python、Ruby和Smalltalk中的一些特性,可以作为Java平台的脚本语言使用。 Groovy的语法与Java非常相似,以至于多数的Java代码也是正确的Groovy代码。Groovy代码动态的被编译器转换成Java字节码。由于其运行在JVM上的特性,Groovy可以使用其他Java语言编写的库。 gradle中的代码段都是groovy的代码,像文件操作等等都可以查询groovy的文档.

很重要的一个groovy特性是闭包closure,一定要理解closure和delegate对象,这个在build script中大量的用到.

gradle是基于插件的

Gradle是一个框架,作为一个框架,它负责定义流程和规则。而具体的编译工作则是通过插件的方式完成的。 比如编译Java有Java插件,编译Groovy有Groovy插件,编译Android APP有Android APP插件,编译Android Library有Android Library插件. gradle本身有个版本号,gradle的插件也有自己独立的版本号,不要搞混.

类似app store, python的pip install, gradle对插件的支持是集成在语言内部,gradle兼容是maven的插件仓库,有集中放置插件的仓库像常用的jcenter mavencentral,简单的通过内置函数就可以指定使用的repository.

group:name:version

Android studio中gradle的设置

android studio可以设置默认的gradle配置,也可以针对特定工程设定gradle的配置.

android gradle settings

设置菜单

设置菜单

默认设置 默认设置

特定工程设置 特定工程设置

gradle基础

使用gradle编译的工程常见的编译文件有下面几种,这些配置文件通过script类来解析用来configure(delegate)一些gradle的对象.Script提供了很对有用的函数.

  • init.gradle 对应 Gradle对象,在整个执行过程中,只有这么一个对象。Gradle对象的数据类型就是 Gradle. 我们一般很少去定制这个默认的配置脚本.
  • build.gradle 对应 Project对象,每一个 build.gradle 会转换成一个 Project 对象.
  • settings.gradle 对应 Settings对象

gradle执行过程,生命周期

这个图很重要,需要记住,build.gradle里的条语句都要清楚是在里面的那个阶段执行,参见这个例子主要是configure和execute阶段的区别,官方说明.

  • Initialization 阶段,执行init.gradle构建Gradle对象.执行Setttings.gradle,确定有几个project
  • Configuration 阶段,解析每个 project 中的 build.gradle。比如 multi-project build 例子中,解析每个子目录中的 build.gradle,确定task的依赖关系图,除了task的action,脚本中的大部分都会在这个阶段被执行.
  • Execution 阶段,根据指定的task(gradle xxtask)和该task的依赖关系,按顺序执行每个task.

gradle project

project和taskGradle里面的所有东西都基于这两个概念。project通常指一个项目,而task指构建过程中的任务。一次构建可以有1到n个project,每个project有1到n个task。

一个project会依赖一些dependencies来工作,也会有产生一些artifacts,供其他工程使用.

上面常见的build的代码应该如何查找文档呢,比如url是从哪里来的,除了这个还有什么类似的变量?

除了maven仓库还能接受什么其他的仓库呢?

可以看到还能接受本地、ivy的repo.从上面看DSL语言是很依赖文档的,需要多查文档才能熟起来.这是一个好的文档入口.

dependency

关于dependency读下官方的这个这个文档基本覆盖了所有的repository和dependencies 的问题.

  • External module dependency A dependency on an external module in some repository.
  • Project dependency A dependency on another project in the same build.
  • File dependency A dependency on a set of files on the local filesystem.
  • Client module dependency A dependency on an external module, where the artifacts are located in some repository but the module meta-data is specified by the local build. You use this kind of dependency when you want to override the meta-data for the module.
  • Gradle API dependency A dependency on the API of the current Gradle version. You use this kind of dependency when you are developing custom Gradle plugins and task types.
  • Local Groovy dependency A dependency on the Groovy version used by the current Gradle version. You use this kind of dependency when you are developing custom Gradle plugins and task types.

在buildscript中的gradle运行时需要依赖的,比如插件的jar,编译时用到其他的功能,比如json markdown等;在build.gradle下面的dependencies是编译对象所需要的,会分成不同的组,一般是插件定义的.

buildscript一般只用classpath这一个组(configure),表示需要把被依赖的库加到classpath中,如果本地没有回去repository里找,然后下载到本地缓存. 使用gradle buildEnvironment的命令查看所有bulidscrpit的依赖

E:\code\gradle\test>gradle buildEnvironment
:buildEnvironment

------------------------------------------------------------
Root project
------------------------------------------------------------

classpath
\--- com.android.tools.build:gradle:2.2.0
     \--- com.android.tools.build:gradle-core:2.2.0
          +--- com.android.tools.build:builder:2.2.0
          |    +--- com.android.tools.build:builder-model:2.2.0
          |    |    \--- com.android.tools:annotations:25.2.0
          |    +--- com.android.tools.build:builder-test-api:2.2.0
          |    |    \--- com.android.tools.ddms:ddmlib:25.2.0
          |    |         +--- com.android.tools:common:25.2.0
          |    |         |    +--- com.android.tools:annotations:25.2.0
          |    |         |    \--- com.google.guava:guava:18.0
          |    |         \--- net.sf.kxml:kxml2:2.3.0
        ....
          +--- net.sf.jopt-simple:jopt-simple:4.9
          +--- com.google.protobuf:protobuf-java:2.5.0
          \--- org.antlr:antlr:3.5.2
               +--- org.antlr:antlr-runtime:3.5.2
               \--- org.antlr:ST4:4.0.8
                    \--- org.antlr:antlr-runtime:3.5.2

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL

对于编译用到的dependencies取决于插件了,比如下面java插件的

dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'
    testCompile 'junit:junit:4.12'
}
E:\code\gradle\test>gradle dependencies
archives - Configuration for archive artifacts.
No dependencies

compile - Dependencies for source set 'main'.
Download http://maven.oa.com/nexus/content/groups/androidbuild/org/slf4j/slf4j-api/1.7.21/slf4j-api-1.7.21.pom
Download http://maven.oa.com/nexus/content/groups/androidbuild/org/slf4j/slf4j-parent/1.7.21/slf4j-parent-1.7.21.pom
\--- org.slf4j:slf4j-api:1.7.21

compileClasspath - Compile classpath for source set 'main'.
\--- org.slf4j:slf4j-api:1.7.21

compileOnly - Compile dependencies for source set 'main'.
\--- org.slf4j:slf4j-api:1.7.21

default - Configuration for default artifacts.
\--- org.slf4j:slf4j-api:1.7.21

runtime - Runtime dependencies for source set 'main'.
\--- org.slf4j:slf4j-api:1.7.21

testCompile - Dependencies for source set 'test'.
+--- org.slf4j:slf4j-api:1.7.21
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

testCompileClasspath - Compile classpath for source set 'test'.
+--- org.slf4j:slf4j-api:1.7.21
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

testCompileOnly - Compile dependencies for source set 'test'.
+--- org.slf4j:slf4j-api:1.7.21
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

testRuntime - Runtime dependencies for source set 'test'.
+--- org.slf4j:slf4j-api:1.7.21
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

BUILD SUCCESSFUL

那么android的gradle插件定义了那些configure呢,文档.

The compile configuration is used to compile the main application. Everything in it is added to the compilation classpath and also packaged in the final APK. There are other possible configurations to add dependencies to:

  • compile: main application
  • androidTestCompile: test application
  • debugCompile: debug Build Type
  • releaseCompile: release Build Type.

使用下面的build.gradle可以打出所有的configure看下

buildscript {
    repositories {
        //jcenter()
        maven {
            url 'http://maven.oa.com/nexus/content/groups/androidbuild'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'

    }
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
        applicationId "com.example.timfeng.testas3"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}

输出的结果节选为

gradle  dependencies
apk - Classpath packaged with the compiled 'main' classes.
No dependencies

archives - Configuration for archive artifacts.
No dependencies

compile - Classpath for compiling the main sources.
\--- com.android.support:appcompat-v7:24.2.1
     +--- com.android.support:support-v4:24.2.1
     |    +--- com.android.support:support-compat:24.2.1
     |    |    \--- com.android.support:support-annotations:24.2.1
     |    +--- com.android.support:support-media-compat:24.2.1
     |    |    \--- com.android.support:support-compat:24.2.1 (*)
     |    +--- com.android.support:support-core-utils:24.2.1
     |    |    \--- com.android.support:support-compat:24.2.1 (*)
     |    +--- com.android.support:support-core-ui:24.2.1
     |    |    \--- com.android.support:support-compat:24.2.1 (*)
     |    \--- com.android.support:support-fragment:24.2.1
     |         +--- com.android.support:support-compat:24.2.1 (*)
     |         +--- com.android.support:support-media-compat:24.2.1 (*)
     |         +--- com.android.support:support-core-ui:24.2.1 (*)
     |         \--- com.android.support:support-core-utils:24.2.1 (*)
     +--- com.android.support:support-vector-drawable:24.2.1
     |    \--- com.android.support:support-compat:24.2.1 (*)
     \--- com.android.support:animated-vector-drawable:24.2.1
          \--- com.android.support:support-vector-drawable:24.2.1 (*)
apk - Classpath packaged with the compiled 'main' classes.
No dependencies

archives - Configuration for archive artifacts.
No dependencies

compile - Classpath for compiling the main sources.
\--- com.android.support:appcompat-v7:24.2.1
     +--- com.android.support:support-v4:24.2.1
     |    +--- com.android.support:support-compat:24.2.1
     |    |    \--- com.android.support:support-annotations:24.2.1
     |    +--- com.android.support:support-media-compat:24.2.1
     |    |    \--- com.android.support:support-compat:24.2.1 (*)
     |    +--- com.android.support:support-core-utils:24.2.1
     |    |    \--- com.android.support:support-compat:24.2.1 (*)
     |    +--- com.android.support:support-core-ui:24.2.1
     |    |    \--- com.android.support:support-compat:24.2.1 (*)
     |    \--- com.android.support:support-fragment:24.2.1
     |         +--- com.android.support:support-compat:24.2.1 (*)
     |         +--- com.android.support:support-media-compat:24.2.1 (*)
     |         +--- com.android.support:support-core-ui:24.2.1 (*)
     |         \--- com.android.support:support-core-utils:24.2.1 (*)
     +--- com.android.support:support-vector-drawable:24.2.1
     |    \--- com.android.support:support-compat:24.2.1 (*)
     \--- com.android.support:animated-vector-drawable:24.2.1
          \--- com.android.support:support-vector-drawable:24.2.1 (*)

android 插件的配置

gradle的实际编译过程,很大程度都是由使用的插件决定的,android插件的user guide,对应的DSL文档.

在android{}中可以使用的变量

sourceset

gradle  sourceSets
Project :app
------------------------------------------------------------

androidTest
-----------
Compile configuration: androidTestCompile
build.gradle name: android.sourceSets.androidTest
Java sources: [app\src\androidTest\java]
Manifest file: app\src\androidTest\AndroidManifest.xml
Android resources: [app\src\androidTest\res]
Assets: [app\src\androidTest\assets]
AIDL sources: [app\src\androidTest\aidl]
RenderScript sources: [app\src\androidTest\rs]
JNI sources: [app\src\androidTest\jni]
JNI libraries: [app\src\androidTest\jniLibs]
Java-style resources: [app\src\androidTest\resources]

debug
-----
Compile configuration: debugCompile
build.gradle name: android.sourceSets.debug
Java sources: [app\src\debug\java]
Manifest file: app\src\debug\AndroidManifest.xml
Android resources: [app\src\debug\res]
Assets: [app\src\debug\assets]
AIDL sources: [app\src\debug\aidl]
RenderScript sources: [app\src\debug\rs]
JNI sources: [app\src\debug\jni]
JNI libraries: [app\src\debug\jniLibs]
Java-style resources: [app\src\debug\resources]

main
----
Compile configuration: compile
build.gradle name: android.sourceSets.main
Java sources: [app\src\main\java]
Manifest file: app\src\main\AndroidManifest.xml
Android resources: [app\src\main\res]
Assets: [app\src\main\assets]
AIDL sources: [app\src\main\aidl]
RenderScript sources: [app\src\main\rs]
JNI sources: [app\src\main\jni]
JNI libraries: [app\src\main\jniLibs]
Java-style resources: [app\src\main\resources]

release
-------
Compile configuration: releaseCompile
build.gradle name: android.sourceSets.release
Java sources: [app\src\release\java]
Manifest file: app\src\release\AndroidManifest.xml
Android resources: [app\src\release\res]
Assets: [app\src\release\assets]
AIDL sources: [app\src\release\aidl]
RenderScript sources: [app\src\release\rs]
JNI sources: [app\src\release\jni]
JNI libraries: [app\src\release\jniLibs]
Java-style resources: [app\src\release\resources]

test
----
Compile configuration: testCompile
build.gradle name: android.sourceSets.test
Java sources: [app\src\test\java]
Java-style resources: [app\src\test\resources]

testDebug
---------
Compile configuration: testDebugCompile
build.gradle name: android.sourceSets.testDebug
Java sources: [app\src\testDebug\java]
Java-style resources: [app\src\testDebug\resources]

testRelease
-----------
Compile configuration: testReleaseCompile
build.gradle name: android.sourceSets.testRelease
Java sources: [app\src\testRelease\java]
Java-style resources: [app\src\testRelease\resources]


BUILD SUCCESSFUL

可见android的插件提供了

  • androidTest
  • debug
  • main
  • release
  • test
  • testDebug
  • testRelease

sourceset和上面说的configure是有关联的.不同的configure会取对应的sourceset. main sourceset是所有configure都会用的.

那么怎么确定当前编译会使用哪个configure呢,是通过build types, product flavors, and build variants来确定的.

build types, product flavors, and build variants

文档

文档

Each build variant represents a different version of your app that you can build. For example, you might want to build one version of your app that's free, with a limited set of content, and another paid version that includes more. You can also build different versions of your app that target different devices, based on API level or other device variations. However, if you want to build different versions based on the device ABI or screen density, instead use APK splits.

flavors:一般指为不同目标编译的版本,比如demo版本,国内版本,国外版本,free版本,pro版本等.

type:debug release

variants: one of{flavors * type}比如demoDebug freeRelease 等

build type

android {
    ...
    defaultConfig {...}
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            applicationIdSuffix ".debug"
        }

        /**
         * The 'initWith' property allows you to copy configurations from other build types,
         * so you don't have to configure one from the beginning. You can then configure
         * just the settings you want to change. The following line initializes
         * 'jnidebug' using the debug build type, and changes only the
         * applicationIdSuffix and versionNameSuffix settings.
         */

        jnidebug {

            // This copies the debuggable attribute and debug signing configurations.
            initWith debug

            applicationIdSuffix ".jnidebug"
            jniDebuggable true
        }
    }
}

每个build type是一个BuildType对象.有这些属性可以设置.

Product Flavors

android {
    ...
    defaultConfig {...}
    buildTypes {...}
    productFlavors {
        demo {
            applicationId "com.example.myapp.demo"
            versionName "1.0-demo"
        }
        full {
            applicationId "com.example.myapp.full"
            versionName "1.0-full"
        }
    }
}

同理每个flavor是一个ProductFlavor对象.


Copyright © FengGuangtu 2017