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,則會出現像上面那樣的錯誤。

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