Gradle+ASM實(shí)戰(zhàn)——隱私方法問題徹底解決之AsmActualCombat開源庫

AsmActualCombat

功能介紹

ASM全埋點(diǎn)功能

  • $AppStart事件:應(yīng)用程序啟動啟動事件。
  • $AppEnd事件:應(yīng)用程序退出事件。
  • $AppViewScreen事件:應(yīng)用程序頁面瀏覽事件
  • $AppClick 事件:應(yīng)用程序控件(View)點(diǎn)擊事件,如:ImageView,Button,Dialog等
  • 默認(rèn)包含防止多次點(diǎn)擊事件的處理
  • 可動態(tài)設(shè)置方法對點(diǎn)擊事件處理之前進(jìn)行攔截,目前只支持對setOnClickListener進(jìn)行攔截

隱私方法調(diào)用處理

  • 對調(diào)用隱私方法的方法體替換成自己的方法(支持動態(tài)替換方法),如:設(shè)備id,Mac地址等

輔助功能

  • 可獲取方法的耗時(shí)時(shí)間
  • 打印方法的參數(shù)和返回值
  • 打印方法的Frame
  • 可動態(tài)配置是否開啟插件,默認(rèn)是開啟

怎樣使用

ASM插件依賴
Add it in your root build.gradle at the end of repositories:

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "io.github.peakmain:plugin:1.1.1"
  }
}
apply plugin: "com.peakmain.plugin"

攔截事件sdk的依賴

  • Step 1. Add the JitPack repository to your build file
    Add it in your root build.gradle at the end of repositories:
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  • Step 2. Add the dependency
    dependencies {
           implementation 'com.github.Peakmain:AsmActualCombat:1.1.1'
    }

使用文檔

Application中初始化

  SensorsDataAPI.init(this);

動態(tài)設(shè)置是否開啟插件

在gradle.properties中設(shè)置以下參數(shù),false代表不關(guān)閉插件,true表示關(guān)閉插件,默認(rèn)是false

peakmainPlugin.disableAppClick=false

數(shù)據(jù)全埋點(diǎn)接口上傳

SensorsDataAPI.getInstance().setOnUploadSensorsDataListener { state: Int, data: String ->
    when (state) {
            //$AppStart事件
        SensorsDataConstants.APP_START_EVENT_STATE,
            //$AppEnd事件
        SensorsDataConstants.APP_END__EVENT_STATE,
            //$AppViewScreen事件
        SensorsDataConstants.APP_VIEW_SCREEN__EVENT_STATE,
            //$AppClick 事件
        SensorsDataConstants.APP_VIEW_CLICK__EVENT_STATE
        -> if (BuildConfig.DEBUG) {
            Log.e("TAG", "埋點(diǎn)\n$data")
        }
        else -> if (BuildConfig.DEBUG) {
            Log.e("TAG", data)
        }
    }
}

在要攔截的頁面設(shè)置攔截事件

  • 需要在app.gradle中添加攔截包名前綴,比如我的項(xiàng)目前綴是com.peakmain
monitorPlugin {
     ...
    interceptPackageName = "com.peakmain"
}

目前只支持setOnClickListener事件攔截

 SensorsDataAPI.getInstance().setOnUserAgreementListener(new SensorsDataAPI.OnUserAgreementListener() {
           @Override
            public boolean onUserAgreement() {
            
        }
 })

完整的舉例代碼如下:

    private void setUserAgreementListener(Activity activity) {
        isAcceptUserPrivacy = (Boolean) PreferencesUtils.getInstance(this).getParam(Constants.HAS_ACCEPT_USER_PRIVACY, false);
        SensorsDataAPI.getInstance().setOnUserAgreementListener(new SensorsDataAPI.OnUserAgreementListener() {
            @Override
            public boolean onUserAgreement() {
                if (!isAcceptUserPrivacy) {
                    //沒有同意
                    AlertDialog dialog = new AlertDialog.Builder(activity)
                            .setContentView(R.layout.dialog_user_agreement)
                            .setCancelable(false)//點(diǎn)擊空白不可取消
                            .show();
                     //設(shè)置用戶協(xié)議的攔截事件為NULL
                    SensorsDataAPI.getInstance().setOnUserAgreementListener(null);
                    dialog.setOnClickListener(R.id.stv_refuse, v -> {
                        //取消按鈕的點(diǎn)擊事件
                        dialog.dismiss();
                        setUserAgreementListener(activity);
                    });
                    dialog.setOnClickListener(R.id.stv_agreement, v -> {
                    //同意按鈕的點(diǎn)擊事件
                        com.peakmain.ui.utils.ToastUtils.showLong("同意了");
                        PreferencesUtils.getInstance(activity).saveParams(Constants.HAS_ACCEPT_USER_PRIVACY, true);
                        dialog.dismiss();
                      
                    });
                }
                return isAcceptUserPrivacy;
            }
        });

獲取方法耗時(shí)時(shí)間和打印方法參數(shù)和返回值

在需要獲取耗時(shí)時(shí)間的方法上方添加以下注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogMessage {
    /**
     * 是否打印方法耗時(shí)時(shí)間
     */
    boolean isLogTime() default false;

    /**
     *
     * 是否打印方法的參數(shù)和返回值
     */
    boolean isLogParametersReturnValue() default false;

}
獲取方法耗時(shí)時(shí)間

如果想獲取方法耗時(shí)時(shí)間,則需要設(shè)置注解參數(shù)isLogTime為true,如:

@LogMessage(isLogTime = true)
public String getMethodTime(long l) {
    return "getMethod";
}
image.png
打印方法參數(shù)和返回值

如果想打印方法參數(shù)和返回值,則需要設(shè)置注解參數(shù)isLogParametersReturnValue,如:

@LogMessage(isLogParametersReturnValue = true)
public String getMethodParametersReturnValue(String name) {
    return "treasure";
}
image.png
獲取方法耗時(shí)的同時(shí)打印方法參數(shù)和返回值

也可支持獲取方法耗時(shí)的同時(shí)打印方法參數(shù)和返回值

@LogMessage(isLogTime = true, isLogParametersReturnValue = true)
public String test(int a) {
    return "peakmain";
}
image.png

打印方法的Frame

  • 1、開啟堆棧分析,默認(rèn)是禁用,false表示開啟,true表示禁用
monitorPlugin {
    disableStackMapFrame = false
}
  • 2、如果想打印方法的Frame,只需要在方法上添加注解@LogFrameInfo,如:
@LogFrameInfo
public String testLogMethodStackMapFrame(int a) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "peakmain";
}

