尝试使用 Ant 构建 Android 应用程序时抛出 ExceptionWithContext [英] ExceptionWithContext gets thrown when trying to build an Android app with Ant

查看:22
本文介绍了尝试使用 Ant 构建 Android 应用程序时抛出 ExceptionWithContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试在 Google 和 stackoverflow 上四处搜索以寻找答案,但我一直无法找到与我遇到的确切问题有关的任何人.我正在尝试设置一个持续集成服务器(特别是 Bamboo)以在每次有人更改源代码管理时更新、构建和导出 APK.当我手动执行每一步操作时,我在本地机器上和在使用我设置的作业时在服务器上都遇到了同样的错误.当我到达构建的 dex 步骤时发生错误.到目前为止,我使用 ant debugant releaseant clean debugant clean release.整个dex步骤的输出,完整的有错误,如下:

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 attempting to set up a continuous integration server (Bamboo, specifically) to update, build, and export an APK every time someone makes a change in source control. I'm running into the same error both on my local machine when I do every step by hand and on the server when I use the job I've set up. The error happens when I reach the dex step of the build. I've gotten the same output so far with ant debug, ant release, ant clean debug, and ant clean release. The output of the entire dex step, complete with error, is as follows:

-dex:
      [dex] input: C:\Users\...\Android\bin\classes
      [dex] input: C:\Users\...\google-play-services_lib\bin\classes.jar
      [dex] input: C:\Program Files (x86)\Android\android-sdk\tools\support\annotations.jar
      [dex] input: C:\Users\...\Android\libs\FlurryAgent.jar
      [dex] input: C:\Users\...\Android\libs\gcm.jar
      [dex] input: C:\Users\...\Android\libs\android-support-v4.jar
      [dex] input: C:\Users\...\google-play-services_lib\libs\google-play-services.jar
      [dex] Pre-Dexing C:\Users\...\google-play-services_lib\bin\classes.jar -> classes-64c0adfe92ddc950c7ab8c5002ceabf2.jar
      [dex] Pre-Dexing C:\Program Files (x86)\Android\android-sdk\tools\support\annotations.jar -> annotations-62bab95d6948a2db17bbc7976160b014.jar
      [dex] Pre-Dexing C:\Users\...\Android\libs\FlurryAgent.jar -> FlurryAgent-499d43756a3ce626a64773e6dfd5eaec.jar
      [dex] Pre-Dexing C:\Users\...\Android\libs\gcm.jar -> gcm-ae2640f44640eb4fd7b13964b65d2d70.jar
      [dex] Pre-Dexing C:\Users\...\Android\libs\android-support-v4.jar -> android-support-v4-fa30b373a3e3ba9f2cf94900a9eb42fe.jar
      [dex] Pre-Dexing C:\Users\...\google-play-services_lib\libs\google-play-services.jar -> google-play-services-9efad6e9178399c185fae6c0b6bdc4c6.jar
      [dex] Converting compiled files and external libraries into C:\Users\...\Android\bin\classes.dex...
       [dx]
       [dx] UNEXPECTED TOP-LEVEL EXCEPTION:
       [dx] com.android.dx.util.ExceptionWithContext
       [dx]     at com.android.dx.util.ExceptionWithContext.withContext(ExceptionWithContext.java:46)
       [dx]     at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:344)
       [dx]     at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:134)
       [dx]     at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:87)
       [dx]     at com.android.dx.command.dexer.Main.processClass(Main.java:487)
       [dx]     at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
       [dx]     at com.android.dx.command.dexer.Main.access$400(Main.java:67)
       [dx]     at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:135)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
       [dx]     at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
       [dx]     at com.android.dx.command.dexer.Main.processOne(Main.java:422)
       [dx]     at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
       [dx]     at com.android.dx.command.dexer.Main.run(Main.java:209)
       [dx]     at com.android.dx.command.dexer.Main.main(Main.java:174)
       [dx]     at com.android.dx.command.Main.main(Main.java:91)
       [dx] Caused by: java.lang.NullPointerException
       [dx]     at com.android.dx.cf.code.ConcreteMethod.<init>(ConcreteMethod.java:87)
       [dx]     at com.android.dx.cf.code.ConcreteMethod.<init>(ConcreteMethod.java:75)
       [dx]     at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:247)
       [dx]     ... 23 more
       [dx] ...while processing <init> (Lcom/.../android/LocationService;)V
       [dx] ...while processing com/.../android/LocationService$1.class
       [dx]
       [dx] 1 error; aborting

