android - 将 google-play-services 添加到 android 依赖项

标签 android android-gradle-plugin dependencies google-play-services

我希望在我的应用程序中包含一个包含条形码扫描仪的 Activity ,为此我需要能够使用 BarcodeDetector。为了能够使用它,我需要实现依赖项“compile 'com.google.android.gms:play-services:11.0.2”,但是当我这样做时,我收到以下错误消息:

enter image description here

我不知道如何解决此错误,我的“build.gradle(模块:应用程序)”如下所示:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.myname.myproject"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support:cardview-v7:26.+'

    compile 'com.google.android.gms:play-services:11.0.2'

    testCompile 'junit:junit:4.12'
}

我的“build.gradle(项目:MyProject)”看起来像这样:

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

有人可以帮我解决这个问题吗?

最佳答案

您所包含的库似乎引用了其他一些库的不同版本。这就是幕后的问题。

在您的示例中,您包含了整个播放服务包。根据你所说的,看起来你可能只需要“vision”包。请尝试以下配置:

compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'

compile 'com.google.android.gms:play-services-vision:11.8.0'

这些是变化:

  • 通过提供特定版本号(今天的最后一个稳定可用版本),某些库中的“+”号已被删除
  • 我没有使用 Play Services 的整个 bundle 版本,而是仅添加了 play-services-vision,这应该足够了。如果您还需要其他软件包,您可以找到完整列表here

编辑: 您还需要在项目的 gradle 中添加 google() 才能访问最新的库,如下所示:

allprojects {
    repositories {
        google()
        jcenter()
    }
}

关于android - 将 google-play-services 添加到 android 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48325020/

相关文章:

android - 在 android studio 中使用 gradle 构建变体的最佳实践

gradle - 如何通过 gradle 设置 git hook 可执行文件

java - 如何在 Gradle 构建中排除其他子项目的依赖?

Android Gradle 组常用属性

android - 升级到 Android Gradle Plugin 4.2.0-beta03 后出现新的 Gradle 错误

android - 添加特定的存储库以构建文件

npm - npm 安装错误

android-activity - 启动 Activity 时的平板电脑 Android 方向

java - Android Studio Logcat 没有显示任何内容

android - 如何使用 TabActivity 或 FragmentActivity 在 android 中创建选项卡