替換方法體

默認(rèn)支持替換的方法
image.png
白名單

如果想讓某個(gè)類不被替換,可以在whiteList中添加,如:

monitorPlugin {
    whiteList = [
            "com.peakmain.asmactualcombat.utils.TestUtils",
            "com.peakmain.plugin"
    ]
}

必須寫到具體某個(gè)類,只寫到包名不可用,如:上面代碼寫到com.peakmain.asmactualcombat.utils并不能起到白名單作用

替換方法體
使用方式
  • 使用方式app.gradle中設(shè)置methodStatus為1
monitorPlugin {
    methodStatus = 1
}
自定義替換方法的返回值
SensorsDataAPI.getInstance().setOnReplaceMethodListener(new OnReplaceMethodListener() {
    @Override
    public String onReplaceMethodListener(int telephoneState, TelephonyManager manager, int slotIndex) {
        switch (telephoneState) {
            case SensorsDataConstants.GET_DEVICE_ID:
                LogUtils.e("替換GET_DEVICE_ID");
                break;
            case SensorsDataConstants.GET_MEID:
                LogUtils.e("替換GET_MEID");
                break;
            case SensorsDataConstants.GET_IMEI:
                LogUtils.e("替換GET_IMEI");
                break;
            case SensorsDataConstants.GET_SUBSCRIBER_ID:
                LogUtils.e("替換GET_SUBSCRIBER_ID");
                break;
            case SensorsDataConstants.GET_SIM_SERIAL_NUMBER:
                LogUtils.e("替換GET_SIM_SERIAL_NUMBER");
                break;
            default:
                break;
        }
        return "";
    }

    @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 "";
    }

    @Override
    public WifiInfo onReplaceMethodListener(WifiManager wifiManager) {
        LogUtils.e("替換WifiManager");
        return null;
    }

    @Override
    public String onReplaceMethodListener(SubscriptionInfo subscriptionInfo) {
        LogUtils.e("替換SubscriptionInfo");
        return "";
    }

    @Override
    public List<PackageInfo> onReplaceMethodListener(PackageManager manager) {
        LogUtils.e("替換PackageManager");
        return new ArrayList<>();
    }

    @Override
    public String onReplaceMethodListener(ContentResolver resolver, String name) {
        LogUtils.e("替換ContentResolver");
        return "onReplaceMethodListener";
    }
});
驗(yàn)證替換方法體

為了驗(yàn)證,我APP引入了極光推送的

未使用前

自身的APP

image.png

image.png

極光
關(guān)于極光,大家可以看這幾個(gè)類的相關(guān)方法

  • cn/jiguang/an/d的getConnectionInfo

  • cn/jiguang/ap/f的getConnectionInfo

  • cn/jiguang/l/b的getConnectionInfo

  • cn/jiguang/w/c的getDeviceId

  • cn/jiguang/f/a的getDeviceId

  • cn/jiguang/f/a的getConnectionInfo

  • cn/jiguang/f/a的getMacAddress

  • cn/jiguang/w/d的getDeviceId

  • 我就不帶大家看所有的了,我們挑幾個(gè)
    看cn/jiguang/f/a的getMacAddress和cn/jiguang/f/a的getDeviceId

  • 因?yàn)槲覀兛床坏皆创a,我們只能通過字節(jié)碼去看


    image.png
image.png
使用替換后
  • 添加依賴,app的build.gradle
monitorPlugin {
    whiteList = [
            "com.peakmain.asmactualcombat.utils.TestUtils",
            "com.peakmain.plugin"
    ]
    methodStatus = 1//替換方法體
    disableStackMapFrame = false
}

自身App

image.png

image.png

可以發(fā)現(xiàn)白名單生效了

極光推送

image.png

image.png

關(guān)于我

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容