对于上下文,我在 Windows 机器上使用 Ant v1.9.2 和 Android build-tools v18.0.1,我没有以任何方式编辑构建脚本.我在它们的两个目录中使用 android update project --path . 为应用程序生成了一个,为库生成了一个.我还没有尝试将它设置为自动使用正确的密钥库进行签名,尽管我的(有限)理解应该没有必要,至少对于使用 Ant 的调试构建来说不是必要的.

For context, I'm using Ant v1.9.2 and Android build-tools v18.0.1 on a Windows machine and I haven't edited the build scripts in any way. I generated one for the app and one for the library using android update project --path . in their two directories. I also haven't tried setting it up to automatically use the proper keystore for signing yet, although to my (limited) understanding that shouldn't be necessary, at least not for a debug build with Ant.

有没有人见过这个特定的问题?生成的.class 文件有问题吗?构建文件?这是我第一次真正尝试使用 Ant 进行构建(我通常只是让 Eclipse 为我完成所有艰苦的工作),所以我几乎没有什么可继续的.任何帮助将不胜感激.

Has anyone seen this particular issue before? Is it a problem with the generated .class file? The build files? This is my first real foray into building with Ant (I generally just let Eclipse do all the hard work for me), so I have very little to go on. Any help would be much appreciated.

更新:如果有人关注这个问题,我的问题似乎已经解决了.如何以及为什么,我不知道.我今天早上尝试更新源代码(我们有一些更改),重新运行 android update project -p .,尝试了 ant clean debug,你瞧,它工作.ant release 也是如此,它甚至用我给它的密钥对它进行了正确的签名.我最好的猜测是 LocationService 类文件中有一些奇怪的东西,尽管我无法理解.

Update: In case anyone was paying attention to this question, my issue seems to have resolved itself. How and why, I don't know. I tried updating the source this morning (we had a few changes in), reran android update project -p ., tried an ant clean debug, and lo and behold, it worked. As did ant release, which even signed it properly with the key I gave it. My best guess is that there was something weird in that LocationService class file, although what it was is beyond me.

更新 2:我在第一次更新中所说的一切现在都无效了.我已经隔离了这个问题,但并没有更接近于理解它.这块代码是罪魁祸首:

Update 2: Anything I said in my first update is now invalid. I've isolated the issue, but am no closer to understanding it. This block of code is the culprit:

if (Settings.DEBUG) {
    Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            storeDebugNotification(AndroidUncaughtExceptionHandler.getStackTraceString(ex));
        }
    });
}

这就是事情变得奇怪的地方.当 Settings.DEBUG 标志为 true 时,这对 ant 构建良好.当它是 false 时,它会失败,给我上面显示的错误.当我将整个事情注释掉时,它可以在 DEBUG 设置中正常工作.对 if (Settings.DEBUG) 行及其花括号进行注释但主体保持不变,以及对主体进行注释并单独留下 if 部分也是如此.所以......我在这里不知所措.在这个特定文件中,当 DEBUG 为 false 时,if 语句和正文之间的交互会导致问题.另一个奇怪的部分是我们在应用程序的另一个文件(一个活动,而这个是一个服务)中有 完全相同的 if 块.

Here's where things get weird. When the Settings.DEBUG flag is true, this builds fine with ant. When it's false, it fails, giving me the error shown above. When I comment the whole thing out, it works fine with either DEBUG setting. The same goes for having the if (Settings.DEBUG) line and its curly braces commented but the body left intact, as well as commenting the body and leaving the if portion alone. So... I'm at a loss here. Something about the interaction between the if statement and the body, in this particular file, when DEBUG is false is causing problems. And the other weird part is that we have that exact same if block in another file in the app (an activity, whereas this one is a service).

推荐答案

我在编译要发布的项目时遇到了同样的异常.我的代码是:

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

因为BuildConfig.DEBUG 是一个值为false 的常量,所以块中的代码被识别为死代码,并在优化时删除.

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

CfTranslator(类文件翻译器)想为块内的匿名类创建一个单独的文件(SomeClass$1.class),但由于它被优化掉了会发生错误.我把花括号外面的匿名类问题解决了:

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

更新:解决这个问题的另一种方法(@Ewoks 在下面的回答中描述)是:

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

这篇关于尝试使用 Ant 构建 Android 应用程序时抛出 ExceptionWithContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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