失败在同步 gradle 项目 Android studio

发布时间: 2016/9/26 6:42:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我不能同步基本样本项目从 Google Play Services 。我有试着更新 minsdktargetsdkcompilesdk 版本,更新的项目结构和flavors,但该项目仍然不会 sync

这是我的错误︰

Error:Could not find method compile() for arguments [com.android.support:support-v4:24.2.0] on DefaultExternalModuleDependency{group='com.android.support', name='appcompat-v7', version='24.2.0', configuration='default'} of type org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency.

Please install the Android Support Repository from the Android SDK Manager.
<a href="openAndroidSdkManager">Open Android SDK Manager</a>

但是,安装最新 Android Support Repository (rev 36) 所以我假设问题是别的东西。

如果你想要试着克隆项目︰ https://github.com/playgameservices/android-basic-samples.git

这里是我更新 BasicSamples gradle 文件︰

BasicSamples.gradle:

// this is the parent build configuration for all the sample projects.
// for more information on gradle build files when used in a multi-project build, see
// http://gradle.org/docs/current/userguide/multi_project_builds.html

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
    }
}

// allprojects apply to every project
allprojects {
    repositories {
        jcenter()
    }

    ext {
        android_compile_version = 24
        android_version = '24.0.1'
        android_min_sdk_version = 19

        appcompat_library_version = '24.2.0'
        support_library_version = '24.2.0'
        gms_library_version = '9.4.0'
    }
}

这里是 BaseGameUtils gradle 文件︰

BaseGameUtils.gradle:

apply plugin: 'com.android.library'

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
    }
}

dependencies {
    // Set defaults so that BaseGameUtils can be used outside of BasicSamples
    if (!project.hasProperty('appcompat_library_version')) {
        ext.appcompat_library_version = '20.0.+'
    }
    if (!project.hasProperty('support_library_version')) {
        ext.support_library_version = '20.0.+'
    }
    if (!project.hasProperty('gms_library_version')) {
        ext.gms_library_version = '8.1.0'
    }

    compile "com.android.support:appcompat-v7:${appcompat_library_version}"
    compile "com.android.support:support-v4:${support_library_version}"
    compile "com.google.android.gms:play-services-games:${gms_library_version}"
    compile "com.google.android.gms:play-services-plus:${gms_library_version}"
}

android {
    // Set defaults so that BaseGameUtils can be used outside of BasicSamples
    if (!project.hasProperty('android_compile_version')) {
        ext.android_compile_version = 24
    }
    if (!project.hasProperty('android_version')) {
        ext.android_version = '24'
    }

    compileSdkVersion android_compile_version
    buildToolsVersion android_version

}

最后在这里是之一 projects gradle 文件

BeGenerous.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion '24.0.1'
    defaultConfig {

        /*
          REPLACE THIS VALUE WITH YOUR APPLICATION ID
        */
        applicationId "com.google.example.games.replace.me"

        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile "com.android.support:appcompat-v7:${appcompat_library_version}" compile "com.android.support:support-v4:${support_library_version}" compile project(':libraries:BaseGameUtils')
}

buildscript {
    repositories {
        jcenter()
    }
}

解决方法 1:

问题是实际上是一个相当轻微︰ 不是你们结束行后每个依赖项。

dependencies {
compile "com.android.support:appcompat-v7:${appcompat_library_version}" compile "com.android.support:support-v4:${support_library_version}" compile project(':libraries:BaseGameUtils')
}

应该是

dependencies {
    compile "com.android.support:appcompat-v7:${appcompat_library_version}"
    compile "com.android.support:support-v4:${support_library_version}"
    compile project(':libraries:BaseGameUtils')
}

此错误是很常见的当上插件 Android studio编辑或修改 gradle 文件以任何方式,当依赖使用变量而不是直接引用的版本编号。例如,我注意到这发生在我身上,当使用该方法计算的插件或甚至重火力点插件。

官方微信
官方QQ群
31647020