==================================================================================================
1.1.1,Gradle
Gradle是一种构建工具,它使用一种基于Groovy的特定领域语言(DSL)来构建项目。不仅仅用于android 工程的构建。
####1.1.2,Android Plugin for Gradle
这就是为了编译android 工程而开发的插件。下面就是申明Android Gradle 插件的位置。(build.gradle)
buildscript {
…
dependencies {
classpath ‘com.android.tools.build:gradle:2.2.0’
}
}
==============================================================================================
1.2.1,gradle 各版本源码地址
http://services.gradle.org/distributions/
1.2.2, google 官网 gradle 插件 与 gradle 版本对照地址
https://developer.android.google.cn/studio/releases/gradle-plugin#updating-plugin
1.2.3,gradle 版本与google gradle 插件版本的区别
在gradle wrapper.properties 中写的是 gradle 版本。
distributionUrl=https://services.gradle.org/distributions/gradle-4.4-all.zip
在build.gradle 中依赖的是 gradle插件版本。
dependencies {
//[this is the android gradle plugin version]
classpath ‘com.android.tools.build:gradle:3.1.0’
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
===============================================================================================
我们编译Android项目时,如果我们想拿到编译时产生的Class文件,并在生成Dex之前做一些处理,我们可以通过编写一个Transform
来接收这些输入(编译产生的Class文件),并向已经产生的输入中添加一些东西。
我们可以通过Gradle插件来注册我们编写的Transform
。注册后的Transform
会被Gradle包装成一个Gradle Task
,这个TransForm Task会在java compile Task
执行完毕后运行。
对于编写Transform
的API, 我们可以通过引入下面这个依赖来使用:
compile ‘com.android.tools.build:gradle:2.3.3’ //版本应该在 2.x以上
先大致看一下Transform
的执行流程图:
##2.2 Transform的使用场景
一般我们使用Transform
会有下面两种场景
-
我们需要对编译class文件做自定义的处理。
-
我们需要读取编译产生的class文件,做一些其他事情,但是不需要修改它。
接下来我们就来看一下这些Transform API
吧 :
我们编写一个自定义的transform需要继承Transform
,它是一个抽象类, 我们这里先看一下Transform
的抽象方法:
public abstract class Transform {
public abstract String getName();
public abstract Set getInputTypes();
public abstract Set<? super Scope> getScopes();
public abstract boolean isIncremental(); // 是否支持增量编译
}
getName()
就是指定自定义的Transform
的名字。
##2.4 输入的类型
Set<ContentType> getInputTypes()
是指明你自定义的这个Transform
处理的输入类型,输入类型共有以下几种:
enum DefaultContentType implements ContentType {
/**
-
The content is compiled Java code. This can be in a Jar file or in a folder. If
-
in a folder, it is expected to in sub-folders matching package names.
*/
CLASSES(0x01),
/**
- The content is standard Java resources.
*/
RESOURCES(0x02);
}
即分为class文件或者java资源。class文件来自于jar或者文件夹。资源就是标准的java资源。
##2.5 输入文件所属的范围 Scope
getScopes()
用来指明自定的Transform
的输入文件所属的范围, 这是因为gradle是支持多工程编译的。总共有以下几种:
/**
-
This indicates what the content represents, so that Transforms can apply to only part(s)
-
of the classes or resources that the build manipulates.
*/
enum Scope implements ScopeType {
/** Only the project content */
PROJECT(0x01), //只是当前工程的代码
/** Only the project’s local dependencies (local jars) */
PROJECT_LOCAL_DEPS(0x02), // 工程的本地jar
/** Only the sub-projects. */
SUB_PROJECTS(0x04), // 只包含子工工程
/** Only the sub-projects’s local dependencies (local jars). */
SUB_PROJECTS_LOCAL_DEPS(0x08),
/** Only the external libraries */
EXTERNAL_LIBRARIES(0x10),
/** Code that is being tested by the current variant, including dependencies */
TESTED_CODE(0x20),
/** Local or remote dependencies that are provided-only */
PROVIDED_ONLY(0x40);
}
对于getScopes()
的返回,其实TransformManager
已经为我们定义了一些,比如:
public static final Set SCOPE_FULL_PROJECT = Sets.immutableEnumSet(
Scope.PROJECT, Scope.PROJECT_LOCAL_DEPS, Scope.SUB_PROJECTS, Scope.SUB_PROJECTS_LOCAL_DEPS, Scope.EXTERNAL_LIBRARIES);
如果一个Transform不想处理任何输入,只是想查看输入的内容,那么只需在getScopes()
返回一个空集合,在getReferencedScopes()
返回想要接收的范围。
public Set<? super Scope> getReferencedScopes() {
return ImmutableSet.of();
}
##2.6 transform()
它是Transform
的关键方法:
public void transform(@NonNull TransformInvocation transformInvocation) {}
它是一个空实现,input
的内容将会打包成一个TransformInvocation
对象,因为我们要想使用input
,我们需要详细了解一下TransformInvocation
参数。
##2.7 TransformInvocation
我们看一下这个类相关的API:
public interface TransformInvocation {
Collection getInputs(); // 输入作为 TransformInput 返回
TransformOutputProvider getOutputProvider(); //TransformOutputProvider 可以用来创建输出内容
boolean isIncremental();
}
public interface TransformInput {
Collection getJarInputs();
Collection getDirectoryInputs();
}
public interface JarInput extends QualifiedContent {
File getFile(); //jar文件
Set getContentTypes(); // 是class还是resource
Set<? super Scope> getScopes(); //属于Scope:
}
DirectoryInput与JarInput定义基本相同。
public interface TransformOutputProvider {
//根据 name、ContentType、QualifiedContent.Scope返回对应的文件( jar / directory)
File getContentLocation(String name, Set<QualifiedContent.ContentType> types, Set<? super QualifiedContent.Scope> scopes, Format format);
}
即我们可以通过TransformInvocation
来获取输入,同时也获得了输出的功能。举个例子,
public void transform(TransformInvocation invocation) {
for (TransformInput input : invocation.getInputs()) {
input.getJarInputs().parallelStream().forEach(jarInput -> {
File src = jarInput.getFile();
JarFile jarFile = new JarFile(file);
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
//处理
}
}
}