方式一
一.定义一个APP名字叫做xxxxx,在他里面新建一个类Test,这个类的内容就简单的一个函数,目的是使我们在另外一个App中实例化这个类,并且调用他的方法.
package com.example.xxxxx;
public class Test {
public int add() {
return 2;
}
}
二,建立另外的一个app,姑且叫做ClassLoad,我们在他的里面编写以下的一个方法
public void ClassLoader(){
DexClassLoader loader = null;
Intent intent = new Intent("com.example.xxxxx", null);
PackageManager manager = getPackageManager();
try {
ApplicationInfo info = manager.getApplicationInfo("com.example.xxxxx", 0);
if(info==null){
System.out.println("--------NULL--------");
return;
}
System.out.println(info.packageName);
System.out.println(info.sourceDir);
String dexPathString = info.sourceDir;
String libPathString = info.nativeLibraryDir;
String optimizedDirectory = this.getApplicationInfo().dataDir;
loader = new DexClassLoader(dexPathString, optimizedDirectory, libPathString, this.getClass().getClassLoader());
try {
//加载包com.example.xxxxx下的Test类
Class> clazz = loader.loadClass(info.packageName+".Test");
//实例化对象
Object oj = clazz.newInstance();
//获取其中的add()方法
Method actionMethod = clazz.getMethod("add", null);
//执行add方法并返回其结果
Integer ret = (Integer)actionMethod.invoke(oj, null);
//打印结果
System.out.println("result---"+ret);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}三,我们先在手机中安装应用xxxxx,然后在手机中安装并运行ClassLoad,我们可以在LogCat中看到输出了result---2,这就说明为我们成功的加载了xxxxx中的类.
方式二
一,我们更改xxxxx的Test类,使其实现接口Comm然后再ClassLoad应用中建立一个包,这个包里只有这一个接口类,其代码为
package com.example.host.plugin;
public interface Comm {
void add();
}
二,我们打包Comm,将其导入xxxxx中(注意要用add lib添加到java pah),然后Test如下
public class Test implements Comm{
public void add() {
System.out.println("This is XXXXXX");
}
}
三,我们稍微更改LoadClass()
public void ClassLoader(){
DexClassLoader loader = null;
Intent intent = new Intent("com.example.xxxxx", null);
PackageManager manager = getPackageManager();
try {
ApplicationInfo info = manager.getApplicationInfo("com.example.xxxxx", 0);
if(info==null){
return;
}
System.out.println(info.packageName);
System.out.println(info.sourceDir);
String dexPathString = info.sourceDir;
String libPathString = info.nativeLibraryDir;
String optimizedDirectory = this.getApplicationInfo().dataDir;
loader = new DexClassLoader(dexPathString, optimizedDirectory, libPathString, this.getClass().getClassLoader());
try {
//加载包com.example.xxxxx下的Test类
Class> clazz = loader.loadClass(info.packageName+".Test");
//实例化对象
//----------------------------------------------------------以下为更改部分
Comm comm = (Comm) clazz.newInstance();
comm.add();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
经过运行,可见输出"This is XXXXXX"
附录
1.生成 comm.jar
编译ClassLoad后进入bin/classes
运行 jar -cvf 导出路径/comm.jar 包路径
如F:\programs\Host\bin\classes>jar -cvf f:/plugin.jar .\com\example\host\plugin
2.添加lib
点击Addd Library--->User Library---->UserLibrarys--->new(新建)--->输入名字--->add external JARS--->选择Comm所在的Jar包