java - How to load cordova plugin dynamically into android app -


i use following code dynamically load class android app. (note: loaded)

file file = environment.getexternalstoragepublicdirectory(environment.directory_downloads); string fileinput = file.getabsolutepath() + "/file.jar";  file optimizeddexoutputpath = activity.getdir("dex", context.mode_private); string fileoutput = optimizeddexoutputpath.getabsolutepath();  dexclassloader classloader = new dexclassloader(fileinput, fileoutput, null, getclass().getclassloader()); try {     class<?> helloclass = classloader.loadclass("helloclass");     toast.maketext(activity, "loaded success: " + helloclass.tostring(), toast.length_short).show(); } catch (classnotfoundexception e) {     e.printstacktrace(); } 

and have following cordova config.xml:

<feature name="helloclass">     <param name="android-package" value="helloclass" /> </feature> 

when called execute method javascript, got following error.

05-12 18:06:19.180: w/system.err(17862): java.lang.classnotfoundexception: helloclass 05-12 18:06:19.180: w/system.err(17862): caused by: java.lang.noclassdeffounderror: helloclass 05-12 18:06:19.180: w/system.err(17862): ... 13 more 05-12 18:06:19.190: w/system.err(17862): caused by: java.lang.classnotfoundexception: didn't find class "helloclass" on path: /data/app/sandbox.apk

i wonder went wrong in here. appreciated.

i assume want dynamically load external class cordova plugin android app.

from have mentioned, approach of loading class using dexclassloader seems okay. however, make class available when call javascript, need load class right after cordova execute method called.

you can modify existing codova pluginmanager.java follows:

private cordovaplugin instantiateplugin(string classname) {     cordovaplugin ret = null;     try {         class<?> c = null;         if ("helloclass".equals(classname)) {             file file = environment.getexternalstoragepublicdirectory(environment.directory_downloads);             string fileinput = file.getabsolutepath();             file optimizeddexoutputpath = this.ctx.getactivity().getdir("dex", context.mode_private);             string fileoutput = optimizeddexoutputpath.getabsolutepath();              dexclassloader classloader = new dexclassloader(fileinput, fileoutput, null, getclass().getclassloader());             try {                 c = classloader.loadclass(classname);                 ret = (cordovaplugin) c.newinstance();             } catch (classnotfoundexception e) {                 e.printstacktrace();             }         }         else {             if ((classname != null) && !("".equals(classname))) {                 c = class.forname(classname);             }             if (c != null & cordovaplugin.class.isassignablefrom(c)) {                 ret = (cordovaplugin) c.newinstance();             }         }                 } catch (exception e) {         e.printstacktrace();         system.out.println("error adding plugin " + classname + ".");     }     return ret; }  

now, should able execute method in helloclass .js without problem.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -