preface

  • Gradle+ASM is a plugin for Gradle+ASM. Gradle+ASM is a plugin for Gradle+ASM
  • Github address: github.com/Peakmain/As…

Demand background

  • At the end of December, a new company was hired, and the new company’s project architecture needed to be rearchitecteded, hence the previous article :BasicLibrary — a framework based on Kotlin + Jetpack + MVVM to improve Android development efficiency
  • At the same time, the company’s project is correcting the privacy policy. One of the requirements is that when the privacy policy pops up, click cancel to enter the simple version of the APP instead of killing it directly
  • In fact, there are many ways to do the simple version of app. First, make an H5 directly; second, restrict some functions; third, enter the home page, but click any button to pop up the privacy pop-up box that requires authorization
  • Today we are going to focus on the third way

solution

  • Each button click event is judged before being intercepted
view.setOnClickListener(new OnClickListener() {
       public void onClick(View var1) {
                //isIntercept() sets itself whether to intercept the event
                boolean isIntercept =isIntercept();
                if(! isIntercept){return;
               }
              // Original event handling}});Copy the code
  • The downside: The downside is obvious, if you have 100 click events, you need to make a copy before each click
  • At this point we can consider using ASM bytecode staking to automatically add interception before each click event, we just need to set the interception event when the interception is needed
  • Look directly at the ASM compiled code that we have done

Function is introduced

  • You can dynamically configure whether to enable plug-ins. By default, plug-ins are enabled
  • The default solves most of the problems of multiple clicks
  • Methods can be dynamically set to intercept click events before they are processed
  • Get the elapsed time of the method

How to use

ASM plugin dependencies

  • Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

Buildscript {dependencies {classpath "IO. Making. Peakmain: plugin: 1.0.0"}}Copy the code
  • Step 2. Add the Use

Add it in your app module build.gradle at the end of repositories:

apply plugin: "com.peakmain.plugin"
Copy the code

Intercepting event SDK dependencies

  • 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' }
		}
	}
Copy the code
  • Step 2. Add the dependency
Dependencies {implementation 'com. Making. Peakmain: AsmActualCombat: while'}Copy the code

Using document

  • 1. Initialize in Application
  SensorsDataAPI.init(this);
Copy the code
  • 2. Dynamically set whether to enable the plug-in

In gradle.properties, set the following parameters, false means not to close the plug-in, true means to close the plug-in, the default is false

peakmainPlugin.disableAppClick=false
Copy the code
  • 3. Set the blocking event on the page to be blocked
 SensorsDataAPI.getInstance().setOnUserAgreementListener(new SensorsDataAPI.OnUserAgreementListener() {
           @Override
            public boolean onUserAgreement() {
            
        }
 })
Copy the code

The complete example code is as follows:

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) {// New alertDialog.builder = new alertdialog.builder .setContentView(r.layout.dialog_user_agreement).setCancelable(false).show(); / / set the user agreement to intercept events to NULL SensorsDataAPI. GetInstance () setOnUserAgreementListener (NULL); Dialog. SetOnClickListener (R.i d.s tv_refuse, v - > {/ / cancel button click event dialog. Dismiss (); setUserAgreementListener(activity); }); Dialog. SetOnClickListener (R.i d.s tv_agreement, v - > {/ / agree button click event com peakmain. UI. Utils. ToastUtils. ShowLong (" agreed "); PreferencesUtils.getInstance(activity).saveParams(Constants.HAS_ACCEPT_USER_PRIVACY, true); dialog.dismiss(); }); } return isAcceptUserPrivacy; }});Copy the code
  • 4. The acquisition method takes time

Add the following annotation above the method that needs to get the elapsed time

 @LogMessageTime
Copy the code

conclusion

  • It’s a handy library to use if you want to intercept code before clicking on an event
  • Of course, we had better own down, in their own projects run, and then change to their own, can directly let their B rise, so that your leadership to you sit up and take notice