android – 如何从我的.apk文件中打包的jar文件中加载一个类?

我试图从我的.apk文件的/ assets目录中的jar文件加载接口的插件实现.我能够让它工作的唯一方法是将jar文件解压缩到私有外部存储器,然后将该文件传递给DexClassLoader.

这有效,但为什么jar必须存在于两个地方(.apk和私人外部存储)? DexClassLoader必须以文件路径作为参数.

有没有办法给它一个直接路径到/ assets文件夹中的文件,这样我就不必使用外部存储来获取已存在的文件的额外副本?

以下是相关的代码段:

// somewhere in my main Activity ...
  final File aExtractedDexFile = new File(getDir("dex",Context.MODE_PRIVATE),LIBRARY_DEX_JAR);
  extractDexTo(aExtractedDexFile);
  loadLibraryProvider(aExtractedDexFile);

/** Extract the jar file that contains the implementation class.dex and place in private storage */
private void extractDexTo(File tJarInternalStoragePath) {
  BufferedInputStream aJarInputStream = null;
  OutputStream aDexOutputStream = null;

  try {
    aJarInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR));
    aJarOutputStream = new bufferedoutputstream(new FileOutputStream(tJarInternalStoragePath));
    byte[] buf = new byte[BUF_SIZE];
    int len;
    while ((len = aJarInputStream.read(buf,BUF_SIZE)) > 0)
    {
      aJarOutputStream.write(buf,len);
    }
    aJarOutputStream.close();
    aJarInputStream.close();
  } catch (IOException e) {
    if (aDexOutputStream != null) {
      try {
        aJarOutputStream.close();
      } catch (IOException ioe) {
        ioe.printstacktrace();
      }
    }

    if (aJarInputStream != null) {
      try {
        aJarInputStream.close();
      } catch (IOException ioe) {
        ioe.printstacktrace();
      }
    }
  }
}

/** Use DexClassLoader to load the classes from LibraryProvider */
private void loadLibraryProvider(File tFile) {
  // Internal storage where the DexClassLoader writes the optimized dex file to.
  final File aOptimizedDexOutputPath = getDir("outdex",Context.MODE_PRIVATE);

  // Initialize the class loader with the secondary dex file.
  DexClassLoader cl = new DexClassLoader(tFile.getAbsolutePath(),aOptimizedDexOutputPath.getAbsolutePath(),null,getClassLoader());
  Class<?> aLibProviderClazz = null;

  try {
    // Load the library class from the class loader.
    aLibProviderClazz = cl.loadClass(LIBRARY_PROVIDER_CLASS);      
    sLibraryProvider = (LibraryInterface) aLibProviderClazz.newInstance();
  } catch (Exception exception) {
    // Handle exception gracefully here.
    exception.printstacktrace();
  }
}

解决方法

有没有办法给它一个直接路径到/ assets文件夹中的文件,这样我就不必使用外部存储来获取已存在的文件的额外副本?

答案是否定的.我想您遵循this blog posted by official source实施您的代码.如果有更好的做事方式,那么bloger应该在他的博客中推荐它.

您需要optimizeDirectory的原因在in the API中解释:

This class loader requires an application-private,writable directory to cache optimized classes.

另请注意,资产目录在apk中不可写,因此无法使用纯资产目录.

你需要复制jar文件的原因有点微妙,在博客中提到:

First,it has to be copied to a storage location whose path can be supplied to the class loader.

嵌入在apk归档中的所有内容(文件夹/文件)在运行时无法公开(或可解释)到底层文件系统.换句话说,DexClassLoader和PathClassLoader构造函数中所需的dexPath需要一个实体路径字符串,如/data/data/com.example/dex/common-lib.jar,它表示文件系统中的文件.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...