ExceptionWithContext gets thrown when trying to build an Android app with Ant

前端 未结 3 576
我在风中等你
我在风中等你 2020-12-15 04:39

I\'ve tried searching around both on Google and on stackoverflow for an answer to this, but I\'ve been unable to find anyone with the exact issue I\'m having. I\'m attemptin

相关标签:
3条回答
  • 2020-12-15 05:11

    Other solution than proposed by @Albert-Jan would be to "wrap" that constant in a method. Something like

    public boolean isInDeveloperMode(){ return BuildConfig.DEBUG; }

    in which case it will not be "resolved" as constant by gradle and there will be no problem during build time.

    Probably the most famous "exploit" of this approach would be isUserAGoat() method from UserManager class in Android SDK. There is very popular discussion here about possible uses of this method.. Enjoy ;)

    Hopefully this will be useful for somebody with similar issues

    0 讨论(0)
  • 2020-12-15 05:13

    I had the same exception while compiling a project for release. My code was:

    if (BuildConfig.DEBUG) {
        myView.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // Do something
            }
        });
    }
    

    Because BuildConfig.DEBUG is a constant with value false, the code in the block is recognized as dead code and removed when optimized.

    The CfTranslator (Classfile Translator) wants to create a separate file for the anonymous class inside the block (SomeClass$1.class), but since it is optimized away an error will occur. I took the anonymous class outside the curly braces the problem was solved:

    View.OnClickListener lClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do something
        }
    };
    
    if (BuildConfig.DEBUG) {
        myView.setOnClickListener(lClickListener);
    }
    

    Update: Another way to solve this (described by @Ewoks in his answer below) is:

    public boolean isInDeveloperMode() {
        return BuildConfig.DEBUG;
    }
    
    ...
    
    if (isInDeveloperMode()) {
        myView.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // Do something
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-15 05:13

    After months of fighting this exact problem, I have finally found a solution that works for me. It might not be your case. Make sure, that none of the classes you are referring to (Maybe Settings? Maybe AndroidUncaughtExceptionHandler?) is private. The Gradle is not able to handle it and cannot find the method within the class. Just change it to public (or just delete the flag to keep it default, if the class is nested), and you should be good to go.

    0 讨论(0)
提交回复
热议问题