Project Url: ganyao114/SandHook
Introduction: Android ART Hook - support 5.0 - 9.0 32/64 bit
More: Author   ReportBugs   

Android ART Hook



arch support

  • ARM64
  • ARM32(no tested)
  • Thumb-2


5.0 - 9.0


  • Object Methods
  • Static Methods
  • Constructors
  • System Methods
  • JNI Methods

hook abstract method is not recommended, you can invoke its impl method.

cant hook if lined

how to use

implementation 'com.swift.sandhook:hooklib:2.5.2'
  • Annotation API

  • hook method must be a static method
  • first par must be this if method is not static
  • method description must "same"(can be isAssignableFrom) with origin method
  • backup method same with above
public class ActivityHooker {

    // can invoke to call origin
    static Method onCreateBackup;

    static Method onPauseBackup;

    public static void onCreate(Activity thiz, Bundle bundle) {
        Log.e("ActivityHooker", "hooked onCreate success " + thiz);
        onCreateBackup(thiz, bundle);

    public static void onCreateBackup(Activity thiz, Bundle bundle) {
        //invoke self to kill inline
        onCreateBackup(thiz, bundle);

    public static void onPause(Activity thiz) {
        Log.e("ActivityHooker", "hooked onPause success " + thiz);

    public static void onPauseBackup(Activity thiz) {
        //invoke self to kill inline


//or like this:

public class NewAnnotationApiHooker {

    public static void onTestNewHookApi(@ThisObject TestClass thiz, @Param("com.swift.sandhook.MainActivity") Activity activity, int a) {
        Log.e("TestClassHook", "testNewHookApi been hooked");
        onTestNewHookApiBackup(thiz, activity, a);

    public static void onTestNewHookApiBackup(@ThisObject TestClass thiz, @Param("com.swift.sandhook.MainActivity") Activity activity, int a) {
        onTestNewHookApiBackup(thiz, activity, a);


SandHook.addHookClass(CtrHook.class, LogHooker.class, CustmizeHooker.class, ActivityHooker.class, ObjectHooker.class);

you can also use:
SanHook.public static boolean hook(Member target, Method hook, Method backup) {}

if hookers is in plugin(like xposed):

provided 'com.swift.sandhook:hookannotation:2.5.2'

in your plugin

if OS <= 5.1 backup method can call itself to avoid be inlining

  • Xposed API

Now you can use Xposed api:

implementation 'com.swift.sandhook:xposedcompat:2.5.2'
//setup for xposed
XposedCompat.cacheDir = getCacheDir();
XposedCompat.context = this;
XposedCompat.classLoader = getClassLoader();
XposedCompat.isFirstApplication= true;  
//do hook
XposedHelpers.findAndHookMethod(Activity.class, "onResume", new XC_MethodHook() {
      protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
          Log.e("XposedCompat", "beforeHookedMethod: " + param.method.getName());

      protected void afterHookedMethod(MethodHookParam param) throws Throwable {
          Log.e("XposedCompat", "afterHookedMethod: " + param.method.getName());



when OS >= 8.0 you must call backup method in hook method, if you want call it in other method, please call SandHook.compileMethod(otherMethod) before call backup method.

because when ART trigger JIT from profiling, JIT will invoke -> ResolveCompilingMethodsClass -> ClassLinker::ResolveMethod -> CheckIncompatibleClassChange -> ThrowIncompatibleClassChangeError finally!!!


Support Me
About Me
Google+: Trinea trinea
GitHub: Trinea