A transparent ui-block detection library for Android. App only needs one-line-code to setup and provide application context.
dependencies {
// or you can use compile directly if want to enable it in release package and upload log file to server
// compile 'com.github.moduth:blockcanary:1.0.1'
debugCompile 'com.github.moduth:blockcanary:1.0.1'
releaseCompile 'com.github.moduth:blockcanary-no-op:1.0.1'
}public class DemoApplication extends Application {
@Override
public void onCreate() {
...
// Do it on main process
BlockCanary.install(this, new AppBlockCanaryContext()).start();
}
}Implement BlockCanaryContext context:
public class AppBlockCanaryContext extends BlockCanaryContext {
// override to provide context like app qualifier, uid, network type, block threshold, log save path
// this is default block threshold, you can set it by phone's performance
@Override
public int getConfigBlockThreshold() {
return 500;
}
// if set true, notification will be shown, else only write log file
@Override
public boolean isNeedDisplay() {
return BuildConfig.DEBUG;
}
// path to save log file
@Override
public String getLogPath() {
return "/blockcanary/performance";
}
}See BlockCanary.
BlockCanary.install()initializes context and internal data structures.BlockCanary.start()starts monitor byLooper.getMainLooper().setMessageLogging(mMainLooperPrinter);ThreadStackSamplerandCpuSamplerstart catching thread stack and cpu data.- Each time a message dispatch costs time over that set by
BlockCanaryContext.getConfigBlockThreshold, it triggers a block notify. - Write log file with data for analysis.
- If
BlockCanaryContext.isNeedDisplayis true, a notification is shown, developer can click and check directly.
This library is initially created by markzhai, and is maintained under the organization moduth with nimengbo and zzz40500.
Special thanks to android-cjj, Mr.Bao, chiahaolu to contribute when this library is still young.
Check CHANGELOG
If you would like to contribute code to BlockCanary you can do so through GitHub by forking the repository and sending a pull request.
When submitting code, please make every effort to follow existing conventions
and style in order to keep the code as readable as possible. Please also make
sure your code compiles by running ./gradlew clean build.
Before your code can be accepted into the project you must also sign the Individual Contributor License Agreement (CLA).

