首页 > 解决方案 > Lint 在组装发布目标时发现致命错误 - minSdkVersion 26 到 21

问题描述

我正在尝试构建我的应用程序的签名发布 APK。该应用程序当前与 Play Store 上的 minSdkVersion 26 相关联,我正在尝试使其与 API 21 兼容。

当我尝试在 minSdkVersion 21 中构建签名发布 APK 时,我收到了一个致命的 lint 错误。

我已经阅读了很多关于此的现有线程,但没有任何效果,我还分析了我的整个代码,一切正常(很少有橙色警告,但没有更多的重大红色错误)。请注意,我不想使用:

android {
  lintOptions {
    checkReleaseBuilds false
    abortOnError false
  }
}

这只会绕过错误,而不是解决它......

这是我的 build.gradle:

apply plugin: 'com.android.application'
android {
    signingConfigs {
        my_signing_key {
            keyAlias ''
            keyPassword ''
            storeFile file('')
            storePassword ''
        }
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId ""
        minSdkVersion 21 // used to be 26
        targetSdkVersion 28
        versionCode 9
        versionName "2.2"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            signingConfig signingConfigs.my_signing_key
        }
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'    //used to be 26.1.0
    implementation 'com.android.support:design:28.0.0'  //used to be 26.1.0
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.opencsv:opencsv:4.6'    // for OpenCV
}

这是错误(从 app/build/reports/lint-results-release-fatal.html 复制粘贴):


Duplicate Platform Classes
../../build.gradle: commons-logging defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.
There are a number of libraries that duplicate not just functionality of the Android platform but using the exact same class names as the ones provided in Android -- for example the apache http classes. This can lead to unexpected crashes.

To solve this, you need to either find a newer version of the library which no longer has this problem, or to repackage the library (and all of its dependencies) using something like the jarjar tool, or finally, rewriting the code to use different APIs (for example, for http code, consider using HttpUrlConnection or a library like okhttp).
Note: This issue has an associated quickfix operation in Android Studio and IntelliJ IDEA.
To suppress this error, use the issue id "DuplicatePlatformClasses" as explained in the Suppressing Warnings and Errors section.

请帮忙。另外,在你的回答中要非常简单和描述性,我不是计算机科学家而是地质学家,在过去的一年里我自己学会了如何用 Java 编写一些代码来创建我的应用程序,所以我对编码的了解理论非常有限。

- 编辑 -

我将此添加到我的 build.gradle 中,它允许我构建我的签名版本 APK:

configurations {
    all {
        exclude module: 'commons-logging'
    }
}

有人可以解释一下这是做什么的吗?它是绕过错误的另一种方式还是它实际上修复了错误(如我所愿)?我还发现文件夹 java 和 res 是重复的/(生成的),我怎样才能阻止这种情况发生,因为这可能是原始错误的原因?

标签: android-studiogradleandroid-studio-3.5

解决方案


From the message error and your current solution, there are duplicate defined classes (conflict) between your used library (module 'commons-logging' on com.opencsv:opencsv:4.6) and Android library. If you are sure you do not use that particular module, your current solution should be OK. Or you can just remove it from your used library (not from all library) by the followings:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'    //used to be 26.1.0
    implementation 'com.android.support:design:28.0.0'  //used to be 26.1.0
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation ('com.opencsv:opencsv:4.6')    // for OpenCV
        {
            exclude module: 'commons-logging'
        }
}

推荐阅读