Version 28 (intended for Android Pie and below) is the last version of the legacy support library

在学习《第一行代码: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已经帮助检查出是过时了:

image

经过上网查阅,找到报错原因:

  1. 由于Android Studio 版本较高,添加库依赖已经不支持compile语句,应使用implementation或者api
  2. 若使用api或implementation语句仍然报错,可能是库的版本较低,出现了不兼容的现象
  3. 由于原来的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

image

image

image

测试,Launch succeeded ,当下问题解决了。

image

但是,在做下一个Demo的时候,又出现了问题:原文在修改activity_main.xml中的代码,使用android.support.percent.PercentFrameLayout的时候,我这里爆红!

image

错误信息是:Cannot resolve class android.support.percent.PercentFrameLayout

网上查阅资料是,原因有以下几点:

  1. AS已经不推荐使用compile导入依赖库
  2. 由于引入的第三方依赖库依赖了androidx ,而自己本身的库又是依赖的support ,导致两者不能共存
  3. 书中介绍的百分比布局PercentFrameLayout和PercentRelativeLayout已经废弃,被androidx.percentlayout:percentlayout:1.0.0替代

解决方法:

  1. 将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'
}
  1. 点击sync now,安装依赖库

image

  1. 将布局文件(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>
  1. 此时,上面代码,Android Stdio会报错:

image

这是因为老版本的Android Studio中内置了布局的检查机制,认为每一个控件都应该通过android:layout_widthandroid:layout_height属性指定宽高才是合法的。而其实我们是通过app:layout_widthPercent和app:layout_heightPercent属性来指定宽高的,所以Android Studio没检测到。这个错误提示并不影响程序运行,忽视,直接运行即可。

image

如果实在有强迫症,不想让activity_main.xml中的Button爆红,可设置一下android:layout_heightandroid:layout_width这两个属性就好了。

posted @ 2022-02-28 15:10  江河湖泊  阅读(1543)  评论(0编辑  收藏  举报