微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,它表示文件系统中的文件.

原文地址:https://www.jb51.cc/android/315306.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