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

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

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

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

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

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

我的主要 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 是保存应用程序业务逻辑的项目。它的结构是这样的:

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

基本上是说我的所有导入不存在,而它们确实存在,并且我的代码文件和类识别的所有导入都没有错误。

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

最佳答案

好吧,虽然 Geadle 是一个强大的工具,但他妈的太可怕了。

如果您遇到同样的麻烦,这是我的经验,供您引用。由于我不确定我的发现是否绝对正确,如果我错了请纠正我。

  • 我发现无法将您的测试文件夹放在库项目下。我希望每个项目库都有 src/test/java 并运行我的测试用例,例如 ./gradlew :booking-sdk:test。但是,通过将此包移动到主应用程序(在我的例子中是 booking-app)并将相关文件从 booking-sdk 的 build.gradle 移动到 booking-sdk 的 build.gradle booking-app 我可以看到测试用例结果。
  • 我发现 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'
}

希望对您有所帮助。

关于Android Gradle Dependency 与 Android 提供的内部版本冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28935846/

相关文章:

javascript - 代码拆分 `import` 破坏了 Jest 测试

java - Android Gradle 插件需要 Java 11 才能运行。您当前使用的是 Java 1.8。错误

android - 使用不同的来源测试和发布 Android Studio

java - 将实时视频从 Raspberry Pi 流式传输到我的 Android 应用程序但出现安全异常

android 小键盘点击按钮

android - 如何从android中的画廊获取地址

Android调用WCF服务

java - 无法执行任务 : the task has already been executed error in downloading items

unit-testing - Redux 如何在单元测试中更新存储?

unit-testing - 带有 Vuetify 的 Vue Composition Api 未能通过 jest 进行单元测试