【问题标题】:Add google play services to a project with compile SDK version 21?将 google play 服务添加到编译 SDK 版本 21 的项目中?
【发布时间】:2018-01-06 07:34:47
【问题描述】:

所以我想将compile 'com.google.android.gms:play-services:10.0.1' 添加到我的gradle。应用级别gradle如下:

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    maven { url 'https://maven.google.com' }
}

android {
compileSdkVersion 21
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.example.myapp"
    minSdkVersion 15
    targetSdkVersion 21
    versionCode 11
    versionName "1.1"

dependencies {
    //    compile 'com.onesignal:OneSignal:[3.6.5, 3.99.99]'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.google.android.gms:play-services:10.0.1'
}

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

这也是我的项目级 gradle 文件:

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
    classpath 'com.google.gms:google-services:3.0.0'
}
}

allprojects {
    repositories {
        jcenter()
    }
}

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

当我添加播放服务依赖时,它会导致这个错误:

Error:(4) Error retrieving parent for item: No resource found that matches the given name ' android:TextAppearance.Material.Widget.Button.Inverse'.
Error:(34) Error retrieving parent for item: No resource found that matches 
the given name 'android:Widget.Material.Button.Colored'.
Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt

我尝试了 10.0.1 之前的版本,但得到了相同的结果。我也不能使用低于 9.0.0 的版本,因为我收到了这个错误:

Error:(50, 0) Version: 8.4.0 is lower than the minimum version (9.0.0) required for google-services plugin.

我无法更改编译 SDK 版本。它必须是 21。这个问题也是一个更大问题的一部分:将 OneSignal 添加到我的项目中,以便我可以发送推送通知。我已经使用过 Firebase,但我必须改用 OneSignal,因为它具有特殊功能。 请注意,所有问题都是由于我的编译 SDK 版本降至 21。如果我将其更改为 23 或更高版本,我可以轻松地将 OneSignal 添加到我的项目中。

【问题讨论】:

  • 您应该将 buildToolsVersion 更改为 buildToolsVersion "21.0.3"
  • buildToolsVersion "21.0.3" 未在我的 SDK 管理器中列出。另外,当我在 gradle 中更改它时,它会显示 not all packages are available for download @Yuliwee
  • 确保 buildTooldVersion 和 compileSdkVersion 匹配。无论如何,我很困惑您必须使用 SDK 版本 21。OneSignal 也适用于较新的版本。而且您不需要添加 com.google.gms.... 依赖项。 OneSignal 文档告诉您需要做的一切:documentation.onesignal.com/docs/android-sdk-setup
  • 听起来我必须打开另一个线程并发布我的主要问题,即使用编译 SDK 21 将 OneSignal 添加到我的项目中。这样就不会那么混乱了。@Yuliwee
  • 为什么需要 compileSdkVersion 21?如果它更高,如果您不使用更新的功能,它仍然可以在旧系统上运行。

标签: android google-play-services onesignal


【解决方案1】:

我建议您为已在您的 Choose_Library_Dependency 对话框中指定的 Google Play 服务添加依赖项。 这件事一直对我有用。 只需浏览选项,您就会找到 Google Play 服务的匹配项。

截图:https://www.android-examples.com/wp-content/uploads/2016/06/choose-library-dependency.png

【讨论】:

    【解决方案2】:

    我无法更改编译 SDK 版本。 必须是 21 岁。

    不要将minSdkVersioncompileSdkVersion 混淆。
    今天我们有了 27 版本,如果你想使用最新的 google play services、firebase、OneSignal 库,你必须改变这个级别,避免使用 compileSdk = 21。

    Check this post:

    compileSdkVersion 是您告诉 Gradle 使用哪个版本的 Android SDK 编译您的应用程序的方式。使用新的 Android SDK 是使用该级别中添加的任何新 API 的要求。

    需要强调的是,更改您的 compileSdkVersion 不会改变运行时行为
    因此强烈建议您始终使用最新的 SDK 进行编译

    如果 compileSdkVersion 为您设置了最新的 API,minSdkVersion 是您应用的下限。 minSdkVersion 是 Google Play 商店用来确定哪个 API 的信号之一可以安装应用程序的用户设备。

    然后使用最新的 OneSignal 库、最新的 GooglePlayServices 库并将你的 compileSdk 更改为 27。

    【讨论】:

    • 感谢您的回复。正如我在问题中提到的,出于某种原因,我不应该更改我的编译 SDK 版本。它应该保持在 21。
    • @saraX 在我看来,更改 compilesdk 应该没有任何问题,因为它不会改变运行时行为。但是如果你今天必须使用 compileSdk=21(现在我们有 27 版),那么使用新库(google play services、firebase、OneSignal....)会遇到很多问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多