我是Android App Development的新手.当我尝试创建一个新项目时,Android Project ...弹出以下消息..
错误:任务':app:preDebugAndroidTestBuild'的执行失败.
与项目':app'中的依赖'com.android.support:support-annotations'冲突.app(26.1.0)和测试app(27.1.1)的已解决版本有所不同.有关详细信息,请参阅https://d.android.com/r/tools/test-apk-dependency-conflicts.html.信息:Gradle任务[:app:generateDebugSources,:app:generateDebugAndroidTestSources,:app:mockableAndroidJar]
这是我项目的截图 点击这里查看我得到的错误的截图
我也尝试将此代码添加到我的依赖项中.androidTestCompile'com.android.support:support-annotations:23.3.0'这没有用.我也尝试了27.1.1和26.1.0 ..但也没有成功.
小智 174
根据您的截图,我找到了两个有效的解决方案
第一个解决方案:在此行添加gradle模块的依赖项
compile 'com.android.support:support-annotations:27.1.1'
Run Code Online (Sandbox Code Playgroud)
并同步你的项目
注:如果您使用的是Android工作室3+改变compile
到implementation
第二种解决方案:配置文档中的项目范围属性https://developer.android.com/studio/build/gradle-tips.html#configure-project-wide-properties
在project gradle中添加以下行:
// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
// The following are only a few examples of the types of properties you can define.
compileSdkVersion = 26
// You can also use this to specify versions for dependencies. Having consistent
// versions between modules can avoid behavior conflicts.
supportLibVersion = "27.1.1"
}
Run Code Online (Sandbox Code Playgroud)
然后访问此部分更改compileSdkVersion
行
compileSdkVersion rootProject.ext.compileSdkVersion
并在dependencies
部分更改导入的库如下所示:
compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
Run Code Online (Sandbox Code Playgroud)
并同步你的项目
注:如果您使用的是Android工作室3+改变compile
到implementation
对于它之间的区别compile
和implementation
看看这个
在gradle中实现和编译之间的区别是什么
Har*_*ain 54
在依赖项阻止之前,在app.gradle文件中添加以下行.
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:26.1.0'
}
}
Run Code Online (Sandbox Code Playgroud)
下面还有截图,以便更好地理解.
如果您希望目标sdk为26,则configurations.all块才有用.如果您可以将其更改为27,则错误将消失,而无需在app.gradle文件中添加配置块.
还有一种方法,如果您从app.gradle文件中删除所有测试实现它将解决错误,在这也你不需要添加配置块,也不需要更改targetsdk版本.
希望有所帮助.
Har*_*ish 34
如果您使用版本26,则内部依赖项版本应为1.0.1和3.0.1,即如下所示
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
Run Code Online (Sandbox Code Playgroud)
如果使用版本27,则内部依赖项版本应为1.0.2和3.0.2,即如下所示
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Run Code Online (Sandbox Code Playgroud)
小智 26
如果您使用的是Android Studio 3.1.+或更高版本
把它放在你的gradle依赖项中:
implementation 'com.android.support:support-annotations:27.1.1'
Run Code Online (Sandbox Code Playgroud)
总体如下:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-annotations:27.1.1'
}
Run Code Online (Sandbox Code Playgroud)
des*_*aci 23
这是由于版本冲突,要解决它,只需强制更新您的支持 - 注释版本,在您的模块上添加此行:app gradle
implementation ('com.android.support:support-annotations:27.1.1')
希望这能解决你的问题;)
编辑
几乎忘了,你可以为版本声明一个额外的属性(https://docs.gradle.org/current/userguide/writing_build_scripts.html#sec:extra_properties),转到你的项目(或你的顶级)gradle文件,声明你的支持,或者只是为了这个例子,注释版本var
ext.annotation_version = "27.1.1"
然后在您的模块gradle中将其替换为:
implementation ("com.android.support:support-annotations:$annotation_version")
这与@emadabel解决方案非常相似,这是一个很好的替代方案,但没有块或rootproject
前缀.
小智 6
别担心这很简单:
转到"项目"目录结构,然后转到"Gradle Scripts",在其中转到"build.gradle(Module:app)"并双击它.
现在 - 向下滚动程序,然后转到依赖项部分:如下所示
依赖{
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Run Code Online (Sandbox Code Playgroud)
}
现在在此删除最后两行代码并重建应用程序,现在它将起作用
依赖项应该是:
依赖{
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
Run Code Online (Sandbox Code Playgroud)
}
重建应用程序和它的工作!
归档时间: |
|
查看次数: |
128286 次 |
最近记录: |