构建带有 gradle 错误的 apk

build apk with gradle error

要使用 gradle 构建 apk,控制台中的错误如下:

如果使 minifyEnabled = false,则错误消失。好像有重复的包。

:app:collectDebugMultiDexComponents
:app:transformClassesWithMultidexlistForDebug
ProGuard, version 5.2.1
Reading program jar [<My_Android_Project>/app/build/intermediates/transforms/proguard/debug/jars/3/1f/main.jar]
Reading library jar [<My_Android_SDK>/build-tools/23.0.2/lib/shrinkedAndroid.jar]
Preparing output jar     [<My_Android_Project>/app/build/intermediates/multi-dex/debug/componentClasses.jar]
Copying resources from program jar [<My_Android_Project>/app/build/intermediates/transforms/proguard/debug/jars/3/1f/main.jar]
:app:transformClassesWithDexForDebug
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Uncaught translation error: com.android.dex.util.ExceptionWithContext: name already added: string{"a"}
Error:Error converting bytecode to dex: 
Cause: java.lang.RuntimeException: Translation has been interrupted
Execution failed for task ':app:transformClassesWithDexForDebug'.

> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.
ExecException: Process 'command <My_JDK_PATH>/bin/java'' finished with non-zero exit value 2

build.gradle如下:

......
buildTypes {
    debug {
        minifyEnabled true
        zipAlignEnabled false
        shrinkResources false
        proguardFiles 'proguard_legacy.cfg'
        signingConfig signingConfigs.debug
    }
    release {
        minifyEnabled true
        zipAlignEnabled true
        shrinkResources true
        proguardFiles 'proguard_legacy.cfg'
        signingConfig signingConfigs.release
    }
}
.....

......    
def dagger_version = '2.0.2'
def okhttp_version = '3.2.0'
def butterknife_version = '7.0.0'
def retrofit_version = '2.0.1'
def rxandroid_version = '1.1.0'
def rxjava_version = '1.1.0'

dependencies {

compile project(':explorer_sdk')

//multidex
compile 'com.android.support:multidex:1.0.0'

//facebook
compile 'com.facebook.android:facebook-android-sdk:4.+'

//dependency independent:  dagger2/butterknife
compile "com.google.dagger:dagger:$dagger_version"
apt "com.google.dagger:dagger-compiler:$dagger_version"
compile "com.jakewharton:butterknife:$butterknife_version"

//rxjava/rxandroid
compile "io.reactivex:rxandroid:$rxandroid_version"
compile "io.reactivex:rxjava:$rxjava_version"

//retrofit2
compile "com.squareup.retrofit2:retrofit:$retrofit_version"
compile "com.squareup.retrofit2:adapter-rxjava:$retrofit_version"
compile "com.squareup.retrofit2:converter-gson:$retrofit_version"
compile "com.squareup.retrofit2:converter-jackson:$retrofit_version"
compile ("com.squareup.retrofit2:converter-simplexml:$retrofit_version") {
    exclude module: 'stax-api'
    exclude module: 'stax:stax'
    exclude module: 'xpp3:xpp3'
}

//picasso
compile 'com.squareup.picasso:picasso:2.5.2'

//support
compile 'com.android.support:cardview-v7:23.+'
compile 'com.android.support:recyclerview-v7:23.+'

//annotation
provided 'javax.annotation:jsr250-api:1.0'

compile files('libs/ant.jar')
compile files('libs/defake.jar')
compile files('libs/HwID_OpenSDK_V3.0.01.07-R9156.jar')
compile files('libs/libammsdk_2015.2.5.jar')
compile files('libs/mta-sdk-1.6.2.jar')
compile files('libs/mtll_sdk_v115_20160226133400.jar')
compile files('libs/open_sdk_v2.9.1.jar')
compile files('libs/passportSDK_V1.4.jar')
compile files('libs/weibosdk_V3.1.jar')
compile files('libs/weibosdkcore_V3.1.jar')

//line-sdk
compile files('libs/line-android-sdk-3.1.19.jar')
}
.....

顺便问一下,如何查看重复的包?

尝试在 build.gradle 中添加以下 gradle 选项。通过添加以下选项,您可以启用 MultiDex 支持和 增量 dex 支持。

 ...
   defaultConfig {
      ...
      multiDexEnabled true // Enabling multidex support.
   }
    ...
   dexOptions {
     //you can speed up your builds by turning on incremental dexing
           incremental = true;
     //you can specify the heap size for the dex process
           javaMaxHeapSize "4g" 
   }

在您的应用中扩展 Application class,您可以覆盖 attachBaseContext() 方法并调用 MultiDex.install(this) 以启用 multidex。

 @Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

您还可以在 Whosebug 上查看以下相关问题链接:

Building Apps with Over 65K Methods

在您的 build.gradle 文件中制作 minifyEnabled false 用于调试。

我花了好几个小时检查依赖关系树,删除重复项。例如,我使用 FastScrollRecylerView 库,因此我不需要编译 com.android.support 版本的 reyclerview。

gradle app:dependencies

我还在每个库的基础上仔细构建了我的 proguard 规则,搜索通用配置等。不过我让它工作了!

您正在为 obfuscation/minification 使用自定义 ProGuard 设置文件,名为:proguard_legacy.cfg

很可能此文件包含一些不再与 DEX 处理兼容的规则。推荐使用Google提供的SDK默认的ProGuard配置文件,保证能配合DEX处理。

您可以完全关闭缩小,因为您已经发现 (minifyEnabled false),或者您可以尝试使用提供的默认 ProGuard 配置。对于后者,请更改 gradle 文件中的这一行:

proguardFiles 'proguard_legacy.cfg'

proguardFiles getDefaultProguardFile('proguard-android.txt')

SDK 中还有另一个配置文件,名为“proguard-android-optimize.txt”,但它的配置方式有点激进您的二进制文件已更改,因此它可能根本无法工作。

除了标准规则之外,您还可以添加更宽松或更严格的规则,方法是将这些规则添加到新的 ProGuard 配置中,并将其列在 gradle 文件中的默认规则旁边,例如:

proguardFiles getDefaultProguardFile('proguard-android.txt'),'my_proguard_rules.pro'

第二个文件中的任何规则都将添加到第一个文件中的规则之上。 (通常通过覆盖某些规则的行为。)

请注意:使用默认的 ProGuard 规则可能会以可能引入问题的方式更改您的代码(例如方法或 类 被重命名或完全删除)。关于必须保持不变的内容,您的自定义配置文件中可能有特定规则。不看文件就很难说出如何调整 ProGuard 配置。

您的应用程序的某些安全相关属性也可能会随着 ProGuard 规则的更改而发生变化,因此如果您的项目有安全相关的考虑,请在更改后对应用程序进行 penetration/security 审核。

How to check which package is duplicated?

将以下内容添加到您的 proguard_legacy.cfg 文件中。

 -dontobfuscate

这不会混淆您的代码,您会在日志中看到 类 或带有错误的对象的真实名称。

最后发现 "compile "com.squareup.retrofit2:converter-jackson:$retrofit_version"" 导致构建错误。

通过"gradle app:dependencies",我们可以看到converter-jackson依赖于fasterxml。所以将以下内容添加到 proguard 文件中,它起作用了!!!

-keep class com.fasterxml.** { *; }
-dontwarn com.fasterxml.**

如何找到确切的依赖包(我的意思是 com.squareup.retrofit2:converter-jackson)?我只是一一排除依赖。我花了几个小时:-(

如果谁有更好的定位依赖包的方法,请告诉我。