Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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文件夹中的文件,这样我就不必使用外部存储来获取已存在的文件的额外副本?

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

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

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

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

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

大佬总结

以上是大佬教程为你收集整理的android – 如何从我的.apk文件中打包的jar文件中加载一个类?全部内容,希望文章能够帮你解决android – 如何从我的.apk文件中打包的jar文件中加载一个类?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。