如何在 android API 19 中使用 appcompat-v7

How to use appcompat-v7 with android API 19

问题全在

我有一个带有这个 gradle 的模块:

apply plugin: 'com.android.library'
android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    compileOptions.encoding "ISO-8859-1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 8
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
}

当我将 "compileSdkVersion" 更改为 19 时 => 我收到 100 多个有关未找到资源的错误:

示例:

    Error:(9, 21) No resource found that matches the given name: attr 'android:actionModeShareDrawable'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Body1'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Body2'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Button'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Caption'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Display1'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Display2'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Display3'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Display4'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Headline'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Inverse'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Large'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Large.Inverse'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.PopupMenu.Large'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.PopupMenu.Small'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Medium'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Medium.Inverse'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Menu'.
Error:Error retrieving parent for item: No resource found that matches the given name '@android:TextAppearance.Material.SearchResult.Subtitle'.
Error:Error retrieving parent for item: No resource found that matches the given name '@android:TextAppearance.Material.SearchResult.Title'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Small'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Small.Inverse'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Subhead'.
Error:Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Title'.

当然你会在 API<21 中遇到这些错误,因为 android:TextAppearance.Material 和其他 (Material) 是在 API 21 中引入的。AppCompat 正在移植资源,但它们在使用时没有 android: 前缀(目前我无法检查您是否也在使用这些,但可能是)。如果您使用 API 21 和 运行 在较低的 API 上编译您的应用程序,也会发生设备崩溃

android:前缀指的是内置参数,它应该在所有支持的APIs(minSDK)版本中。 Material 不存在于 API21 之前的系统中

查看build.gradle

中的compileSdkVersion

例如。

    android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    dexOptions {
        javaMaxHeapSize "2g"
        //jumboMode true
    }
}

如果你保持最新,应该不会让你头疼。发生这种情况的原因是如果为同一个库生成了多个 R 文件。