Kikiwa
Kikiwa

Reputation: 1252

Android dex issue: nested class + final boolean : com.android.dex.util.ExceptionWithContext

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

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();
                    }
                });
            }
        }
    }
}

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 home: C:\apache-maven-3.2.1\bin.. Java version: 1.7.0_71, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk1.7.0_71\jre Default locale: fr_FR, platform encoding: Cp1252 OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

Full maven project (2 classes): http://www.filedropper.com/dexissue_1

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.

Upvotes: 2

Views: 795

Answers (1)

user1801374
user1801374

Reputation: 398

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.

I have created an issue in the Android Open Source Project.

Upvotes: 1

Related Questions