android-studiogradleandroid-studio-3.5

Lint found fatal errors while assembling a release target - minSdkVersion 26 to 21


I am attempting to build a signed release APK of my app. The app is currently associated to minSdkVersion 26 on the Play Store and I am trying to make it retrocompatible with API 21.

When I try to build a signed release APK in minSdkVersion 21 I get a fatal lint error.

I have read quite a few of the existing threads about this but nothing worked, I also analyzed my entire code and everything is ok (a very few orange warnings but no more major red errors). Please note that I do not want to use:

android {
  lintOptions {
    checkReleaseBuilds false
    abortOnError false
  }
}

This would only bypass the error, not solve it...

Here is my 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
}

Here is the error (copy-paste from 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.

Please help. Also, be very very simple and descriptive in your answer, I am not a computer scientist but a geologist and have learnt by myself over the past year only how to put together some code in Java to create my app, so my knowledge of the coding theory is extremely limited.

--EDIT--

I added this to my build.gradle which allows me to build my signed release APK:

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

Can someone explain me what this does? Is it another way of bypassing the error or does it actually fixes the error (as I intend)? I also found that the folder java and res are duplicated/(generated), how can I stop this from happening since this is likely the cause of the original error?


Solution

  • 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'
            }
    }