android – 通过classloader从dex文件访问应用程序类

我有一个应用程序,从服务器接收一个dex文件,然后将其保存在SD卡上并加载它

做一些函数.我从我的dex文件加载我的应用程序类如下

dex文件

public class Main  {


    public void onCreate() 
    {
        System.out.print("------------onCreate------------");
        try {
             final ClassLoader classloader = ClassLoader.getSystemClassLoader();
            final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("com.example.myapp.M");
            final Object myInstance  = classToLoad.newInstance();
            final Method doSomething = classToLoad.getMethod("doSomething");
            doSomething.invoke(myInstance);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

应用代码

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            final String libPath = Environment.getExternalStorageDirectory() + "/Lib/lib.apk";
            final File tmpDir = getDir("dex", 0);
            final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
            final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("com.example.lib.Main");
            final Object myInstance  = classToLoad.newInstance();
            final Method doSomething = classToLoad.getMethod("onCreate");
            doSomething.invoke(myInstance);
            } catch (Exception e) {
            e.printStackTrace();
        }
    }

我的应用程序中的M类

public class M {
public void doSomething()
{
    Log.e("doSomething", "--------------------doSomething----");
}
}

但它有我跟随错误.我的代码有什么问题

11-19 08:24:12.718: W/System.err(10867): java.lang.ClassNotFoundException: Didn't find class "com.example.myapp2.M" on path: .
11-19 08:24:12.738: W/System.err(10867):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
11-19 08:24:12.747: W/System.err(10867):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-19 08:24:12.758: W/System.err(10867):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-19 08:24:12.758: W/System.err(10867):    at com.example.lib.Main.onCreate(Main.java:15)
11-19 08:24:12.768: W/System.err(10867):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 08:24:12.788: W/System.err(10867):    at java.lang.reflect.Method.invoke(Method.java:511)
11-19 08:24:12.788: W/System.err(10867):    at com.example.myapp2.MainActivity.onCreate(MainActivity.java:28)
11-19 08:24:12.798: W/System.err(10867):    at android.app.Activity.performCreate(Activity.java:5104)
11-19 08:24:12.818: W/System.err(10867):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-19 08:24:12.828: W/System.err(10867):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 08:24:12.838: W/System.err(10867):    at android.os.Looper.loop(Looper.java:137)
11-19 08:24:12.847: W/System.err(10867):    at android.app.ActivityThread.main(ActivityThread.java:5041)
11-19 08:24:12.847: W/System.err(10867):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 08:24:12.847: W/System.err(10867):    at java.lang.reflect.Method.invoke(Method.java:511)
11-19 08:24:12.857: W/System.err(10867):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-19 08:24:12.898: W/System.err(10867):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-19 08:24:12.898: W/System.err(10867):    at dalvik.system.NativeStart.main(Native Method)

最佳答案 看看你是否遵循这些步骤.

>您是否将库文件作为APK或包含SD卡上的(Classes.dex)文件的JAR?
>将APK文件复制到应用程序导向器/data/data/your.app.name/tmp中的临时文件夹中.这一点非常重要,因为出于安全考虑,Android不允许您从SD卡上加载某些东西.
>使用DexClassLoader,如此处所述 – > How to load a Java class dynamically on android/dalvik?
>使用此类加载器实例加载任何动态类文件.

如果您有多个JAR文件,则将它们合并为一个DEX文件/ APK.

要将Java JAR文件或类转换为DEX,您需要使用Android SDK的dx.bat.这是一个ANT脚本示例.这也可以通过命令行使用类似的参数来完成.

<exec executable="dx.bat">
    <arg value="--dex" />
    <arg value="--output=${target.directory.fullpath}/${apk.name}" />
    <arg value="--positions=lines" />
    <arg path="${classes.directory.full.path}" />
</exec>
点赞