AOP

java assist

tool

Github.com/didi/DroidA…

By making changes where the method is called, you can modify the system API call. However, the system API internal implementation cannot be modified.

You need to write the substitution into the XML

Comparison before and after modification

<Replace>
    <MethodCall>
        <Source>
           int android.util.Log.d(java.lang.String,java.lang.String)
        </Source>
        <Target>
            $_=com.didichuxing.tools.test.LogUtils.log($1,$2);
        </Target>
    </MethodCall>
</Replace>
Copy the code

Class before processing:

public class MainActivity extends Activity { public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "MainActivity onCreate"); }}Copy the code

Class after processing:

public class MainActivity extends Activity {
    public static final String TAG = "MainActivity";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String var2 = "MainActivity";
	    String var3 = "MainActivity onCreate";
        int var4 = LogUtils.log(var2, var3); // The target method using custom log method.
    }
}
Copy the code

aspectJ

For a method, the modified content resides within the method. Therefore, the system API, Java,Android and other SYSTEM SDK apis cannot be modified

Advantage: Write the implementation directly in Java code, connecting sections only through annotations

grammar

www.cnblogs.com/gy19920604/…

Generated code content:

tool

Github.com/HujiangTech…

Plug-in Usage Guide

Environment:

Android studio4

Com. Android. Tools. Build: gradle: 4.1.0.”

Gradle – 6.5 –

Build. gradle at the project root

buildscript {
  ...
    dependencies {
      ...
      classpath "Com. Android. Tools. Build: gradle: 4.1.0." "
        classpath 'com. Hujiang. Aspectjx: gradle - android plugin - aspectjx: 2.0.10'}}Copy the code

Write a separate gradle file apply to the main project build.gradle:

For example, the file is called aspectJConfig.gradle

Then apply from: “aspectJconfig.gradle”

The contents of the script are:

project.dependencies{
    implementation 'org. Aspectj: aspectjrt: 1.9.5'
   api 'com. Making. Hss01248. Aop - android: logforaop: 1.0.1'
}
def isNoop(){
    for (String s : gradle.startParameter.taskNames) {
        if (s.contains("ultiChannel") | s.contains("elease")) {
            return true
        } else if (s.contains("ebug") | s.contains("ommon")) {
            return false}}return null
}
if(! isNoop()){ applyplugin: 'com.hujiang.android-aspectjx'
// To speed up compilation, you need to include the scanned package path yourself. Performance gap: 4min vs 4S
    aspectjx {
// Exclude all class files and libraries (JAR files) that contain 'android.support' in the package path :Invalid byte tag in constant pool
        exclude 'com.google'.'com.taobao'.'com.alibaba'.'module-info'.'com.squareup.haha'.'versions.9'.'com.tencent'.'android.support'.'androidx'.'com.squareup'.'com.alipay'.'org.apache'.'com.alipay'.'com.facebook'.'cn.jiguang'.'com.github'.'com.meizu'.'com.huawei'.'com.qiyukf'.'com.sina'.'io.reactivex'.'de.greenrobot.event'.'com.netease.neliveplayer'.'com.umeng'.'im.yixin'.'com.commonsware'.'io.fabric'.'rx.android'.'com.android'
        // The package path where the aspect annotation resides must be added
        include 'xxx'}}Copy the code

Log tool for AspectJ

api 'com. Making. Hss01248. Aop - android: logforaop: 1.0.1'
Copy the code

Print class name, object hash, method name, method parameter value, return value for section, time consuming

There is a solution to compile the file

  • Zip file is empty, not found. Zip file is not found
  • Exclude any package path or class that appears in the error report

Other SAO operation

Replace a class in a third-party library

Github.com/skyNet2017/…

Hook the system/language SDK internally

Epic etc.