Android build.gradle 使用支持库构建失败

Android build.gradle using Support Library Failing Build

我在开发过程中成功构建了我的应用程序的调试版本,尽管伴随有以下警告:

"W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable.

当我进行完整发布构建时遇到以下错误:

Error:Execution failed for task   ':app:transformClassesAndResourcesWithProguardForFullRelease'.
> java.io.IOException: Can't write  [/PATH_TO_APP/app/build/intermediates/transforms/proguard/full/release/jar s/3/1f/main.jar] (Can't read  [/PATH_TO_APP/app/build/intermediates/exploded- aar/com.android.support/support- fragment/24.2.1/jars/classes.jar(;;;;;;**.class)] (Duplicate zip entry  [android/support/v4/app/r.class ==  classes.jar:android/support/v4/app/Fragment.class]))

我合并支持库是为了在 Android 6.0+ 中使用 AppCompat 权限。以下是我的build.gradle文件。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion '24.0.3'
    useLibrary 'org.apache.http.legacy'
ext {
    supportLibVersion = '24.2.1'
}

lintOptions {
    abortOnError false
}

defaultConfig {
    applicationId "com.whatever.appname"
    testApplicationId 'com.whatever.appname'
    versionCode 8
    minSdkVersion 9
    targetSdkVersion 24
}

buildTypes {
    debug {
        debuggable true
        minifyEnabled false
        signingConfig signingConfigs.debug
    }
    release {
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFile 'PATH_TO_PROGUARD/proguard-project.txt'
    }
}

signingConfigs {
    release {
        keyPassword 'PASSWORD'
        storePassword 'PASSWORD'
        keyAlias 'ALIAS'
        storeFile file('PATH_TO_KEYSTORE/keystore_file')
    }
    debug {
        keyAlias 'KEY_ALIAS'
        keyPassword 'PASSWORD'
        storePassword 'PASSWORD'
        storeFile file('PATH_TO_DEBUG_KEYSTORE/debug.keystore')
    }
}
productFlavors {
    full {
        versionName '10.6.0'
        applicationId 'com.whatever.appname'
        testApplicationId 'APP_TEST_ID'
        versionCode 8
        proguardFile '/PATH_TO_PROGUARD/proguard-project.txt'
        signingConfig signingConfigs.release
        targetSdkVersion 24
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

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 'ch.acra:acra:4.7.0'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-compat:24.2.1'
    compile 'com.android.support:support-core-utils:24.2.1'
    compile 'com.android.support:support-core-ui:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-annotations:24.2.1'
    compile fileTree(include: ['*.jar'], dir: 'libs')
}

在我的 proguard-project.txt 文件中,由于使用了一些已弃用的 类,我的应用程序的一些相关和特定条目是:

-keep class org.apache.http.** { *; }
-dontwarn org.apache.commons.**
-dontwarn org.apache.http.**
-dontwarn android.net.http.AndroidHttpClient
-dontwarn com.google.android.gms.**
-dontwarn com.android.volley.toolbox.**
-ignorewarnings

对于那些好奇的人,当我从构建中完全删除 proguard 进程并清理应用程序,然后进行 fullRelease 构建时,我遇到的错误是不同的:

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define     Landroid/support/graphics/drawable/AnimatedVectorDrawableCompat;

和:

Error:Execution failed for task  ':app:transformClassesWithDexForFullRelease'.
> com.android.build.api.transform.TransformException:   com.android.ide.common.process.ProcessException:    java.util.concurrent.ExecutionException:    java.lang.UnsupportedOperationException

任何人都可以阐明我遇到的错误,并可能提出解决方法吗?当然,我更愿意将混淆过程合并到构建过程中。

更新: 此外,当从命令行执行 运行 任何 gradle 任务时,我遇到错误:

problem occurred evaluating project ':app'.
> java.lang.UnsupportedClassVersionError:   com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0

这让我相信 AppCompat 或支持库中的某处使用了 Java 8 种语言特性,而我正在针对 Java 7.

进行编译

不确定伙计,但如果我是你,我会尝试看这个广告

compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:support-compat:24.2.1'

也许 appcompact 和 support compact 并不太顺利。 尝试删除 appcompact 并使用支持(反之亦然)

希望对您有所帮助

我的问题的解决方案是启用 Java 8 种语言功能的配置解决方案,如文档 Use Java 8 Language Features.

中所述

我为解决该问题采取的具体步骤是: 将以下内容添加到 build.gradle defaultConfig 部分。

jackOptions {
    enabled true
}

将以下内容分别添加到 build.gradle 构建类型、调试和发布:

debug{
    shrinkResources false
}
release {
    shrinkResources false
}

将 build.gradle sourceCompatability 和 targetCompatability Java 版本从 VERSION_1_7 更改为以下

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8

进行这些更改后,我进行了清理和构建,并成功构建了应用程序。