1+ package com.blankj.bus
2+
3+ import com.android.build.api.transform.*
4+ import com.android.build.gradle.internal.pipeline.TransformManager
5+ import com.blankj.util.JavassistUtils
6+ import com.blankj.util.JsonUtils
7+ import com.blankj.util.LogUtils
8+ import com.google.common.base.Preconditions
9+ import org.apache.commons.io.FileUtils
10+ import org.gradle.api.Project
11+
12+ import java.util.function.Predicate
13+
14+ class BusTransformAsm extends Transform {
15+
16+ Project mProject;
17+
18+ BusTransformAsm (Project project ) {
19+ mProject = project
20+ }
21+
22+ @Override
23+ String getName () {
24+ return " busTransform"
25+ }
26+
27+ @Override
28+ Set<QualifiedContent.ContentType > getInputTypes () {
29+ return TransformManager . CONTENT_CLASS
30+ }
31+
32+ @Override
33+ Set<? super QualifiedContent.Scope > getScopes () {
34+ return TransformManager . SCOPE_FULL_PROJECT
35+ }
36+
37+ @Override
38+ boolean isIncremental () {
39+ return true
40+ }
41+
42+ @Override
43+ void transform (TransformInvocation transformInvocation )
44+ throws TransformException , InterruptedException , IOException {
45+ super . transform(transformInvocation)
46+ def outputProvider = transformInvocation. getOutputProvider()
47+ Preconditions . checkNotNull(outputProvider, " Missing output object for transform " + getName());
48+
49+ LogUtils . l(getName() + " started" )
50+
51+ long stTime = System . currentTimeMillis();
52+
53+ def inputs = transformInvocation. getInputs()
54+ def referencedInputs = transformInvocation. getReferencedInputs()
55+ def isIncremental = transformInvocation. isIncremental()
56+
57+ if (! isIncremental) {
58+ outputProvider. deleteAll();
59+ }
60+
61+ JavassistUtils . init(mProject)
62+ BusScan busScan = new BusScan ()
63+
64+ for (TransformInput input : transformInvocation. getInputs()) {
65+ for (DirectoryInput dirInput : input. getDirectoryInputs()) {// 遍历文件夹
66+ File dir = dirInput. file
67+ JavassistUtils . getPool(). appendClassPath(dir. absolutePath)
68+
69+ def dest = outputProvider. getContentLocation(
70+ dirInput. name,
71+ dirInput. contentTypes,
72+ dirInput. scopes,
73+ Format . DIRECTORY
74+ )
75+ FileUtils . copyDirectory(dir, dest)
76+
77+ LogUtils . l(" scan dir: $dir [$dest ]" )
78+
79+ if (isIncremental) {
80+ switch (status) {
81+ case Status . NOTCHANGED :
82+ break ;
83+ case Status . ADDED :
84+ case Status . CHANGED :
85+ transformJar(jarInput. getFile(), dest, status);
86+ break ;
87+ case Status . REMOVED :
88+ if (dest. exists()) {
89+ FileUtils . forceDelete(dest);
90+ }
91+ break ;
92+ }
93+ } else {
94+ // Forgive me!, Some project will store 3rd-party aar for serveral copies in dexbuilder folder,,unknown issue.
95+ if (inDuplcatedClassSafeMode() & ! isIncremental && ! flagForCleanDexBuilderFolder) {
96+ cleanDexBuilderFolder(dest);
97+ flagForCleanDexBuilderFolder = true ;
98+ }
99+ transformJar(jarInput. getFile(), dest, status);
100+ }
101+
102+
103+ busScan. scanDir(dir)
104+ }// 遍历文件夹结束
105+
106+ for (JarInput jarInput : input. getJarInputs()) {// 遍历 jar 文件
107+ File jar = jarInput. file
108+ JavassistUtils . getPool(). appendClassPath(jarInput. file. absolutePath)
109+
110+ def jarName = jarInput. name
111+ def dest = outputProvider. getContentLocation(
112+ jarName,
113+ jarInput. contentTypes,
114+ jarInput. scopes,
115+ Format . JAR
116+ )
117+ FileUtils . copyFile(jar, dest)
118+
119+ if (jarName. startsWith(" com.blankj:utilcode:" )
120+ || jarName. startsWith(" com.blankj:utilcodex:" )
121+ || jarName. contains(" utilcode-lib" )) {
122+ busScan. busJar = dest
123+ LogUtils . l(" bus jar: $jarName [$dest ]" )
124+ return
125+ }
126+
127+ if (jumpScan(jarName)) {
128+ LogUtils . l(" jump jar: $jarName [$dest ]" )
129+ return
130+ }
131+
132+ LogUtils . l(" scan jar: $jarName [$dest ]" )
133+ busScan. scanJar(jar)
134+ }
135+ }// 遍历 jar 文件结束
136+
137+ if (busScan. busJar != null ) {
138+ File jsonFile = new File (mProject. projectDir. getAbsolutePath(), " __bus__.json" )
139+ String busJson = JsonUtils . getFormatJson(busScan. busStaticMap)
140+ LogUtils . l(jsonFile. toString() + " : " + busJson)
141+ FileUtils . write(jsonFile, busJson)
142+ BusInject . start(busScan. busStaticMap, busScan. busJar)
143+ } else {
144+ LogUtils . l(' u should <implementation "com.blankj:utilcode:1.22.+">' )
145+ }
146+
147+ LogUtils . l(getName() + " finished: " + (System . currentTimeMillis() - stTime) + " ms" )
148+ }
149+
150+ private static boolean jumpScan (String jarName ) {
151+ boolean isExcept = false
152+ for (String except : Config . EXCEPTS ) {
153+ if (jarName. startsWith(except)) {
154+ isExcept = true
155+ break
156+ }
157+ }
158+ isExcept
159+ }
160+ }
0 commit comments