问题描述
我正在尝试构建我的 Android 项目,但在构建步骤中遇到了这个问题:
I'm trying to build my Android project but I have this issue during the build step :
[INFO] UNEXPECTED TOP-LEVEL EXCEPTION:
[INFO] com.android.dex.util.ExceptionWithContext
[INFO] at com.android.dex.util.ExceptionWithContext.withContext(ExceptionWithContext.java:45)
[INFO] at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:369)
[INFO] at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:137)
[INFO] at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:93)
[INFO] at com.android.dx.command.dexer.Main.processClass(Main.java:729)
[INFO] at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
[INFO] at com.android.dx.command.dexer.Main.access$300(Main.java:82)
[INFO] at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:170)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
[INFO] at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
[INFO] at com.android.dx.command.dexer.Main.processOne(Main.java:632)
[INFO] at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
[INFO] at com.android.dx.command.dexer.Main.runMonoDex(Main.java:279)
[INFO] at com.android.dx.command.dexer.Main.run(Main.java:245)
[INFO] at com.android.dx.command.dexer.Main.main(Main.java:214)
[INFO] at com.android.dx.command.Main.main(Main.java:106)
[INFO] Caused by: java.lang.NullPointerException
[INFO] at com.android.dx.cf.code.ConcreteMethod.<init>(ConcreteMethod.java:87)
[INFO] at com.android.dx.cf.code.ConcreteMethod.<init>(ConcreteMethod.java:75)
[INFO] at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:271)
[INFO] ... 22 more
[INFO] ...while processing <init> (Lcom/glureau/dex_issue/MyClass;)V
[INFO] ...while processing com/glureau/dex_issue/MyClass$1.class
看起来它与嵌套类 + 一个 if 条件有关,其中值始终为 false :
It looks like it's related to a nested class + a if condition where the value is always false :
public class MyClass {
public static final boolean DEBUG = false;
private class NestedClass {
}
public void updateButton(Button button) {
new NestedClass();
if (DEBUG) {
if (button != null) {
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Test", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
我怀疑这是一个 dex 问题,因为新的 OnClickListener 创建了一个新的 NestedClass 来引用 MyClass 的实例,但与此同时,这部分代码在构建阶段被删除了(因为 DEBUG=false).
I suspect it's a dex issue because the new OnClickListener create a new NestedClass that refer the instance of MyClass, but in the same time, this part of code is removed during the build phase (because DEBUG=false).
如果有人可以确认问题或为此添加一些解释,谢谢.
If someone can confirm the issue or add some explanations for this, thanks.
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00)Maven 主目录:C:\apache-maven-3.2.1\bin..Java 版本:1.7.0_71,供应商:Oracle CorporationJava 主目录:C:\Program Files\Java\jdk1.7.0_71\jre默认语言环境:fr_FR,平台编码:Cp1252操作系统名称:windows 7",版本:6.1",arch:amd64",系列:windows"
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00)Maven home: C:\apache-maven-3.2.1\bin..Java version: 1.7.0_71, vendor: Oracle CorporationJava home: C:\Program Files\Java\jdk1.7.0_71\jreDefault locale: fr_FR, platform encoding: Cp1252OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
完整的 Maven 项目(2 个类):http://www.filedropper.com/dexissue_1
Full maven project (2 classes):http://www.filedropper.com/dexissue_1
PS:是的,我可以轻松更改代码以使其再次编译.例如,只需将新的 OnClickListener 移到 if 之外,但我很想了解为什么我的代码在 dex 过程中崩溃.
PS: Yes I can easily change the code to make it compile again. For example just move the new OnClickListener outside of the if, but I would love to understand why my code crashes during the dex process.
推荐答案
我可以确认这是 Android 构建工具链中某处的错误.在标准的 Android Studio 安装中,创建一个新项目并添加像您这样的类会导致崩溃.
I can confirm this is a bug somewhere in the Android build tool chain. In a standard Android Studio installation, creating a new project and adding a class such as yours, will cause the crash.
我创建了一个问题 在 Android 开源项目中.
I have created an issue in the Android Open Source Project.
这篇关于Android dex 问题:嵌套类 + 最终布尔值:com.android.dex.util.ExceptionWithContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!