无法使用 `debugImplementation 'androidx.fragment:fragment-testing:'+fragment_version` 作为依赖项运行应用程序
Posted
技术标签:
【中文标题】无法使用 `debugImplementation \'androidx.fragment:fragment-testing:\'+fragment_version` 作为依赖项运行应用程序【英文标题】:Failing to run the app with `debugImplementation 'androidx.fragment:fragment-testing:'+fragment_version` as a dependency无法使用 `debugImplementation 'androidx.fragment:fragment-testing:'+fragment_version` 作为依赖项运行应用程序 【发布时间】:2020-06-25 17:59:08 【问题描述】:我是安卓新手。
我正在使用 Java 进行开发。
我的目标是在我的测试框架中使用 FragmentScenraio。
在我找到的指南中,我看到我需要添加:
dependencies
def fragment_version = "1.2.2"
// ...
debugImplementation 'androidx.fragment:fragment-testing:$fragment_version'
致我的眷属。
我正在添加它并且项目成功构建。
问题是当我在 Android Studio 中运行应用程序时,我得到了
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:preDebugAndroidTestBuild'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
> Could not resolve androidx.test:core:1.3.0-alpha04.
Required by:
project :app
project :app > androidx.test:core-ktx:1.3.0-alpha04
project :app > androidx.test.ext:junit:1.1.2-alpha04
project :app > androidx.test.espresso:espresso-intents:3.3.0-alpha04
> Cannot find a version of 'androidx.test:core' that satisfies the version constraints:
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:strictly 1.2.0' because of the following reason: debugRuntimeClasspath uses version 1.2.0
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'
> Could not resolve androidx.test:core:strictly 1.2.0.
Required by:
project :app
> Cannot find a version of 'androidx.test:core' that satisfies the version constraints:
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:strictly 1.2.0' because of the following reason: debugRuntimeClasspath uses version 1.2.0
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'
> Could not resolve androidx.test:monitor:strictly 1.2.0.
Required by:
project :app
> Cannot find a version of 'androidx.test:monitor' that satisfies the version constraints:
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:monitor:strictly 1.2.0' because of the following reason: debugRuntimeClasspath uses version 1.2.0
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:runner:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
> Could not resolve androidx.test:monitor:1.3.0-alpha04.
Required by:
project :app > androidx.test:core:1.3.0-alpha04
project :app > androidx.test.ext:junit:1.1.2-alpha04
project :app > androidx.test:runner:1.3.0-alpha04
> Cannot find a version of 'androidx.test:monitor' that satisfies the version constraints:
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:monitor:strictly 1.2.0' because of the following reason: debugRuntimeClasspath uses version 1.2.0
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:runner:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
> Could not resolve androidx.test:core:1.2.0.
Required by:
project :app > androidx.fragment:fragment-testing:1.2.2
> Cannot find a version of 'androidx.test:core' that satisfies the version constraints:
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:strictly 1.2.0' because of the following reason: debugRuntimeClasspath uses version 1.2.0
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
谁能告诉我这里发生了什么?
我尝试关注this,但也没有用。
谢谢。
我正在添加我的 app/gradle.build 文件:
apply plugin: 'com.android.application'
android
compileSdkVersion 28
buildToolsVersion rootProject.buildToolsVersion
defaultConfig
applicationId "com.example.examscanner"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
testOptions
unitTests
includeAndroidResources = true
compileOptions
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies
def camerax_version = "1.0.0-beta01"
def fragment_version = "1.2.2"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
//noinspection GradleCompatible
implementation 'com.android.support:recyclerview-v7:28.0.0'
// CameraX core library using the camera2 implementation
implementation "androidx.camera:camera-camera2:$camerax_version"
// If you want to use the CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha08"
// If you want to use the CameraX Extensions library
implementation "androidx.camera:camera-extensions:1.0.0-alpha08"
// If you want to use the CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// RxJava
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation "io.reactivex.rxjava2:rxjava:2.1.3"
implementation ("androidx.fragment:fragment-testing:"+fragment_version,
exclude group: 'androidx.test', module: 'core'
)
// androidTest
androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
androidTestImplementation 'androidx.test:core-ktx:' + rootProject.coreVersion
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test.ext:junit-ktx:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
androidTestImplementation 'androidx.test.espresso:espresso-intents:'+rootProject.espressoVersion
// test
testImplementation 'androidx.test:core:' + rootProject.coreVersion;
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
testImplementation 'androidx.test.espresso:espresso-intents:' + rootProject.espressoVersion
testImplementation 'androidx.test.ext:truth:' + rootProject.extTruthVersion
testImplementation 'org.json:json:20140107'
debugImplementation 'androidx.fragment:fragment-testing:'+fragment_version
apply plugin: "androidx.navigation.safeargs"
【问题讨论】:
【参考方案1】:这些是重要的行:
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:strictly 1.2.0' because of the following reason: debugRuntimeClasspath uses version 1.2.0
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'
让我们解开每个含义。最后一行
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'
说fragment-testing:1.2.2
依赖于androidx.test:core:1.2.0
。这是有道理的,它是一个测试库。
第二行是
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:strictly 1.2.0' because of the following reason: debugRuntimeClasspath uses version 1.2.0
这表示debugRuntimeClasspath
使用androidx.test:core:1.2.0
(这是fragment-testing:1.2.2
引入的,所以这是意料之中的)。
重要的是这是一个strictly 1.2.0
- 您不能更改androidTest
或test
中使用的版本,因为这可能会使debug
的预期无效。
其余行表明您的rootProject.coreVersion
版本是1.3.0-alpha04
。但是没有strictly 1.2.0
(你的Constraint
)和1.3.0-alpha04
的版本,因此出现错误。
您也可以通过将这些依赖项添加到 debugImplementation
来升级您的 debugImplementation
:
debugImplementation 'androidx.test:core:' + rootProject.coreVersion
debugImplementation 'androidx.test:monitor:' + rootProject.coreVersion
这意味着您的debug
和androidTest
(和test
)版本都将匹配,避免了约束问题。
【讨论】:
太令人沮丧了,所以官方指南是错误的,一个人不得不去额外的箍来设置基本的东西。这使得鼓励开发人员加入 UI 测试变得非常困难,这种努力是不值得的 不,您需要使用debugImplementation
才能让fragment-testing
工作。文档是正确的。
debugImplementation
+ 一个黑客。而在高级测试的codelab中,使用implementation
:developer.android.com/codelabs/…
implementation
是debugImplementation
和releaseImplementation
(以及您拥有的任何其他配置)的超集。重要的是你不能使用androidTestImplementation
。这总是正确的。
当前版本 1.4.0 和 Hilt(将我带到这里)也是如此。以上是关于无法使用 `debugImplementation 'androidx.fragment:fragment-testing:'+fragment_version` 作为依赖项运行应用程序的主要内容,如果未能解决你的问题,请参考以下文章
如何使用模块化代码片段中的LeakCanary检测内存泄漏?