Android Gradle Dependency与Android提供的内部版本冲突

时间:2015-03-09 05:42:06

标签: android unit-testing android-studio android-gradle gradlew

this question可能重复,但解决方案对我没有帮助。

我讨厌复制/粘贴所有源代码,但似乎在gradle中没有办法:(因为这是第三天,我在我的工作人员附近,我把我的代码放在这里,并且我为这么多代码道歉......

我的主项目中有三个项目。因此我的settings.gradle看起来像这样:

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

我的main build.gradle(在项目的根目录中)如下所示:

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")
            }
        }
    }
}

通过Robolectric和仪器测试进行单元测试我正在关注我测试并成功运行的Decard-Gradle项目。

booking-sdk是包含app业务逻辑的项目。它的结构是这样的:

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

我在/src/main/java/src/test/java下有相同的包名称。最后,booking-sdk的build.gradle看起来像这样:

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

当我运行clean命令时,结果为:

 ./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

当我运行./gradlew :passenger-sdk:check代码以查看单元测试结果时发生错误。我首先得到的是输出,然后是很多错误:

...
: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;

基本上说我的所有导入在它们存在时都不存在,我的代码文件中没有错误,所有导入都被类识别。

任何想法都将不胜感激。感谢。

1 个答案:

答案 0 :(得分:12)

好吧,Geadle虽然是强大的工具,但却很可怕。

如果您遇到同样的麻烦,供您参考,这是我的经验。由于我不确定我的发现是否完全正确,如果我错了,请纠正我。

  • 我发现无法将测试文件夹放在库项目下。我希望每个项目库都有src/test/java并运行我的测试用例./gradlew :booking-sdk:test。但是,通过将此包移动到主应用程序(在我的情况下为booking-app)并将相关文件从booking-sdk的build.gradle移动到booking-app的build.gradle,我可以看到测试用例结果
  • 我发现来自依赖项的exclude个模块不起作用。我找到的解决方案是使用配置。

像这样:

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

希望它有所帮助。