在學習《第一行代碼:Android篇》時,做書中的Demo,案例是:
打開app/build.gradle文件,在dependencies閉包中添加如下內容:
dependencies{
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:percent:24.2.1'
testCompile 'junit:junit:4.12'
}
此時,Android Studio已經幫助檢查出是過時了:
經過上網查閱,找到報錯原因:
- 由於Android Studio 版本較高,添加庫依賴已經不支持compile語句,應使用implementation或者api
- 若使用api或implementation語句仍然報錯,可能是庫的版本較低,出現了不兼容的現象
- 由於原來的support 庫太亂了,谷歌在新版本中取消了support庫,使用了新的andriodX庫
解決方法:
需要將原理的support庫遷移到AndroidX並使用implementation添加依賴。
步驟一:將dependenci閉包修改如下
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.percentlayout:percentlayout:1.0.0'
testImplementation 'junit:junit:4.13.2'
}
步驟二:遷移到 AndroidX
測試,Launch succeeded ,當下問題解決了。
但是,在做下一個Demo的時候,又出現了問題:原文在修改activity_main.xml中的代碼,使用android.support.percent.PercentFrameLayout
的時候,我這里爆紅!
錯誤信息是:Cannot resolve class android.support.percent.PercentFrameLayout
網上查閱資料是,原因有以下幾點:
- AS已經不推薦使用compile導入依賴庫
- 由於引入的第三方依賴庫依賴了androidx ,而自己本身的庫又是依賴的support ,導致兩者不能共存
- 書中介紹的百分比布局PercentFrameLayout和PercentRelativeLayout已經廢棄,被androidx.percentlayout:percentlayout:1.0.0替代
解決方法:
- 將dependenci閉包修改如下:(上面的dependencies徹底更換)
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
testImplementation 'junit:junit:4.13.2'
implementation 'androidx.percentlayout:percentlayout:1.0.0'
}
- 點擊sync now,安裝依賴庫
- 將布局文件(activity_main.xml)中的標簽改為
androidx.percentlayout.widget.PercentFrameLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="Button1"
android:layout_gravity="right|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button2"
android:text="Button2"
android:layout_gravity="left|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button3"
android:text="Button3"
android:layout_gravity="right|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button4"
android:text="Button4"
android:layout_gravity="left|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
</androidx.percentlayout.widget.PercentFrameLayout>
- 此時,上面代碼,Android Stdio會報錯:
這是因為老版本的Android Studio中內置了布局的檢查機制,認為每一個控件都應該通過android:layout_width
和android:layout_height
屬性指定寬高才是合法的。而其實我們是通過app:layout_widthPercent和app:layout_heightPercent屬性來指定寬高的,所以Android Studio沒檢測到。這個錯誤提示並不影響程序運行,忽視,直接運行即可。
如果實在有強迫症,不想讓activity_main.xml中的Button爆紅,可設置一下android:layout_height
和android:layout_width
這兩個屬性就好了。