@@ -27,8 +27,11 @@ import java.util.zip.ZipEntry
2727class SensorsAnalyticsTransform extends Transform {
2828 private static Project project
2929 private static HashSet<String > exclude = [' com.sensorsdata.analytics.android.sdk' , ' android.support' ]
30- private static HashSet<String > include = []
30+ private static HashSet<String > include = [' butterknife.internal.DebouncingOnClickListener' ,
31+ ' com.jakewharton.rxbinding.view.ViewClickOnSubscribe' ,
32+ ' com.facebook.react.uimanager.NativeViewHierarchyManager' ]
3133 protected static boolean disableJar
34+ private static final String VERSION = " v2.2.0"
3235
3336 SensorsAnalyticsTransform (Project project ) {
3437 this . project = project
@@ -62,7 +65,7 @@ class SensorsAnalyticsTransform extends Transform {
6265 println (" ####################################################################" )
6366 println (" ######## ########" )
6467 println (" ######## ########" )
65- println (" ######## 欢迎使用 SensorsAnalytics® 编译插件 ########" )
68+ println (" ######## 欢迎使用 SensorsAnalytics® ( " + VERSION + " )编译插件 ########" )
6669 println (" ######## 使用过程中碰到任何问题请联系我们 ########" )
6770 println (" ######## ########" )
6871 println (" ######## ########" )
@@ -101,8 +104,6 @@ class SensorsAnalyticsTransform extends Transform {
101104 */
102105 input. jarInputs. each { JarInput jarInput ->
103106 String destName = jarInput. file. name
104- Logger . info(" 开始遍历 jar:" + jarInput. file. absolutePath)
105-
106107 /**
107108 * 截取文件路径的md5值重命名输出文件,因为可能同名,会覆盖
108109 */
@@ -114,15 +115,17 @@ class SensorsAnalyticsTransform extends Transform {
114115 File dest = outputProvider. getContentLocation(destName + " _" + hexName, jarInput. contentTypes, jarInput. scopes, Format . JAR )
115116
116117 def modifiedJar = null
117- // if (!project.sensorsAnalytics.disableJar) {
118- modifiedJar = modifyJarFile(jarInput. file, context. getTemporaryDir())
119- // }
118+ if (! project. sensorsAnalytics. disableJar) {
119+ if (isShouldModifyJar(jarInput. file. absolutePath)) {
120+ Logger . info(" 开始遍历 jar:" + jarInput. file. absolutePath)
121+ modifiedJar = modifyJarFile(jarInput. file, context. getTemporaryDir())
122+ Logger . info(" 结束遍历 jar:" + jarInput. file. absolutePath)
123+ }
124+ }
120125 if (modifiedJar == null ) {
121126 modifiedJar = jarInput. file
122127 }
123128 FileUtils . copyFile(modifiedJar, dest)
124-
125- Logger . info(" 结束遍历 jar:" + jarInput. file. absolutePath)
126129 }
127130
128131 /**
@@ -157,36 +160,59 @@ class SensorsAnalyticsTransform extends Transform {
157160 }
158161 }
159162
160- private static boolean isShouldModifyJar (String jarName ) {
161- Iterator<String > iterator = exclude. iterator()
162- while (iterator. hasNext()) {
163- String packageName = iterator. next()
164- if (packageName == jarName) {
165- return false
163+ private static boolean isShouldModifyJar (String jarFileName ) {
164+ return true
165+ /*
166+ if (project.sensorsAnalytics.useInclude) {
167+ Iterator<String> iterator = include.iterator()
168+ while (iterator.hasNext()) {
169+ String jarName = iterator.next()
170+ if (jarFileName.contains(jarName)) {
171+ return true
172+ }
166173 }
174+ return false
175+ } else {
176+ Iterator<String> iterator = exclude.iterator()
177+ while (iterator.hasNext()) {
178+ String jarName = iterator.next()
179+ if (jarFileName.contains(jarName)) {
180+ return false
181+ }
182+ }
183+ return true
167184 }
168- return true
185+ */
169186 }
170187
171188 private static boolean isShouldModifyClass (String className ) {
172- if (! disableJar) {
173- Iterator<String > iterator = exclude. iterator()
189+ if (className. contains(' R$' ) ||
190+ className. contains(' R2$' ) ||
191+ className. contains(' R.class' ) ||
192+ className. contains(' R2.class' ) ||
193+ className. contains(' BuildConfig.class' )) {
194+ return false
195+ }
196+
197+ if (project. sensorsAnalytics. useInclude) {
198+ Iterator<String > iterator = include. iterator()
174199 while (iterator. hasNext()) {
175200 String packageName = iterator. next()
201+
176202 if (className. startsWith(packageName)) {
177- return false
203+ return true
178204 }
179205 }
180- return true
206+ return false
181207 } else {
182- Iterator<String > iterator = include . iterator()
208+ Iterator<String > iterator = exclude . iterator()
183209 while (iterator. hasNext()) {
184210 String packageName = iterator. next()
185211 if (className. startsWith(packageName)) {
186- return true
212+ return false
187213 }
188214 }
189- return false
215+ return true
190216 }
191217 }
192218
@@ -195,9 +221,7 @@ class SensorsAnalyticsTransform extends Transform {
195221 */
196222 private static File modifyJarFile (File jarFile , File tempDir ) {
197223 if (jarFile) {
198- // if (isShouldModifyJar(jarFile.getName())) {
199- return modifyJar(jarFile, tempDir, true )
200- // }
224+ return modifyJar(jarFile, tempDir, true )
201225
202226 }
203227 return null
@@ -225,26 +249,35 @@ class SensorsAnalyticsTransform extends Transform {
225249 InputStream inputStream = file. getInputStream(jarEntry)
226250
227251 String entryName = jarEntry. getName()
228- String className
252+ if (entryName. endsWith(" .DSA" ) || entryName. endsWith(" .SF" )) {
253+ // ignore
254+ } else {
255+ String className
229256
230- ZipEntry zipEntry = new ZipEntry (entryName)
257+ ZipEntry zipEntry = new ZipEntry (entryName)
231258
232- jarOutputStream. putNextEntry(zipEntry)
259+ jarOutputStream. putNextEntry(zipEntry)
233260
234- byte [] modifiedClassBytes = null
235- byte [] sourceClassBytes = IOUtils . toByteArray(inputStream)
236- if (entryName . endsWith( " .class " )) {
237- className = entryName . replace( " / " , " . " ) . replace( " .class " , " " )
238- if (isShouldModifyClass(className) ) {
239- modifiedClassBytes = modifyClasses(className, sourceClassBytes)
261+ byte [] modifiedClassBytes = null
262+ byte [] sourceClassBytes = null
263+ try {
264+ sourceClassBytes = IOUtils . toByteArray(inputStream )
265+ } catch ( Exception e ) {
266+ return null
240267 }
268+ if (entryName. endsWith(" .class" )) {
269+ className = entryName. replace(" /" , " ." ). replace(" .class" , " " )
270+ if (isShouldModifyClass(className)) {
271+ modifiedClassBytes = modifyClasses(className, sourceClassBytes)
272+ }
273+ }
274+ if (modifiedClassBytes == null ) {
275+ jarOutputStream. write(sourceClassBytes)
276+ } else {
277+ jarOutputStream. write(modifiedClassBytes)
278+ }
279+ jarOutputStream. closeEntry()
241280 }
242- if (modifiedClassBytes == null ) {
243- jarOutputStream. write(sourceClassBytes)
244- } else {
245- jarOutputStream. write(modifiedClassBytes)
246- }
247- jarOutputStream. closeEntry()
248281 }
249282 jarOutputStream. close()
250283 file. close()
0 commit comments