Skip to content

Using ASM to handle Android's click debounce, specially a quick double click.

License

Notifications You must be signed in to change notification settings

SmartDengg/click-debounce

Repository files navigation

README.md

click-debounce

Support incremental compilation! Support parallel compilation! Faster compilation speed, and shorter compilation time.

It is a gradle plugin that uses bytecode weaving technology to solve the click jitter problem of Android applications.

For example, a normal onClick method without any debounce plan, multiple quick clicks may trigger multiple Activity starts:

  @Override public void onClick(View v) {
    startActivity(new Intent(MainActivity.this, SecondActivity.class));
  }

modify the bytecode at compile time to:

  @Debounced
  public void onClick(View var1) {
    if (DebouncedPredictor.shouldDoClick(var1)) {
      startActivity(new Intent(this, SecondActivity.class));
    }
  }

The @Debounced annotation indicates that the method has been debounced. The shouldDoClick(View) method will determine which are the jitters and which are the correct clicks.

I also wrote a BLOG to share my ideas to solve the click jitter.

Note: This repository is just a gradle plugin, responsible for bytecode weaving work. Android runtime library please move here.

Requirements

  • JDK 1.7 +
  • Gradle 4.0.0 +

To build

$ git clone git@github.com:SmartDengg/asm-clickdebounce.git
$ cd asm-clickdebounce/
$ ./gradlew build

Getting Started

Step 1. Add the JitPack repository and the plugin to your buildscript:

buildscript {

    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        ...
        classpath 'com.github.SmartDengg.click-debounce:click-debounce-gradle-plugin:2.1.0'
    }
}

Step 2. Apply it in your module:

Supports com.android.application, com.android.library and com.android.feature.

apply plugin: 'smartdengg.clickdebounce'
// or apply plugin: 'clickdebounce'

Step 3 (Optional). By adding the following code to your build.gradle to enable printe the beautiful log or add an exclusive list to indicate which methods do not need to be debounced. By default, the log is not printed, and process all the methods in the support list.

It is not recommended to manually add @Debounce annotations to methods, you should use the exclusive feature that which methods should not be debounced, as follows:

debounce {
  // enable log
  loggable = true
  // java bytecode descriptor: [class: [methods]]
  exclusion = ["com/smartdengg/clickdebounce/ClickProxy": ['onClick(Landroid/view/View;)V',
                                                           'onItemClick(Landroid/widget/AdapterView;Landroid/view/View;IJ)V']]
}

Step 4 (Optional). Set the debounce window time in your Java code(default is 300 milliseconds):

DebouncedPredictor.FROZEN_WINDOW_MILLIS = 400L

Artifact

We output some log files to help developers better understand the build information. These file path is located in buildDir/outputs/debounce/logs//, as follows:

.
+-- app (apply this AGP)
|   +-- build
|       +-- generated
|       +-- intermediates
|       +-- outputs
|           +-- debounce
|               +-- logs
|                   +-- debug
|                       +-- files.txt
|                       +-- classes.txt
+-- build.gradle
+-- gradle.properties
+-- gradlew
+-- gradlew.bat
+-- settings.gradle

  • files.txt :Record the class files consumed by this build,it can help you better understand this build.
  • classes.txt :Record information about the classes and methods woven in this build.

How it works

Will not intercept the touch event delivery, only intercepted in the callback of the click event, so that it will not be passed to the business logic.

Support

R8 / ProGuard (Not Required)

Pure bytecode weaving without any reflections. No Proguard rules are required.

Bugs and Feedback

For bugs, feature requests, and discussion please use GitHub Issues. Or send email to hi4joker@gmail.com.

Found this project useful

❤️ Hope this article can help you. Support by clicking the ⭐, or share it with people around you. ❤️

About me

email : hi4joker@gmail.com

blog : 小鄧子

weibo : -小鄧子-

License

See the LICENSE file for license rights and limitations (MIT).