如何在Android API 19中使用appcompat-v7?

3

一切在问题中

我有一个模块,其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'.

3
试试这个:compile 'com.android.support:appcompat-v7:19.+',意思是编译使用Android支持库V7版本为19或更高的版本。 - M D
targetSdkVersion 不应该是8,那真的非常糟糕。 - David Corsalini
这是一个库模块,我想可以去掉targetSdkVersion。 - An-droid
我个人长期以来一直在使用M D答案。 - maveroid
2个回答

6
当然,你在API<21中遇到这些错误,因为android:TextAppearance.Material和其他Material(材料)是在API 21中引入的。AppCompat正在移植资源,但它们没有使用android:前缀(此时我不能检查你是否也在使用,但可能是的)。如果你使用API 21编译应用程序并在较低的API设备上运行,也会出现崩溃。 android:前缀是指内置参数,应该在所有支持的API版本(minSDK)中存在。在pre-API21系统中没有Material(材料)。

你的意思是说我们的minSdk应该至少是API 21吗? - amadib
不,使用您想要支持的最低SDK版本。但是,如果您想要使用较新SDK中的任何新方法/类/样式(如上所述),请记住,在具有旧Android版本的设备上运行的应用程序(例如此最旧支持的minSdk)可能会抛出“异常”或简单地省略这些行(例如XML文件中的属性)。上述问题涉及“AppCompat”库,该库为您的应用程序带来了一些来自较新SDK的方法/类/样式,因此可以在较旧的系统(例如ICS)中也使用新样式(部分材料),但请注意它们具有不同的命名方式,不会干扰原始名称。 - snachmsm

0
检查 build.gradle 中的 compileSdkVersion。
例如:
    android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    dexOptions {
        javaMaxHeapSize "2g"
        //jumboMode true
    }
}

如果你保持最新的版本,就不应该会出现问题。出现这种情况的原因是同一库生成了多个R文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接