Skip to content

Commit

Permalink
异步执行
Browse files Browse the repository at this point in the history
  • Loading branch information
Peakmain committed May 24, 2022
1 parent 2dc0ecf commit afe9967
Show file tree
Hide file tree
Showing 22 changed files with 133 additions and 107 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dependencies {
}
monitorPlugin {
whiteList = [
//"com.peakmain.asmactualcombat.utils.TestUtils",
"com.peakmain.asmactualcombat.utils.TestUtils",
"com.peakmain.plugin"
]
methodStatus = 1
Expand Down
26 changes: 6 additions & 20 deletions app/src/main/java/com/peakmain/asmactualcombat/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.SubscriptionInfo;
Expand Down Expand Up @@ -87,29 +88,14 @@ public String onReplaceMethodListener(int telephoneState, TelephonyManager manag
}

@Override
public String onReplaceMethodListener(int wifiInfoState, WifiInfo wifiInfo) {
switch (wifiInfoState) {
case SensorsDataConstants.GET_MAC_ADDRESS:
LogUtils.e("替换GET_MAC_ADDRESS");
break;
case SensorsDataConstants.GET_SSID:
LogUtils.e("替换GET_SSID");
break;
case SensorsDataConstants.GET_BSSID:
LogUtils.e("替换GET_SSIDGET_BSSID");
break;
case SensorsDataConstants.GET_IP_ADDRESS:
LogUtils.e("替换GET_IP_ADDRESS");
break;
default:
break;
}
return "";
public WifiInfo onReplaceMethodListener(WifiManager wifiManager) {
LogUtils.e("替换WifiManager");
return null;
}

@Override
public WifiInfo onReplaceMethodListener(WifiManager wifiManager) {
LogUtils.e("替换WifiManager");
public List<ScanResult> onReplaceWifiManagerMethodListener(WifiManager wifiManager) {
LogUtils.e("替换onReplaceWifiManagerMethodListener");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,27 @@ class MonitorAnalyticsTransform {
* 过滤不需要修改的class
*/
protected static boolean isShouldModify(String className) {
boolean isShouldModify = false
if (!isAndroidGenerated(className)) {
for (pkgName in special) {
if (className.startsWith(pkgName)) {
return true
}
}
} else {
isShouldModify = true
if (!isLeanback(className)) {
for (pkgName in exclude) {
if (className.startsWith(pkgName)) {
return false
isShouldModify = false
break
}
}
}
}
return true
return isShouldModify
}

private static boolean isLeanback(String className) {
private static boolean isLeanback(String className) {
return className.startsWith("android.support.v17.leanback") || className.startsWith("androidx.leanback")
}

Expand All @@ -81,12 +83,12 @@ class MonitorAnalyticsTransform {
className.contains('BuildConfig.class')
}

static File modifyClassFile(File dir, File classFile, File tempDir,MonitorConfig monitorConfig) {
static File modifyClassFile(File dir, File classFile, File tempDir, MonitorConfig monitorConfig) {
File modified = null
try {
String className = path2ClassName(classFile.absolutePath.replace(dir.absolutePath + File.separator, ""))
byte[] sourceClassBytes = IOUtils.toByteArray(new FileInputStream(classFile))
byte[] modifiedClassBytes = modifyClass(sourceClassBytes,monitorConfig)
byte[] modifiedClassBytes = modifyClass(sourceClassBytes, monitorConfig)
if (modifiedClassBytes) {
modified = new File(tempDir, className.replace('.', '') + '.class')
if (modified.exists()) {
Expand All @@ -102,15 +104,15 @@ class MonitorAnalyticsTransform {
return modified
}

private static byte[] modifyClass(byte[] srcClass,MonitorConfig monitorConfig) throws IOException {
private static byte[] modifyClass(byte[] srcClass, MonitorConfig monitorConfig) throws IOException {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS)
PeakmainVisitor classVisitor = new PeakmainVisitor(classWriter,monitorConfig)
PeakmainVisitor classVisitor = new PeakmainVisitor(classWriter, monitorConfig)
ClassReader cr = new ClassReader(srcClass)
cr.accept(classVisitor, ClassReader.SKIP_FRAMES)
return classWriter.toByteArray()
}

static File modifyJar(File jarFile, File tempDir, boolean nameHex,MonitorConfig monitorConfig) {
static File modifyJar(File jarFile, File tempDir, boolean nameHex, MonitorConfig monitorConfig) {
/**
* 读取原 jar
*/
Expand All @@ -128,7 +130,7 @@ class MonitorAnalyticsTransform {
Enumeration enumeration = file.entries()
while (enumeration.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) enumeration.nextElement()
InputStream inputStream = null
InputStream inputStream
try {
inputStream = file.getInputStream(jarEntry)
} catch (Exception e) {
Expand All @@ -147,7 +149,7 @@ class MonitorAnalyticsTransform {
if (entryName.endsWith(".class")) {
className = entryName.replace(Matcher.quoteReplacement(File.separator), ".").replace(".class", "")
if (isShouldModify(className)) {
modifiedClassBytes = modifyClass(sourceClassBytes,monitorConfig)
modifiedClassBytes = modifyClass(sourceClassBytes, monitorConfig)
}
}
if (modifiedClassBytes == null) {
Expand All @@ -161,6 +163,7 @@ class MonitorAnalyticsTransform {
file.close()
return outputJar
}

static String path2ClassName(String pathName) {
pathName.replace(File.separator, ".").replace(".class", "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.ide.common.internal.WaitableExecutor
import com.peakmain.analytics.plugin.ext.MonitorConfig
import com.peakmain.analytics.plugin.utils.log.Logger
import groovy.io.FileType
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
Expand Down Expand Up @@ -68,18 +69,20 @@ class MonitorTransform extends Transform {
@Override
void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
super.transform(transformInvocation)
_transform(transformInvocation.context, transformInvocation.inputs, transformInvocation.outputProvider)
_transform(transformInvocation.context, transformInvocation.inputs, transformInvocation.outputProvider, transformInvocation.isIncremental())
}
/**
*
* @param context
* @param inputs 有两种类型,一种是目录,一种是 jar 包,要分开遍历
* @param outputProvider 输出路径
*/
void _transform(Context context, Collection<TransformInput> inputs, TransformOutputProvider outputProvider) throws IOException, TransformException, InterruptedException {
void _transform(Context context, Collection<TransformInput> inputs,
TransformOutputProvider outputProvider, boolean isIncremental)
throws IOException, TransformException, InterruptedException {
println(monitorConfig.toString())
long startTime = System.currentTimeMillis()
if (!incremental) {
if (!isIncremental) {
//不是增量更新删除所有的outputProvider
outputProvider.deleteAll()
}
Expand All @@ -91,12 +94,12 @@ class MonitorTransform extends Transform {
waitableExecutor.execute(new Callable<Object>() {
@Override
Object call() throws Exception {
handleDirectoryInput(context, directoryInput, outputProvider,monitorConfig)
handleDirectoryInput(context, directoryInput, outputProvider, monitorConfig,isIncremental)
return null
}
})
} else {
handleDirectoryInput(context, directoryInput, outputProvider,monitorConfig)
handleDirectoryInput(context, directoryInput, outputProvider, monitorConfig,isIncremental)
}
}
// 遍历jar 第三方引入的 class
Expand All @@ -105,12 +108,12 @@ class MonitorTransform extends Transform {
waitableExecutor.execute(new Callable<Object>() {
@Override
Object call() throws Exception {
handleJarInput(context,jarInput, outputProvider,monitorConfig)
handleJarInput(context, jarInput, outputProvider, monitorConfig,isIncremental)
return null
}
})
} else {
handleJarInput(context,jarInput, outputProvider,monitorConfig)
handleJarInput(context, jarInput, outputProvider, monitorConfig,isIncremental)
}
}
}
Expand All @@ -120,36 +123,97 @@ class MonitorTransform extends Transform {
println("[MonitorTransform]: 此次编译共耗时:${System.currentTimeMillis() - startTime}毫秒")
}

void handleDirectoryInput(Context context, DirectoryInput directoryInput, TransformOutputProvider outputProvider,MonitorConfig monitorConfig) {

File dest = outputProvider.getContentLocation(directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
void handleDirectoryInput(Context context, DirectoryInput directoryInput, TransformOutputProvider outputProvider, MonitorConfig monitorConfig,boolean isIncremental) {
File dir = directoryInput.file
if (dir) {
HashMap<String, File> modifyMap = new HashMap<>()
dir.traverse(type: FileType.FILES, nameFilter: ~/.*\.class/) {
File classFile ->
if (MonitorAnalyticsTransform.isShouldModify(classFile.name)) {
File modified = MonitorAnalyticsTransform.modifyClassFile(dir, classFile, context.getTemporaryDir(),monitorConfig)
File dest = outputProvider.getContentLocation(directoryInput.getName(),
directoryInput.getContentTypes(), directoryInput.getScopes(),
Format.DIRECTORY)
FileUtils.forceMkdir(dest)
String srcDirPath = dir.absolutePath
String destDirPath = dest.absolutePath
if (isIncremental) {
Map<File, Status> fileStatusMap = directoryInput.getChangedFiles()
for (Map.Entry<File, Status> changedFile : fileStatusMap.entrySet()) {
Status status = changedFile.getValue()
File inputFile = changedFile.getKey()
String destFilePath = inputFile.absolutePath.replace(srcDirPath, destDirPath)
File destFile = new File(destFilePath)
switch (status) {
case Status.NOTCHANGED:
break
case Status.REMOVED:
Logger.info("目录 status = $status:$inputFile.absolutePath")
if (destFile.exists()) {
//noinspection ResultOfMethodCallIgnored
destFile.delete()
}
break
case Status.ADDED:
case Status.CHANGED:
Logger.info("目录 status = $status:$inputFile.absolutePath")
File modified = MonitorAnalyticsTransform.modifyClassFile(dir, inputFile, context.getTemporaryDir(),monitorConfig)
if (destFile.exists()) {
destFile.delete()
}
if (modified != null) {
String ke = classFile.absolutePath.replace(dir.absolutePath, "")
modifyMap.put(ke, modified)
FileUtils.copyFile(modified, destFile)
modified.delete()
} else {
FileUtils.copyFile(inputFile, destFile)
}
}
break
default:
break
}
}
FileUtils.copyDirectory(directoryInput.file, dest)
modifyMap.entrySet().each {
Map.Entry<String, File> en ->
File target = new File(dest.absolutePath + en.getKey())
if (target.exists()) {
target.delete()
}
FileUtils.copyFile(en.getValue(), target)
en.getValue().delete()
}else {
FileUtils.copyDirectory(dir, dest)
dir.traverse(type: FileType.FILES, nameFilter: ~/.*\.class/) {
File inputFile ->
forEachDir(dir, inputFile, context, srcDirPath, destDirPath)
}
}

}
void forEachDir(File dir, File inputFile, Context context, String srcDirPath, String destDirPath) {
File modified = MonitorAnalyticsTransform.modifyClassFile(dir, inputFile, context.getTemporaryDir(),monitorConfig)
if (modified != null) {
File target = new File(inputFile.absolutePath.replace(srcDirPath, destDirPath))
if (target.exists()) {
target.delete()
}
FileUtils.copyFile(modified, target)
modified.delete()
}
}
void handleJarInput(Context context, JarInput jarInput, TransformOutputProvider outputProvider, MonitorConfig monitorConfig,boolean isIncremental) {
//获得输出文件
File destFile = outputProvider.getContentLocation(jarInput.name, jarInput.contentTypes, jarInput.scopes, Format.JAR)
if (isIncremental) {
Status status = jarInput.getStatus()
switch (status) {
case Status.NOTCHANGED:
break
case Status.ADDED:
case Status.CHANGED:
Logger.info("jar status = $status:$destFile.absolutePath")
transformJar(context,jarInput, outputProvider, monitorConfig)
break
case Status.REMOVED:
Logger.info("jar status = $status:$destFile.absolutePath")
if (destFile.exists()) {
FileUtils.forceDelete(destFile)
}
break
default:
break
}
} else {
transformJar(context,jarInput, outputProvider, monitorConfig)
}

void handleJarInput(Context context,JarInput jarInput, TransformOutputProvider outputProvider,MonitorConfig monitorConfig) {
}
void transformJar(Context context,JarInput jarInput,TransformOutputProvider outputProvider,MonitorConfig monitorConfig1) {
String destName = jarInput.file.name

/**截取文件路径的 md5 值重命名输出文件,因为可能同名,会覆盖*/
Expand All @@ -161,7 +225,7 @@ class MonitorTransform extends Transform {

/** 获得输出文件*/
File dest = outputProvider.getContentLocation(destName + "_" + hexName, jarInput.contentTypes, jarInput.scopes, Format.JAR)
def modifiedJar = MonitorAnalyticsTransform.modifyJar(jarInput.file, context.getTemporaryDir(), true,monitorConfig)
def modifiedJar = MonitorAnalyticsTransform.modifyJar(jarInput.file, context.getTemporaryDir(), true, monitorConfig)
if (modifiedJar == null) {
modifiedJar = jarInput.file
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.peakmain.analytics.plugin.utils
import org.objectweb.asm.Opcodes

class MethodFieldUtils {
public static final String PLUGIN_VERSION = "1.0.8"
public static final String PLUGIN_VERSION = "1.0.9"
enum StatusEnum {
METHOD_STATE_NORMAL(0),
METHOD_STATE_REPLACE(1)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b4c17a7fd0b505c4dc894880d98f27cd
fd77a7a0540b0878f100c98ca65a69d1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137e1ee3b23a4079e5919c42ea42c3eea0c29069
d43f5bfd21977d58746d7b8f3b1ac0ad1a7b10bd
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f408535cd0f2cea5899204f98c7b5e9b
672e91d539a1b015d1a5f93e252997d4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5a08f219ef3f743b0862047a6a203610fbc26f8e
3e55508966d0714ba5e6d7bdc38993b0fe7bdd12
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79995d67cd36046edfcb9139d9b2345d
6fc84fdd20111cd7a574e2a6706a19d0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
98401e6af6769b357c21a838321b6797ee62c7e7
3f9e42b7cb5cd1b8224c0e0c6f891f7128e006b6
Binary file modified repo/com/peakmain/android.plugin/1.0.0/android.plugin-1.0.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23c16c1a1bd5ef8e30fbda3d15775866
52967815474cef07778ea4f48610d6e0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8b3de2a58c940a1bf96b8f9fdc57d189d270f13c
bfef381a9258fc2390f4ae21d5e4b2a5a81a09af
2 changes: 1 addition & 1 deletion repo/com/peakmain/android.plugin/maven-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<versions>
<version>1.0.0</version>
</versions>
<lastUpdated>20220524013406</lastUpdated>
<lastUpdated>20220524074110</lastUpdated>
</versioning>
</metadata>
2 changes: 1 addition & 1 deletion repo/com/peakmain/android.plugin/maven-metadata.xml.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6d9d64fa6fd79490f4dcbef88761e8a5
d80c19aaad554188a998e5bc89e7b0d1
Loading

0 comments on commit afe9967

Please sign in to comment.