用类加载器加载,可以自定义ClassLoader , jar文件可以放到assert目录下。
解决方案 »
- fedora15 上android运行不了???
- 求救关于AndroidManifest.xml
- android out of memory for surface
- 求助(急):service中不能通过BroadcastReceiver的onReceive获取按键事件,如endcall
- android TextView里的图片和文字一起通过手势放大和缩小,不要将文字和图片转为一张图操作,图片和文字是由html导入的,求指导,求demo,
- 内核里跟触摸屏坐标有关的参数错了,怎么改呢?
- 关于安卓
- adt安装出现问题
- Android加载3d人体模型
- 目前Kotlin 开发Android APP的比例有多少?
- 微信分享图片时title和描述出不来
- 250*250的图片在屏幕上显示的长度
其中,MainActivity.javapackage com.testapp.mytest;......import dalvik.system.DexClassLoader;
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button)findViewById(R.id.button_start);
View.OnClickListener l = new View.OnClickListener(){
@Override
public void onClick(View arg0) {
final File optimizedDexOutputPath =
new File(Environment.getExternalStorageDirectory().toString() + "/tmp/FuncEntry.jar");
DexClassLoader cl = new DexClassLoader(optimizedDexOutputPath.getAbsolutePath(),
Environment.getExternalStorageDirectory().toString(),
null,
getClassLoader());
Class libProviderClazz = null;
try {
libProviderClazz = cl.loadClass("com.testapp.mytest.FuncEntry");
IFuncEntry lib = (IFuncEntry)libProviderClazz.newInstance(); if(2 == lib.func(1)){
Toast.makeText(MainActivity.this, "Good job!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "Damn it!", Toast.LENGTH_SHORT).show();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
};
btnStart.setOnClickListener(l);
}
}
IFuncEntry.java的代码package com.testapp.mytest;public interface IFuncEntry {
public int func (int i);
}
在库的这个项目中,有两个文件IFuncEntry.java和FuncEntry.java两个文件
其中,IFuncEntry.java如下package com.testapp.mytest;public interface IFuncEntry {
public int func (int i);
}
而FuncEntry.java如下package com.testapp.mytest;public class FuncEntry implements IFuncEntry{
public int func(int i){
return i + 1;
}
}
将库编译生成FuncEntry.jar,并且放在SD卡的tmp目录下,运行应用,点击按钮以后,会在这一句崩溃 DexClassLoader cl = new DexClassLoader(optimizedDexOutputPath.getAbsolutePath(),
Environment.getExternalStorageDirectory().toString(),
null,
getClassLoader());
1> 我把两个项目,都声明成同一个package ("package com.testapp.mytest"),这样正确吗?
2> 在应用里面,我也加入了一个IFuncEntry.java文件,否则以下语句编译不过,我的处理正确吗?IFuncEntry lib = (IFuncEntry)libProviderClazz.newInstance();3> 为什么会在以上地方崩溃?应该如何修改呢?
谢谢
你把奔溃的log贴出来看看