Gradle依赖引起"Invalid Magic Number"错误

7
我在GitHub上有一个项目,既在办公室也在家里工作。大约两个月前,这个项目在两台机器上都正常运行。然后两周前,在我的家用电脑上停止了运行,但是在我的工作电脑上仍然可以正常运行。以下是我得到的错误信息:
:app:shrinkDebugMultiDexComponents FAILED FAILURE: Build failed with an exception. What went wrong: Execution failed for task ':app:shrinkDebugMultiDexComponents'. java.io.IOException: Can't read [D:\dev\gitRepo\app\android\app\build\intermediates\multi-dex\debug\allclasses.jar] (Can't process class [__MACOSX/com/stripe/android/._BuildConfig.class] (Invalid magic number [51607] in class)) Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

给我带来错误的stripe包是一个第三方库,你可以在这里找到。我按照他们的要求将其列为我的build.gradle文件中的依赖项。

compile 'com.stripe:stripe-android:+'

我已经将涉及stripe的所有代码注释掉,应用程序完全正常运行,所以我知道问题与我处理该包的方式有关。
不幸的是,我不记得我做了什么来使它停止工作。我认为在这件事发生前一周我升级了Android Studio,并花费了相当长的时间来处理ProGuard配置。 我尝试过:
  • 在没有进行ProGuard更改的master分支上工作。
  • 卸载并重新安装Android Studio。
  • 重新克隆git仓库。
  • 安装API 17(stripe for eclipse需要此项。虽然不是Studio,但我尝试了一下)。
  • 联系stripe客户支持,但他们毫无头绪。
  • 这篇stackoverflow文章。然而,没有任何Mac电脑接触过该项目,我也没有个人压缩与stripe相关的任何内容。
  • 从这里,将魔数从十六进制转换为ASCII码。结果是Q`,我不认识它。
"I think it may have something to do with something I did for ProGuard, but I don't understand how. I'm on a completely different branch than any Proguard work, with a clean AndroidStudio install, with a clean repository clone, and the project still works fine when I'm in the office."
"EDIT:
I am running this on the 'debug' BuildType. These are my 3 gradle files. The first is for the entire Project, the second is for the Application Module, and the third is for a local android library Module.
Project 'build.gradle':
buildscript { repositories { jcenter() maven { url 'http://download.crashlytics.com/maven' } } dependencies { classpath 'com.android.tools.build:gradle:1.2.3' classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+' } }"
allprojects {
    repositories {
        jcenter()
        maven{ url 'http://download.crashlytics.com/maven' }
    }
}

Android应用程序模块 build.gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}


android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.app.android"
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
        minSdkVersion 16
        targetSdkVersion 22
        multiDexEnabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            buildConfigField 'Boolean', 'enableCrashlytics', 'true'
        }
        debug {
            buildConfigField 'Boolean', 'enableCrashlytics', 'false'
        }
        adhoc {
            debuggable true
            signingConfig signingConfigs.debug
            buildConfigField 'Boolean', 'enableCrashlytics', 'true'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}
dependencies {
    compile project(':localLibrary')
    compile 'com.facebook.android:facebook-android-sdk:3.21.1'
    compile 'commons-io:commons-io:2.4'
    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile 'com.android.support:support-v4:22.0.1'
    compile 'com.google.android.gms:play-services-identity:8.1.0'
    compile 'com.google.android.gms:play-services-plus:8.1.0'
    compile 'com.google.android.gms:play-services-maps:8.1.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'io.card:android-sdk:5.0.1'
    compile 'com.stripe:stripe-android:+'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
        transitive = true;
    }
}

本地 Android 库模块 build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
        multiDexEnabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:22.1.0'
    compile 'com.google.code.gson:gson:2.2.2'
    compile 'com.android.support:multidex:1.0.0'
    compile group: 'org.apache.httpcomponents' , name: 'httpmime' , version: '4.3.5'
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'

}

2
无效的魔数表示与Java版本相关的问题。 - ligi
@ligi,你是指我正在使用的Java版本的机器吗? - JuiCe
我建议避免在依赖中使用 +,而是指定完整的版本号来引用 stripe-android。这样你就可以尝试不同的版本来查看是否有问题。对于 io.fabric.tools:gradle 也是如此。 - Sean Barbeau
2个回答

3

这个问题可能与您运行的Java版本有关。我曾经遇到过类似的问题,发现构建时使用了Java 8。当我切换到Java 7后,这个构建问题得到了解决。

在Android Studio中,请转到

File -> Project Structure -> SDK Location

JDK位置应该是Java 1.7.x(Java 7)


3
是的,要验证两台机器是否具有相同的Java版本,确实是相同版本的Android Studio。我能想到的唯一其他可能就是Stripe正在使用AS捆绑的构建工具来构建您的apk文件,而您默认安装的构建工具略有不同,并且缺少某些东西 - 请参见我在类似问题上发布的这篇文章

1
我还没有测试过这个,但赏金即将到期,所以我把它给你了。我会在周末在我的家用电脑上检查。 - JuiCe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接