我想在运行时向 android 应用程序中注入(inject)代码。我曾尝试使用 dx 工具在 sdcard 中生成一个 dexfile,但是当我想实例化时,它失败了。是否有任何工具可以注入(inject)生成新的 dalvik 字节码的代码?我正在研究一些图书馆,aspecjt 或 guice for android。使用脚本语言更好吗?
谢谢大家 :)
最佳答案
Dexmaker是新的,专为此而设计。以下是项目网站示例的一部分:
DexMaker dexMaker = new DexMaker();
// Generate a HelloWorld class.
TypeId<?> helloWorld = TypeId.get("LHelloWorld;");
dexMaker.declare(helloWorld, "HelloWorld.generated", Modifier.PUBLIC, TypeId.OBJECT);
generateHelloMethod(dexMaker, helloWorld);
// Create the dex file and load it.
File outputDir = new File(".");
ClassLoader loader = dexMaker.generateAndLoad(HelloWorldMaker.class.getClassLoader(),
outputDir, outputDir);
Class<?> helloWorldClass = loader.loadClass("HelloWorld");
// Execute our newly-generated code in-process.
helloWorldClass.getMethod("hello").invoke(null);
关于android - 是否可以在 Android 应用程序中注入(inject)代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4260231/