Java 如何将cordova插件动态加载到android应用程序中

Java 如何将cordova插件动态加载到android应用程序中,java,android,cordova,cordova-plugins,Java,Android,Cordova,Cordova Plugins,我使用以下代码将我的类动态加载到android应用程序。(注意:已成功加载) File File=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u下载); 字符串fileInput=file.getAbsolutePath()+“/file.jar”; 文件optimizedDexOutputPath=activity.getDir(“dex”,Context.MODE\u PRIVATE); String

我使用以下代码将我的类动态加载到android应用程序。(注意:已成功加载)

File File=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u下载);
字符串fileInput=file.getAbsolutePath()+“/file.jar”;
文件optimizedDexOutputPath=activity.getDir(“dex”,Context.MODE\u PRIVATE);
String fileOutput=optimizedDexOutputPath.getAbsolutePath();
DexClassLoader classLoader=新的DexClassLoader(fileInput,fileOutput,null,getClass().getClassLoader());
试一试{
类helloClass=classLoader.loadClass(“helloClass”);
Toast.makeText(活动,“加载成功:+helloClass.toString(),Toast.LENGTH\u SHORT.show();
}catch(classnotfounde异常){
e、 printStackTrace();
}
我有以下cordova config.xml文件:

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

当我从javascript调用execute方法时,我得到了以下错误

05-12 18:06:19.180:W/System.err(17862):java.lang.ClassNotFoundException:HelloClass 05-12 18:06:19.180:W/System.err(17862):原因:java.lang.NoClassDefFoundError:HelloClass 05-12 18:06:19.180:带系统错误(17862):。。。还有13个 05-12 18:06:19.190:W/System.err(17862):原因:java.lang.ClassNotFoundException:在路径:/data/app/sandbox.apk上未找到类“HelloClass”


我想知道这里出了什么问题。非常感谢您的帮助。

我假设您希望将外部类从cordova插件动态加载到android应用程序中

从前面提到的内容来看,您使用DexClassLoader加载类的方法似乎还可以。但是,要使该类在从javascript调用时可用,需要在调用cordova execute方法之后立即加载该类

您可以修改现有的codova PluginManager.java,如下所示:

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;
} 
private CordovaPlugin实例化plugin(字符串类名称){
CordovaPlugin ret=null;
试一试{
c类=空;
if(“HelloClass.equals(className)){
File File=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u下载);
字符串fileInput=file.getAbsolutePath();
文件optimizedDexOutputPath=this.ctx.getActivity().getDir(“dex”,Context.MODE\u PRIVATE);
String fileOutput=optimizedDexOutputPath.getAbsolutePath();
DexClassLoader classLoader=新的DexClassLoader(fileInput,fileOutput,null,getClass().getClassLoader());
试一试{
c=classLoader.loadClass(类名);
ret=(CordovaPlugin)c.newInstance();
}catch(classnotfounde异常){
e、 printStackTrace();
}
}
否则{
如果((className!=null)和(“”.equals(className))){
c=类。forName(类名称);
}
if(c!=null&CordovaPlugin.class.isAssignableFrom(c)){
ret=(CordovaPlugin)c.newInstance();
}
}            
}捕获(例外e){
e、 printStackTrace();
System.out.println(“添加插件“+className+”时出错”);
}
返回ret;
} 
现在,您应该能够从.js中执行HelloClass中的方法,而不会出现任何问题

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;
}