Skip to content

daisuke-nomura/crash

Repository files navigation

crash: Cause a crash at @UiThread/@WorkerThread annotated method

Release

crash cause a crash explicitly at @UiThread/@MainThread or @WorkerThread annotated method if an unmatched thread is working.
This library is inspired by Jake Wharton's Hugo.

@UiThread/@MainThread/@WorkerThread warnings of Android Studio are not useful on callbacks. So I tried to forcibly crash at run time.

NOTE: To prevent crashes, REMOVE THIS LIBRARY on release build.

Sample usage

Cause a crash at @UiThread/@MainThread if thread is NOT UI/main thread. Throws ExecuteOnWorkerThreadException.

new AsyncTask<Void, Void, Void>() {
    @UiThread
    @Override
    protected Void doInBackground(Void... params) {
        //crash here
        return null;
    }
}.execute();

Cause a crash at @WorkerThread if thread is main thread. Throws ExecuteOnMainThreadException.

//create handler on UI/main thread
handler.postDelayed(new Runnable() {
    @WorkerThread
    @Override
    public void run() {
        //crash here
    }
}, 0);

Binaries

Add aspectjx plugin. aspectjx is great and very useful for AspectJ support.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:1.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

apply plugin

apply plugin: 'android-aspectjx'

and

dependencies {
    debugImplementation 'com.github.daisuke-nomura:crash:1.0.2'
}

Bugs and Feedback

Please use GitHub Issues.

License

Copyright 2017 Daisuke Nomura

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Reference

I referred Hugo and android 10 coder article, stackoverflow, Qiita, AspectJX-Demo to build AspectJ code.