android dexclassloader 资源,【Android】利用DexClassLoader加载别的App中的Class文件

方式一

一.定义一个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

0818b9ca8b590ca3270a3433284dd417.png

点击Addd Library--->User Library---->UserLibrarys--->new(新建)--->输入名字--->add external JARS--->选择Comm所在的Jar包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值