Commit d77a98bd authored by swift_gan's avatar swift_gan Committed by swift_gan

[XposedCompat]complete new implement

parent 8cb1f361
...@@ -38,7 +38,7 @@ cant hook if lined ...@@ -38,7 +38,7 @@ cant hook if lined
# how to use # how to use
```gradle ```gradle
implementation 'com.swift.sandhook:hooklib:3.1.0' implementation 'com.swift.sandhook:hooklib:4.0.0'
``` ```
## Annotation API ## Annotation API
...@@ -115,7 +115,7 @@ SanHook.public static boolean hook(Member target, Method hook, Method backup) {} ...@@ -115,7 +115,7 @@ SanHook.public static boolean hook(Member target, Method hook, Method backup) {}
if hookers is in plugin(like xposed): if hookers is in plugin(like xposed):
```groovy ```groovy
provided 'com.swift.sandhook:hookannotation:3.1.0' provided 'com.swift.sandhook:hookannotation:4.0.0'
``` ```
in your plugin in your plugin
...@@ -127,18 +127,30 @@ backup method can call itself to avoid be inlining ...@@ -127,18 +127,30 @@ backup method can call itself to avoid be inlining
-------------------------------------------------------------------- --------------------------------------------------------------------
Now you can use Xposed api: Now you can use Xposed api:
We have two different implements:
```groovy ```groovy
implementation 'com.swift.sandhook:xposedcompat:3.1.0' //stable
implementation 'com.swift.sandhook:xposedcompat:4.0.0'
//or
//hook fast first time
implementation 'com.swift.sandhook:xposedcompat_new:4.0.0'
``` ```
```java ```java
//setup for xposed //setup for xposed
//for xposed compat only(no need xposed comapt new)
XposedCompat.cacheDir = getCacheDir(); XposedCompat.cacheDir = getCacheDir();
//for load xp module(sandvxp)
XposedCompat.context = this; XposedCompat.context = this;
XposedCompat.classLoader = getClassLoader(); XposedCompat.classLoader = getClassLoader();
XposedCompat.isFirstApplication= true; XposedCompat.isFirstApplication= true;
//do hook //do hook
XposedHelpers.findAndHookMethod(Activity.class, "onResume", new XC_MethodHook() { XposedHelpers.findAndHookMethod(Activity.class, "onResume", new XC_MethodHook() {
@Override @Override
......
...@@ -27,7 +27,7 @@ dependencies { ...@@ -27,7 +27,7 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':hooklib') implementation project(':hooklib')
implementation project(':nativehook') //implementation project(':nativehook')
//implementation project(':xposedcompat') implementation project(':xposedcompat')
implementation project(':xposedcompat_new') //implementation project(':xposedcompat_new')
} }
...@@ -5,7 +5,6 @@ import android.app.Application; ...@@ -5,7 +5,6 @@ import android.app.Application;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import com.swift.sandhook.nativehook.NativeHook;
import com.swift.sandhook.test.TestClass; import com.swift.sandhook.test.TestClass;
import com.swift.sandhook.testHookers.ActivityHooker; import com.swift.sandhook.testHookers.ActivityHooker;
import com.swift.sandhook.testHookers.CtrHook; import com.swift.sandhook.testHookers.CtrHook;
...@@ -15,7 +14,7 @@ import com.swift.sandhook.testHookers.LogHooker; ...@@ -15,7 +14,7 @@ import com.swift.sandhook.testHookers.LogHooker;
import com.swift.sandhook.testHookers.NewAnnotationApiHooker; import com.swift.sandhook.testHookers.NewAnnotationApiHooker;
import com.swift.sandhook.testHookers.ObjectHooker; import com.swift.sandhook.testHookers.ObjectHooker;
import com.swift.sandhook.wrapper.HookErrorException; import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.xposedcompat_new.XposedCompat; import com.swift.sandhook.xposedcompat.XposedCompat;
import dalvik.system.DexClassLoader; import dalvik.system.DexClassLoader;
import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XC_MethodHook;
...@@ -30,7 +29,6 @@ public class MyApp extends Application { ...@@ -30,7 +29,6 @@ public class MyApp extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
NativeHook.test();
SandHookConfig.DEBUG = BuildConfig.DEBUG; SandHookConfig.DEBUG = BuildConfig.DEBUG;
...@@ -58,24 +56,28 @@ public class MyApp extends Application { ...@@ -58,24 +56,28 @@ public class MyApp extends Application {
e.printStackTrace(); e.printStackTrace();
} }
//setup for xposed\ //for xposed compat(no need xposed comapt new)
XposedCompat.cacheDir = getCacheDir();
//for load xp module(sandvxp)
XposedCompat.context = this; XposedCompat.context = this;
XposedCompat.classLoader = getClassLoader(); XposedCompat.classLoader = getClassLoader();
XposedCompat.isFirstApplication= true; XposedCompat.isFirstApplication= true;
// XposedHelpers.findAndHookMethod(Activity.class, "onResume", new XC_MethodHook() { XposedHelpers.findAndHookMethod(Activity.class, "onResume", new XC_MethodHook() {
// @Override @Override
// protected void beforeHookedMethod(MethodHookParam param) throws Throwable { protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
// super.beforeHookedMethod(param); super.beforeHookedMethod(param);
// Log.e("XposedCompat", "beforeHookedMethod: " + param.method.getName()); Log.e("XposedCompat", "beforeHookedMethod: " + param.method.getName());
// } }
//
// @Override @Override
// protected void afterHookedMethod(MethodHookParam param) throws Throwable { protected void afterHookedMethod(MethodHookParam param) throws Throwable {
// super.afterHookedMethod(param); super.afterHookedMethod(param);
// Log.e("XposedCompat", "afterHookedMethod: " + param.method.getName()); Log.e("XposedCompat", "afterHookedMethod: " + param.method.getName());
// } }
// }); });
XposedHelpers.findAndHookMethod(MainActivity.class, "testStub", TestClass.class, int.class, String.class, boolean.class, char.class, String.class, new XC_MethodHook() { XposedHelpers.findAndHookMethod(MainActivity.class, "testStub", TestClass.class, int.class, String.class, boolean.class, char.class, String.class, new XC_MethodHook() {
......
...@@ -30,7 +30,7 @@ ext { ...@@ -30,7 +30,7 @@ ext {
userOrg = 'ganyao114' userOrg = 'ganyao114'
groupId = 'com.swift.sandhook' groupId = 'com.swift.sandhook'
repoName = 'SandHook' repoName = 'SandHook'
publishVersion = '3.4.6' publishVersion = '4.0.0'
desc = 'android art hook' desc = 'android art hook'
website = 'https://github.com/ganyao114/SandHook' website = 'https://github.com/ganyao114/SandHook'
licences = ['Apache-2.0'] licences = ['Apache-2.0']
......
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.swift.sandhook.xposedcompat_new" /> package="com.swift.sandhook.xposedcompat" />
...@@ -280,7 +280,7 @@ FFIClosure* BuildJniClosure(ArtHookParam *param) { ...@@ -280,7 +280,7 @@ FFIClosure* BuildJniClosure(ArtHookParam *param) {
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_xposedcompat_1new_XposedCompat_init(JNIEnv *env, jclass type, jclass jbridgeClass, jobject jbridgeMethod, jclass jobjClass) { Java_com_swift_sandhook_xposedcompat_XposedCompat_init(JNIEnv *env, jclass type, jclass jbridgeClass, jobject jbridgeMethod, jclass jobjClass) {
java_lang_Object = static_cast<jclass>(env->NewGlobalRef(jobjClass)); java_lang_Object = static_cast<jclass>(env->NewGlobalRef(jobjClass));
bridgeClass = static_cast<jclass>(env->NewGlobalRef(jbridgeClass)); bridgeClass = static_cast<jclass>(env->NewGlobalRef(jbridgeClass));
bridgeMethod = env->FromReflectedMethod(jbridgeMethod); bridgeMethod = env->FromReflectedMethod(jbridgeMethod);
...@@ -297,7 +297,7 @@ Java_com_swift_sandhook_xposedcompat_1new_XposedCompat_init(JNIEnv *env, jclass ...@@ -297,7 +297,7 @@ Java_com_swift_sandhook_xposedcompat_1new_XposedCompat_init(JNIEnv *env, jclass
extern "C" extern "C"
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_com_swift_sandhook_xposedcompat_1new_XposedCompat_getJNITrampoline(JNIEnv *env, jclass type, jint slot, jboolean isStatic, jchar retShorty, jcharArray paramShorty) { Java_com_swift_sandhook_xposedcompat_XposedCompat_getJNITrampoline(JNIEnv *env, jclass type, jint slot, jboolean isStatic, jchar retShorty, jcharArray paramShorty) {
ArtHookParam* artHookParam = new ArtHookParam(); ArtHookParam* artHookParam = new ArtHookParam();
hookParams.push_back(artHookParam); hookParams.push_back(artHookParam);
artHookParam->is_static_ = isStatic; artHookParam->is_static_ = isStatic;
...@@ -321,6 +321,71 @@ Java_com_swift_sandhook_xposedcompat_1new_XposedCompat_getJNITrampoline(JNIEnv * ...@@ -321,6 +321,71 @@ Java_com_swift_sandhook_xposedcompat_1new_XposedCompat_getJNITrampoline(JNIEnv *
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_xposedcompat_1new_XposedCompat_addHookMethod(JNIEnv *env, jclass type, jobject hookMethod) { Java_com_swift_sandhook_xposedcompat_XposedCompat_addHookMethod(JNIEnv *env, jclass type, jobject hookMethod) {
hookMethods.insert(env->FromReflectedMethod(hookMethod)); hookMethods.insert(env->FromReflectedMethod(hookMethod));
} }
static JNINativeMethod jniXpCompat[] = {
{
"init",
"(Ljava/lang/Class;Ljava/lang/reflect/Method;Ljava/lang/Class;)V",
(void *) Java_com_swift_sandhook_xposedcompat_XposedCompat_init
},
{
"getJNITrampoline",
"(IZC[C)J",
(void *) Java_com_swift_sandhook_xposedcompat_XposedCompat_getJNITrampoline
},
{
"addHookMethod",
"(Ljava/lang/reflect/Member;)V",
(void *) Java_com_swift_sandhook_xposedcompat_XposedCompat_addHookMethod
}
};
static bool registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *jniMethods, int methods) {
jclass clazz = env->FindClass(className);
if (clazz == NULL) {
return false;
}
return env->RegisterNatives(clazz, jniMethods, methods) >= 0;
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
const char* CLASS_XP_COMPAT = "com/swift/sandhook/xposedcompat/XposedCompat";
int jniMethodSize = sizeof(JNINativeMethod);
JNIEnv *env = NULL;
if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
if (!registerNativeMethods(env, CLASS_XP_COMPAT, jniXpCompat, sizeof(jniXpCompat) / jniMethodSize)) {
return -1;
}
LOGW("JNI Loaded");
return JNI_VERSION_1_6;
}
extern "C"
JNIEXPORT bool JNI_Load_Ex(JNIEnv* env, jclass classXpComapt) {
int jniMethodSize = sizeof(JNINativeMethod);
if (env == nullptr || classXpComapt == nullptr)
return false;
if (env->RegisterNatives(classXpComapt, jniXpCompat, sizeof(jniXpCompat) / jniMethodSize) < 0) {
return false;
}
LOGW("JNI Loaded");
return true;
}
package com.swift.sandhook.xposedcompat_new; package com.swift.sandhook.xposedcompat;
import java.lang.reflect.Member; import java.lang.reflect.Member;
import java.lang.reflect.Method; import java.lang.reflect.Method;
......
package com.swift.sandhook.xposedcompat_new; package com.swift.sandhook.xposedcompat;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import com.swift.sandhook.HookLog; import com.swift.sandhook.HookLog;
import com.swift.sandhook.SandHook; import com.swift.sandhook.SandHook;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.wrapper.HookErrorException; import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
import com.swift.sandhook.wrapper.StubMethodsFactory; import com.swift.sandhook.wrapper.StubMethodsFactory;
import com.swift.sandhook.xposedcompat_new.utils.ApplicationUtils; import com.swift.sandhook.xposedcompat.utils.ApplicationUtils;
import com.swift.sandhook.xposedcompat_new.utils.ClassUtils; import com.swift.sandhook.xposedcompat.utils.ClassUtils;
import com.swift.sandhook.xposedcompat_new.utils.ComposeClassLoader; import com.swift.sandhook.xposedcompat.utils.ComposeClassLoader;
import com.swift.sandhook.xposedcompat_new.utils.ProcessUtils; import com.swift.sandhook.xposedcompat.utils.ProcessUtils;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Member; import java.lang.reflect.Member;
...@@ -32,6 +33,14 @@ import de.robv.android.xposed.callbacks.XC_LoadPackage; ...@@ -32,6 +33,14 @@ import de.robv.android.xposed.callbacks.XC_LoadPackage;
*/ */
public class XposedCompat { public class XposedCompat {
public volatile static boolean inited = false;
public volatile static SandHookConfig.LibLoader libLoader = new SandHookConfig.LibLoader() {
@Override
public void loadLib() {
System.loadLibrary("sandhook-xp");
}
};
public static Context context; public static Context context;
public static volatile ClassLoader classLoader; public static volatile ClassLoader classLoader;
public static String packageName; public static String packageName;
...@@ -44,17 +53,20 @@ public class XposedCompat { ...@@ -44,17 +53,20 @@ public class XposedCompat {
public static volatile HookInfo[] hookInfos = new HookInfo[100]; public static volatile HookInfo[] hookInfos = new HookInfo[100];
static { static {
System.loadLibrary("sandhook-xp");
try { try {
init(); init();
} catch (NoSuchMethodException e) { } catch (Throwable throwable) {
throw new RuntimeException("init XposedCompat error!", e); throwable.printStackTrace();
} }
} }
public static void init() throws NoSuchMethodException { public static boolean init() throws Throwable {
if (inited) return true;
libLoader.loadLib();
Method bridgeMethod = XposedCompat.class.getDeclaredMethod("hookBridge", int.class, Object.class, Object[].class); Method bridgeMethod = XposedCompat.class.getDeclaredMethod("hookBridge", int.class, Object.class, Object[].class);
init(XposedCompat.class, bridgeMethod, Object.class); init(XposedCompat.class, bridgeMethod, Object.class);
inited = true;
return true;
} }
private native static void init(Class bridgeClass, Method bridgeMethod, Class ObjClass); private native static void init(Class bridgeClass, Method bridgeMethod, Class ObjClass);
......
package com.swift.sandhook.xposedcompat_new.utils; package com.swift.sandhook.xposedcompat.utils;
import android.app.Application; import android.app.Application;
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.swift.sandhook.xposedcompat_new.utils; package com.swift.sandhook.xposedcompat.utils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
......
package com.swift.sandhook.xposedcompat_new.utils; package com.swift.sandhook.xposedcompat.utils;
public class ComposeClassLoader extends ClassLoader { public class ComposeClassLoader extends ClassLoader {
......
package com.swift.sandhook.xposedcompat_new.utils; package com.swift.sandhook.xposedcompat.utils;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
......
...@@ -5,7 +5,7 @@ import android.util.Log; ...@@ -5,7 +5,7 @@ import android.util.Log;
import com.swift.sandhook.HookLog; import com.swift.sandhook.HookLog;
import com.swift.sandhook.SandHook; import com.swift.sandhook.SandHook;
import com.swift.sandhook.xposedcompat_new.XposedCompat; import com.swift.sandhook.xposedcompat.XposedCompat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
......
...@@ -2,7 +2,7 @@ package de.robv.android.xposed.callbacks; ...@@ -2,7 +2,7 @@ package de.robv.android.xposed.callbacks;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import com.swift.sandhook.xposedcompat_new.XposedCompat; import com.swift.sandhook.xposedcompat.XposedCompat;
import de.robv.android.xposed.IXposedHookLoadPackage; import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XposedBridge.CopyOnWriteSortedSet; import de.robv.android.xposed.XposedBridge.CopyOnWriteSortedSet;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment