Search code examples
androidunit-testingandroid-studioandroid-gradle-plugingradlew

Android Gradle Dependency conflict with internal version provided by Android


Possible duplicate of this question, although the solution did not help me.

I hate copy/paste all source code but seems in gradle there is no way :( since this is the 3rd day that gradle staffing me around I put my codes here and my apology for putting so much code...

I have three projects in my main project. Therefore my settings.gradle looks like this:

include ':booking-sdk'
include ':booking-app-lib'
include ':booking-app'

and my main build.gradle (in root of project) looks like this:

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
        classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.1.1'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
        maven { url 'https://zendesk.artifactoryonline.com/zendesk/repo' }
    }
}

ext {
    ANDROID_SUPPORT = 'com.android.support:support-v4:20.0.0'
    CRASHLYTICS = 'com.crashlytics.android:crashlytics:1.+'

    androidConfiguration = {
        compileSdkVersion 21
        buildToolsVersion '21.1.2'

        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 21
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }

        packagingOptions {
            exclude 'values/com_crashlytics_export_strings.xml'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'LICENSE.txt'
        }

        lintOptions {
            abortOnError false
            absolutePaths false
            lintConfig file("lint.xml")
        }

        sourceSets {
            main {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java.srcDirs = ['src/main/java']
                resources.srcDirs = ['src/main/resources']
                aidl.srcDirs = ['src/main/java']
                renderscript.srcDirs = ['src/main/java']
                res.srcDirs = ['src/main/res']
                assets.srcDirs = ['src/main/assets']

                java {
                    exclude 'com/booking_1/passenger/LibraryConfigurationConstants.java'
                    exclude 'com/booking_2/passenger/ProductFlavorConstants.java'
                }
            }
        }

        signingConfigs {
            debug {
                storeFile file("$rootProject.projectDir/debug.keystore")
                storePassword "android"
                keyAlias "androiddebugkey"
                keyPassword "android"
            }

            release {
                storeFile file("$rootProject.projectDir/booking-androd-prod.keystore")
                storePassword System.getenv("PASSWORD")
                keyAlias System.getenv("ALIAS")
                keyPassword System.getenv("PASSWORD")
            }
        }
    }
}

For doing Unit test by Robolectric and Instrument test I am following Decard-Gradle project that I tested and works successfully.

booking-sdk is the project that holds business logic of app. It's structure is like this:

-booking-sdk
-/build
-/src
  - booking_1 (flavour 1)
  - booking_2 (flavour 2)
  - main
  - test
- build.gradle

I have same package name under /src/main/java and /src/test/java. Finally, build.gradle of booking-sdk looks like this:

apply plugin: 'com.android.library'

android androidConfiguration

android {
    publishNonDefault true

    productFlavors {
        booking_1 { }

        booking_2 { }
    }
}

dependencies {
    repositories {
        mavenCentral()
    }

    compile ANDROID_SUPPORT
    compile CRASHLYTICS
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.squareup.retrofit:retrofit:1.7.0'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.squareup.okhttp:okhttp:2.1.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile 'com.squareup:otto:1.3.5'
    compile 'org.apache.commons:commons-lang3:3.3.2'

    compile files('src/main/libs/GeoPIP4J.jar')

    // Robolectric
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile 'org.hamcrest:hamcrest-core:1.1'
    testCompile 'org.hamcrest:hamcrest-library:1.1'
    testCompile 'org.hamcrest:hamcrest-integration:1.1'

    // TODO: requires special build of robolectric right now. working on this...
    androidTestCompile('org.robolectric:robolectric:2.4') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }
}

When I run clean command the result is:

 ./gradlew :booking-sdk:clean
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
:booking-sdk:clean                                                                                       

BUILD SUCCESSFUL

Total time: 4.83 secs

Error happens when I run ./gradlew :passenger-sdk:check code to see unit test result. I'm getting above outputs first then a lot of errors like this:

...
:booking-sdk:compileBookingDebugUnitTestJava                 
/Users/admin/Desktop/android/booking/booking-sdk/src/test/java/com/booking/passenger/db/dao/BookingDAOTest.java:3: error: package com.booking.passenger.db.providers does not exist
import com.booking.passenger.db.providers.BookingContentProvider;

It is basically saying all my imports do not exist while they do exists and I have no error in my code file and all imports recognised by class.

Any Idea would be appreciated. Thanks.


Solution

  • OK, Gradle is horrible although it is a powerful tool.

    For your reference if you fall in same trouble this is my experience. Since I'm not sure my findings are absolutely right please correct me if I'm wrong.

    • I found that it is not possible to put your test folder under library projects. I wanted to have src/test/java for every project libraries and run my test cases like ./gradlew :booking-sdk:test. However, by moving this package to main application (in my case booking-app) and also moving related files from build.gradle of booking-sdk to build.gradle of booking-app I could see test case results.
    • I found exclude modules from dependencies don't work. The solution that I found is using configuration.

    Like this:

    configurations {
        all*.exclude module: 'classworlds'
        all*.exclude module: 'commons-logging'
        all*.exclude module: 'httpclient'
        all*.exclude module: 'maven-artifact'
        all*.exclude module: 'maven-artifact-manager'
        all*.exclude module: 'maven-error-diagnostics'
        all*.exclude module: 'maven-model'
        all*.exclude module: 'maven-project'
        all*.exclude module: 'maven-settings'
        all*.exclude module: 'plexus-container-default'
        all*.exclude module: 'plexus-interpolation'
        all*.exclude module: 'plexus-utils'
        all*.exclude module: 'wagon-file'
        all*.exclude module: 'wagon-http-lightweight'
        all*.exclude module: 'wagon-provider-api'
    }