代码之家  ›  专栏  ›  技术社区  ›  SilentDev

生成第一个默认应用程序时,生成错误“任务执行失败”:应用程序:preDebugAndroidTestBuild“”

  •  1
  • SilentDev  · 技术社区  · 6 年前

    尝试按照以下教程操作: https://developer.android.com/training/basics/firstapp/creating-project

    但在我创建一个活动并单击“Finish”后,会出现一个Gradle构建错误,显示:

    Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
    

    与依赖项com冲突。安卓支持:支持注释“在项目中”:应用程序。app(26.1.0)和测试app(27.1.1)的解析版本有所不同。看见 https://d.android.com/r/tools/test-apk-dependency-conflicts.html 有关详细信息。

    查看其他SO解决方案(第二个答案: Resolved versions for app (22.0.0) and test app (21.0.3) differ )它说要将此添加到构建的依赖项中。gradle脚本:

    androidTestCompile 'com.android.support:support-annotations:xx.x.x'
    

    上面的解决方案也显示在Google示例上( https://github.com/googlesamples/android-testing/blob/ba14ef9e925fa17621bf86abe5336dcb9d53e466/runner/AndroidJunitRunnerSample/app/build.gradle#L36 )

    这就是我现在的身材。gradle(模块:应用程序):

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "com.example.ahmedayman.ember"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    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.0'
        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'
        androidTestCompile 'com.android.support:support-annotations:26.1.0'
    }
    

    但当我尝试同步项目时,仍然会遇到相同的错误。有没有办法解决这个问题?

    4 回复  |  直到 6 年前
        1
  •  3
  •   jonny    6 年前

    我也有同样的问题,我在构建中做了一些更改。gradle(模块:app),它对我很有用。我改了这个 androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    收件人: androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    这对我有用。

        2
  •  2
  •   nijfranck    6 年前

    我在内部版本中将appcompat-v7的实现版本更改为27.1.1。gradle(模块:app),它为我解决了这个问题。

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support.constraint:constraint-layout:1.1.0'
        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'
    }
    
        3
  •  0
  •   SilentDev    6 年前

    编辑

    答案如下: All com.android.support libraries must use the exact same version specification

    提到android studio 3.0取代了 compile 有了实现 implementation 'com.android.support:design:27.1.1' 而不是 编写 (如下所述)。

    原始答案

    错误消息提供了指向要查看的网站的链接。我查看了网站和链接到此URL的网站: https://developer.android.com/studio/build/gradle-tips#configure-project-wide-properties

    用于配置项目范围属性的URL。它说在我的构建中添加这个。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"
        ...
    }
    

    并将其添加到我的构建中。gradle应用程序文件:

    android {
      // Use the following syntax to access properties you define at the project level:
      // rootProject.ext.property_name
      compileSdkVersion rootProject.ext.compileSdkVersion
      ...
    }
    ...
    dependencies {
        compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
        ...
    }
    

    这就解决了问题。

        4
  •  0
  •   Waleed Abdalmajeed    6 年前

    代替 jcenter() 具有 mavenCentral() 在存储库中