Android remote code loading

后端 未结 3 987
孤街浪徒
孤街浪徒 2020-12-07 11:08

I am developing a library for Android that requires frequent updates from a central server. I was thinking how nice it would be if my library could update itself -- or if I

3条回答
  •  情歌与酒
    2020-12-07 11:33

    I've successfully used DexClassLoader. It's important to provide a dexOutputDir that is actually writeable by your app, so not /data/dalvik-cache. Otherwise the log will show one or two lines about failing to write there, followed by ClassNotFoundException.

    cl = new DexClassLoader("/full/path/com.example.apk",
                            getFilesDir().getAbsolutePath(),// /data/data/foo/files
                            null,  // native lib path, I haven't used this
                            MyClass.class.getClassLoader());
    // This doesn't make Class.forName() work, instead I do this:
    Class foo = cl.loadClass("com.example.foo");
    

    To make Class.forName() work, you could try Thread.setContextClassLoader() (I haven't).

提交回复
热议问题