compileSdkVersion和appcompat-v7关系研究

经常在开发中遇到由于版本不对导致出现如下的错误

Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Large.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Medium.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Small.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ProgressBar'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ProgressBar.Horizontal'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Spinner'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Material'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Material.Dialog'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Material.Light'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Material.Light.Dialog'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Button'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material'.
该错误是来自于com.android.support/appcompat-v7,com.android.support是google做的一个版本兼容包。可以简单理解为,如果你在开发中使用了在高版本中才有的API特性,通过com.android.support库也可实现在低版本上使用。appcompat-v7为com.android.support中的其中一个库。


问题分析:
以第一行报错为例

Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Inverse'.
在工程中指向

<style name="Base.TextAppearance.AppCompat.Inverse" parent="android:TextAppearance.Material.Inverse"/>

找不到父类android:TextAppearance.Material.Inverse,那这个父类是在哪里呢?这个是android的系统资源类,通过查找android平台源码https://android.googlesource.com/platform/frameworks/base/+/android-5.0.0_r2/core/res/res/values/styles_material.xml

最终在styles_material.xml中找到了它的定义

<style name="TextAppearance.Material.Inverse">
        <item name="textColor">?attr/textColorPrimaryInverse</item>
        <item name="textColorHint">?attr/textColorHintInverse</item>
        <item name="textColorHighlight">?attr/textColorHighlightInverse</item>
        <item name="textColorLink">?attr/textColorLinkInverse</item>
</style>

这个是在Android 5.0下找到的。为了对比,我也找了一下Android 4.4下面


发现根本不存在styles_material.xml文件,说明是从Android 5.0才开始增加此特性的。


结论:
我们使用appcompat-v7是为了保证我们编译的apk能够保持向下兼容,所以appcompat-v7的版本必须和compileSdkVersion保持一致。如果appcompat-v7版本小于compileSdkVersion,则不能保证到所有版本的兼容性。如果appcompat-v7版本大于compileSdkVersion,则会出现像上面那样的错误。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章