Android Studio和Gradle 2.0版中的DexFile [英] DexFile in 2.0 versions of Android Studio and Gradle

查看:107
本文介绍了Android Studio和Gradle 2.0版中的DexFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码实例化特定程序包中包含的所有类.

I am using the following code to instantiate all the classes included in a certain package.

DexFile df = new DexFile(getPackageCodePath());
for (Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
    String className = iter.nextElement();
    if (className.contains(packageName) && !className.contains("$")) {
        myClasses.add(Class.forName(className).newInstance());
    }
}

很遗憾,它无法正常工作.从Android Studio 2和Gradle 2.0.0开始,DexFile条目不再包含应用程序中的所有类,而仅包含属于com.android.tools包的类.

Unfortunately it is not working properly anymore. Since Android Studio 2 and Gradle 2.0.0, the DexFile entries no longer include all the classes within the app but only the classes belonging to the com.android.tools package.

这是一个已知问题吗?

推荐答案

此问题似乎与新的 InstantRun 功能.

Looks like this issue is related to the new InstantRun feature in the Android Plugin for Gradle 2.0.0.

getPackageCodePath()获取一个指向Android文件系统中base.apk文件的字符串.如果解压缩该apk,则可以在其根文件夹中找到一个或几个.dex文件.从方法df.entries()获得的条目将遍历在该根文件夹中找到的.dex文件,以获取其所有已编译类.

getPackageCodePath() gets a String pointing towards the base.apk file in the Android file system. If we unzip that apk we can find one or several .dex files inside its root folder. The entries obtained from the method df.entries() iterates over the .dex files found in that root folder in order to obtain all of its compiled classes.

但是,如果我们使用新的Android Gradle插件,则只会找到与android运行时和即时运行相关的.dex(包com.tools.android.fd.runtimecom.tools.android.fd.commoncom.tools.android.tools.ir.api).每个其他类都将编译成几个.dex文件,压缩成一个名为Instant-run.zip的文件,并放入apk的根文件夹中.

However, if we are using the new Android Plugin for Gradle, we will only find the .dex related to the android runtime and instant run (packages com.tools.android.fd.runtime, com.tools.android.fd.common and com.tools.android.tools.ir.api). Every other class will be compiled in several .dex files, zipped into a file called instant-run.zip and placed into the root folder of the apk.

这就是为什么问题中发布的代码无法列出应用程序内的所有类的原因.不过,这仅会影响Debug版本,因为Release版本不具有InstantRun功能.

That's why the code posted in the question is not able to list all the classes within the app. Still, this will only affect Debug builds since the Release ones don't feature InstantRun.

这篇关于Android Studio和Gradle 2.0版中的DexFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