我可以将最新版本的 appcompat 库用于针对 sdk 21 的项目吗

Can I use the latest version of the appcompat library with a project that is targeting sdk 21

我不太了解这个 appcompat 库,如果这不是最聪明的问题,我深表歉意。

我有一个项目遇到了问题。结果我将 targetSdkVersion 设置为 23,这给我带来了问题,因为我想使用 21。所以我将其更改为 21。

在我的 build.gradle 我有编译 com.android.support:appcompat-v7:23.1.1.

这会在我尝试构建时导致以下问题:

C:\Users\Conor\Documents\Programming\AndroidStudioProjects\AndroidStudioProjects\RouteTracker\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7.1.1\res\values-v23\values-v23.xml

Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.

Error:(2) 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: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\Conor\AppData\Local\Android\sdk\build-tools.0.0\aapt.exe'' finished with non-zero exit

所以我尝试使用 com.android.support:appcompat-v7:21.1.0 的 appcompat(因为它是我唯一能明确找到的),但它也没有用。说找不到它并告诉我使用最新版本,即 23.1.1。

那么我应该使用 23.1.1 吗?如果是这样,您知道这些错误是关于什么的吗?

没有。 AppCompat 最新更新删除了一些旧的依赖项。这仅适用于 v23。因此,您必须使用 targetSdkVersion 23.(marshmallow).

更新您的代码

Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.

这是因为您使用的支持库 v23 需要编译 with API 23.

改变你的build.gradle这一行:

compileSdkVersion 23

您可以使用其他版本的支持库,但是:

  • 注意有另一个使用最新版本的依赖项
  • 21.1.0 不存在
  • 使用正确的api编译

这里是完整列表:

  //it requires compileSdkVersion 23
  compile 'com.android.support:appcompat-v7:23.1.1'
  compile 'com.android.support:appcompat-v7:23.1.0'
  compile 'com.android.support:appcompat-v7:23.0.1'
  compile 'com.android.support:appcompat-v7:23.0.0'

  //it requires compileSdkVersion 22
  compile 'com.android.support:appcompat-v7:22.2.1'
  compile 'com.android.support:appcompat-v7:22.2.0'
  compile 'com.android.support:appcompat-v7:22.1.1'
  compile 'com.android.support:appcompat-v7:22.1.0'
  compile 'com.android.support:appcompat-v7:22.0.0'

  //it requires compileSdkVersion 21
  compile 'com.android.support:appcompat-v7:21.0.3'
  compile 'com.android.support:appcompat-v7:21.0.2'
  compile 'com.android.support:appcompat-v7:21.0.0'

您已经找到了解决方案,但您的问题 'Can I use the latest version of the appcompat library with a project that is targeting sdk 21' 的答案如下。项目库应与 'compileSdkVersion' 兼容,但不一定与 targetSdkVersion 兼容。理想情况下,您的 compileSdkVersion 和 targetSdkVersion 应该相同,但如果您的要求是针对较低的 targetSdkVersion,那么您可以将其保持在低于 compileSdkVersion 的水平。