-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,479 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/io/github/a13e300/tools/DexKitWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.a13e300.tools; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.luckypray.dexkit.DexKitBridge; | ||
|
||
import java.util.Objects; | ||
|
||
public class DexKitWrapper { | ||
|
||
static { | ||
try { | ||
System.loadLibrary("dexkit"); | ||
} catch (Throwable t) { | ||
Logger.e("failed to load dexkit", t); | ||
} | ||
} | ||
|
||
@NonNull | ||
public static DexKitBridge create(String apkPath) { | ||
return Objects.requireNonNull(DexKitBridge.create(apkPath), "dexkit failed to create"); | ||
} | ||
|
||
@NonNull | ||
public static DexKitBridge create(ClassLoader cl) { | ||
return Objects.requireNonNull(DexKitBridge.create(cl, true), "dexkit failed to create"); | ||
} | ||
|
||
@NonNull | ||
public static DexKitBridge create(ClassLoader cl, boolean useMemory) { | ||
return Objects.requireNonNull(DexKitBridge.create(cl, useMemory), "dexkit failed to create"); | ||
} | ||
|
||
@NonNull | ||
public static DexKitBridge create(byte[][] dex) { | ||
return Objects.requireNonNull(DexKitBridge.create(dex), "dexkit failed to create"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.github.a13e300.tools; | ||
|
||
|
||
import android.util.Log; | ||
|
||
import com.android.dx.DexMaker; | ||
import com.android.dx.TypeId; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.nio.ByteBuffer; | ||
|
||
import dalvik.system.InMemoryDexClassLoader; | ||
|
||
public class DexUtils { | ||
public static void log(String msg) { | ||
Logger.d("DexUtils log: " + msg); | ||
} | ||
public static Object make(ClassLoader classLoader, String name, String superName) { | ||
var maker = new DexMaker(); | ||
var superType = TypeId.get(superName); // LNeverCall$AB; | ||
var myType = TypeId.get(name); // LMyAB; | ||
maker.declare(myType, "Generated", Modifier.PUBLIC, superType); | ||
var printMethod = myType.getMethod(TypeId.VOID, "print", TypeId.STRING); | ||
var cstr = maker.declare(myType.getConstructor(), Modifier.PUBLIC); | ||
var superCstr = superType.getConstructor(); | ||
cstr.invokeDirect(superCstr, null, cstr.getThis(myType)); | ||
cstr.returnVoid(); | ||
var code = maker.declare(printMethod, Modifier.PUBLIC); | ||
var tag = code.newLocal(TypeId.STRING); | ||
code.loadConstant(tag, "DexUtils"); | ||
var p0 = code.getParameter(0, TypeId.STRING); | ||
var logI = TypeId.get(Log.class).getMethod(TypeId.INT, "i", TypeId.STRING, TypeId.STRING); | ||
var myLog = TypeId.get(DexUtils.class).getMethod(TypeId.VOID, "log", TypeId.STRING); | ||
code.invokeStatic(logI, null, tag, p0); | ||
code.invokeStatic(myLog, null, p0); | ||
code.returnVoid(); | ||
var dex = maker.generate(); | ||
try { | ||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) return null; | ||
var loader = new InMemoryDexClassLoader(ByteBuffer.wrap(dex), new HybridClassLoader(classLoader)); | ||
var clazz = loader.loadClass("MyAB"); | ||
Logger.d("class loader=" + clazz.getClassLoader()); | ||
var superClass = classLoader.loadClass("NeverCall$AB"); | ||
var inst = clazz.newInstance(); | ||
var m = superClass.getDeclaredMethod("call", superClass, String.class); | ||
m.setAccessible(true); | ||
m.invoke(null, inst, "test"); | ||
return inst; | ||
} catch (Throwable t) { | ||
Logger.e("dexutils load and call", t); | ||
} | ||
return null; | ||
} | ||
|
||
static class HybridClassLoader extends ClassLoader { | ||
private final ClassLoader mBase; | ||
public HybridClassLoader(ClassLoader parent) { | ||
mBase = parent; | ||
} | ||
|
||
@Override | ||
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { | ||
if (name.startsWith("io.github.a13e300.tools")) { | ||
return getClass().getClassLoader().loadClass(name); | ||
} | ||
try { | ||
return mBase.loadClass(name); | ||
} catch (ClassNotFoundException e) { | ||
return super.loadClass(name, resolve); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.