资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android中怎么动态加载jar文件

Android 中怎么动态加载 jar 文件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

为定陶等地区用户提供了全套网页设计制作服务,及定陶网站建设行业解决方案。主营业务为成都做网站、网站建设、外贸营销网站建设、定陶网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

1.1  首先需要了解一点:在Android中可以动态加载,但无法像Java中那样方便动态加载jar

原因:Android的虚拟机(Dalvik VM)是不认识Java打出jar的byte code,需要通过dx工具来优化转换成Dalvik byte code才行。这一点在咱们Android项目打包的apk中可以看出:引入其他Jar的内容都被打包进了classes.dex。

所以这条路不通,请大家注意。

1.2  当前哪些API可用于动态加载

1.2.1  DexClassLoader

这个可以加载jar/apk/dex,也可以从SD卡中加载,也是本文的重点。

1.2.2  PathClassLoader

只能加载已经安装到Android系统中的apk文件。

1.3  代码

     package com.example.dynamicloaddemo.jar;
    
   public class DynamicTest implements IDynamic {
    
       @Override
       public String helloWorld() {
        return "Hello World Form JAR";
       }
    
   }

   将这个类导出,注意,编译的时候必须是 java 1.6 编译(java 1.7 编译会出现错误:Zip is good, but no classes.dex inside, and no valid .odex file in the same directory)

1.4  编译文件之后将上面的类打包为 dynamic.jar 

        1.4.1  下载jar 转化 dex 工具,将打包好的jar拷贝到SDK安装目录android-sdk-windows\platform-tools下,DOS进入这个目录,执行命名: dx --dex --output=test.jar dynamic.jar

        1.4.2  将test.jar拷贝到 /data/data/packagename/app-libs/ 

放在 SDCard 上会出现错误   Optimized data directory /data/data/com.example.dynamicloaddemo/files/test.jar is not owned by the current user.  Shared storage cannot protect your application from code injection attacks.

Android 中怎么动态加载 jar 文件

程序代码:

public class MainActivity extends Activity {
Button mToastButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToastButton = (Button) findViewById(R.id.main_btn);
// Before the secondary dex file can be processed by the DexClassLoader,
// it has to be first copied from asset resource to a storage location.
// final File dexInternalStoragePath = new File(getDir("dex",
// Context.MODE_PRIVATE),SECONDARY_DEX_NAME);
// if (!dexInternalStoragePath.exists()) {
// mProgressDialog = ProgressDialog.show(this,
// getResources().getString(R.string.diag_title),
// getResources().getString(R.string.diag_message), true, false);
// // Perform the file copying in an AsyncTask.
// // 从网络下载需要的dex文件
// (new PrepareDexTask()).execute(dexInternalStoragePath);
// } else {
// mToastButton.setEnabled(true);
// }
System.out.println(getDir("libs", Context.MODE_PRIVATE));
System.out.println(getFilesDir());
System.out.println(getCacheDir());
System.out.println(getDir("libs", Context.MODE_PRIVATE));
System.out.println(getDir("libs", Context.MODE_PRIVATE));
mToastButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Internal storage where the DexClassLoader writes the
// optimized dex file to.
// final File optimizedDexOutputPath = getDir("outdex", Context.MODE_PRIVATE);
final File optimizedDexOutputPath = new File(getDir("libs", Context.MODE_PRIVATE) + File.separator + "test.jar");
// Initialize the class loader with the secondary dex file.
// DexClassLoader cl = new
// DexClassLoader(dexInternalStoragePath.getAbsolutePath(),
// optimizedDexOutputPath.getAbsolutePath(),
// null,
// getClassLoader());
/*
DexClassLoader cl = new DexClassLoader(optimizedDexOutputPath.getAbsolutePath(), Environment
.getExternalStorageDirectory().toString(), null, getClassLoader());
*/
DexClassLoader cl = new DexClassLoader(optimizedDexOutputPath.getAbsolutePath(), getDir("libs", Context.MODE_PRIVATE).getAbsolutePath(), null, getClassLoader());
Class libProviderClazz = null;
try {
// Load the library class from the class loader.
// 载入从网络上下载的类
// libProviderClazz =
// cl.loadClass("com.example.dex.lib.LibraryProvider");
libProviderClazz = cl.loadClass("com.example.dynamicloaddemo.jar.DynamicTest");
// Cast the return object to the library interface so that
// the
// caller can directly invoke methods in the interface.
// Alternatively, the caller can invoke methods through
// reflection,
// which is more verbose and slow.
// LibraryInterface lib = (LibraryInterface)
// libProviderClazz.newInstance();
IDynamic lib = (IDynamic) libProviderClazz.newInstance();
// Display the toast!
// lib.showAwesomeToast(view.getContext(), "hello 世界!");
Toast.makeText(MainActivity.this, lib.helloWorld(), Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
// Handle exception gracefully here.
exception.printStackTrace();
}
}
});
}
}

1.5  运行,出现 Hello World From JAR , 表明动态加载成功。

 

看完上述内容,你们掌握Android 中怎么动态加载 jar 文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


分享题目:Android中怎么动态加载jar文件
网页路径:http://cdkjz.cn/article/jecsch.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220