Android Studio Gradle 构建问题(无法运行项目)

标签 android android-studio android-gradle-plugin

我刚刚将我的 Android Studio 更新到最新版本 2.2.2。之后需要更新 gradle。但是在更新两者之后我无法运行我之前运行良好的项目。

下面是 gradle 日志错误:-

Warning:WARNING: Dependency xpp3:xpp3:1.1.4c is ignored for productionDebug as it may be conflicting with the internal version provided by Android.

Warning:WARNING: Dependency xpp3:xpp3:1.1.4c is ignored for productionRelease as it may be conflicting with the internal version provided by Android.

Warning:WARNING: Dependency xpp3:xpp3:1.1.4c is ignored for stagingDebug as it may be conflicting with the internal version provided by Android.

Warning:WARNING: Dependency xpp3:xpp3:1.1.4c is ignored for stagingRelease as it may be conflicting with the internal version provided by Android.

Warning:Conflict with dependency 'com.android.support:recyclerview-v7'. Resolved versions for app (23.0.0) and test app (21.0.3) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

Warning:Conflict with dependency 'com.android.support:support-v4'. Resolved versions for app (25.0.0) and test app (21.0.3) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

Warning:Conflict with dependency 'com.android.support:recyclerview-v7'. Resolved versions for app (23.0.0) and test app (21.0.3) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

Warning:Conflict with dependency 'com.android.support:support-v4'. Resolved versions for app (25.0.0) and test app (21.0.3) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

Error:Execution failed for task ':app:prepareStagingDebugAndroidTestDependencies'.Dependency Error. See console for details.

这是我的 Build.gradle 文件供引用:-

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

apply plugin: 'com.google.gms.google-services'

android {
    useLibrary 'org.apache.http.legacy'
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'LICENSE.txt'
    }
    compileSdkVersion 23
    buildToolsVersion '22.0.1'
    defaultConfig {
        multiDexEnabled true
        applicationId 'com.qa.app'
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 28
        versionName '3.5'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    dexOptions {
        incremental true
        javaMaxHeapSize "2048M"
        jumboMode = true
    }

    productFlavors {
        production {
            minSdkVersion 15
            targetSdkVersion 21
            versionCode 28
            versionName '3.5'
            signingConfig signingConfigs.config
        }
        staging {
            minSdkVersion 15
            targetSdkVersion 21
            versionCode 28
            versionName '3.5'
            signingConfig signingConfigs.config
        }
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:cardview-v7:22.1.1'
    compile 'com.android.support:recyclerview-v7:22.1.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.facebook.rebound:rebound:0.3.7'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.github.castorflex.smoothprogressbar:library-circular:1.0.2'
    compile 'com.facebook.android:facebook-android-sdk:4.+'
    compile 'com.facebook.android:notifications:1.0.2'
//    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.google.android.gms:play-services:9.6.1'
    compile 'de.keyboardsurfer.android.widget:crouton:1.8.5@aar'
    compile 'com.loopj.android:android-async-http:1.4.6'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.android.support:support-annotations:25.0.0'
    //    compile 'com.google.android.gms:play-services:7.5.0'
    compile project(':library')
    androidTestCompile 'com.android.support:support-annotations:25.0.0'
    //    androidTestCompile 'com.android.support:support-annotations:22.2.1'
    androidTestCompile 'com.android.support.test:runner:0.2'
    androidTestCompile 'com.android.support.test:rules:0.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
    compile project(':rate-me')
    compile project(':volleyNew')
    compile files('libs/YouTubeAndroidPlayerApi.jar')
    compile('com.paypal.sdk:paypal-android-sdk:2.15.0') {
        exclude group: 'io.card'
    }
    compile 'org.igniterealtime.smack:smack-android-extensions:4.1.0'
    compile 'org.igniterealtime.smack:smack-experimental:4.1.0'
    compile 'org.igniterealtime.smack:smack-tcp:4.1.0'
    compile 'org.igniterealtime.smack:smack-im:4.1.0'
    compile 'com.loopj.android:android-async-http:1.4.6'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.2@aar') {
        transitive = true;
    }
}

最佳答案

在您的情况下,解决方案应该是将以下行添加到依赖项:

androidTestCompile 'com.android.support:recyclerview-v7:25.0.0'
androidTestCompile 'com.android.support:support-v4:25.0.0'

您还应该调整支持库版本。 在您的情况下,所有这些都应该是 25.0.0

这是类似的问题:Resolved versions for app (22.0.0) and test app (21.0.3) differ

更新: 为了使依赖控制更容易,您可以定义变量,您可以在其中存储当前支持库版本。

因此,您的依赖 block 应该如下所示:

def support_version = '25.0.0'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/YouTubeAndroidPlayerApi.jar')

    // support libraries that should have the same version
    compile "com.android.support:appcompat-v7:$support_version"
    compile "com.android.support:cardview-v7:$support_version"
    compile "com.android.support:support-v4:$support_version"
    compile "com.android.support:recyclerview-v7:$support_version"
    compile "com.android.support:support-annotations:$support_version"
    compile "com.android.support:design:$support_version"

    compile project(':library')
    compile project(':rate-me')
    compile project(':volleyNew')

    compile 'com.facebook.rebound:rebound:0.3.7'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.github.castorflex.smoothprogressbar:library-circular:1.0.2'
    compile 'com.facebook.android:facebook-android-sdk:4.+'
    compile 'com.facebook.android:notifications:1.0.2'
    compile 'com.google.android.gms:play-services:9.6.1'
    compile 'de.keyboardsurfer.android.widget:crouton:1.8.5@aar'
    compile 'com.loopj.android:android-async-http:1.4.6'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'org.igniterealtime.smack:smack-android-extensions:4.1.0'
    compile 'org.igniterealtime.smack:smack-experimental:4.1.0'
    compile 'org.igniterealtime.smack:smack-tcp:4.1.0'
    compile 'org.igniterealtime.smack:smack-im:4.1.0'
    compile 'com.loopj.android:android-async-http:1.4.6'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.squareup.picasso:picasso:2.4.0'

    compile('com.crashlytics.sdk.android:crashlytics:2.6.2@aar') {
        transitive = true;
    }
    compile('com.paypal.sdk:paypal-android-sdk:2.15.0') {
        exclude group: 'io.card'
    }

    androidTestCompile "com.android.support:recyclerview-v7:$support_version"
    androidTestCompile "com.android.support:support-v4:$support_version"
    androidTestCompile "com.android.support:support-annotations:$support_version"

    androidTestCompile 'com.android.support.test:runner:0.2'
    androidTestCompile 'com.android.support.test:rules:0.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}

阅读更多:GString , Resolving conflicts between main and test APK

关于Android Studio Gradle 构建问题(无法运行项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40587403/

相关文章:

android - 如何解决flutter_local_notifications PlatformException错误?

java - 回收者查看项目不改变其颜色

keystore 的 Android Studio 备份

android - 更新 gradle 结果在 'Unable to find method ' com.android.build.gradle.tasks.GenerateBuildConfig.getBuildConfigPackageName()Ljava/lang/String'

Android Build from source find - 没有这样的文件目录错误

android - 单元测试应该涵盖常量变化吗?

java - Android - getResources() 和 getPackageName() 问题

java - 无法在 Android Studio 中添加操作栏项目

android - 无法找到可选库 : android. test.runner

android - 如何根据发布/调试为不同的产品风格和不同的图标设置 gradle?