jjzjj

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

长河落日圆 2023-03-28 原文

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

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

  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

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

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

错误信息是: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,安装依赖库

  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会报错:

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

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

有关Version 28 (intended for Android Pie and below) is the last version of the legacy support library的更多相关文章

  1. 使用时 Rubygems 2.0.14 不是线程安全的 bundle 程序安装消息 - RUBYGEMS VERSION : 2. 4.5.1 - 2

    运行bundle安装时,我收到以下消息:Rubygems2.0.14isnotthreadsafe,soyourgemswillbeinstalledoneatatime.UpgradetoRubygems2.1.0orhighertoenableparallelgeminstallation.这很奇怪,因为在我的RubyGems环境中它说我的RubyGems版本是:2.4.5.1(见下文)~/w/Rafftopia❯❯❯gemenvRubyGemsEnvironment:-RUBYGEMSVERSION:2.4.5.1-RUBYVERSION:2.2.5(2016-04-26patc

  2. ruby - session 未创建 : Chrome version must be between - 2

    当使用ruby​​selenium驱动程序驱动chrome时,我得到/home/travis/.rvm/gems/ruby-2.6.2/gems/selenium-webdriver-3.141.5926/lib/selenium/webdriver/remote/response.rb:72:in`assert_ok':sessionnot创建:Chrome版本必须在70和73之间(Selenium::WebDriver::Error::SessionNotCreatedError)如何解决这个问题?降级chrome不是我想做的事。 最佳答案

  3. ruby - 来自 Bundler 的 Fresh Ruby gem - 无法加载我的 version.rb 文件? - 2

    我想制作一个全新的Ruby应用程序,所以我决定使用bundlegemfoo来完成它。这给了我全部:lib/foo/version.rbfoo.rbGemfileRakefilefoo.gemspecREADME.md结构。现在,当我运行rubyfoo.rb时,我得到:`require':cannotloadsuchfile--foo/version(LoadError)foo.rb只是默认值:require'foo/version'moduleFooendgems不能自己跑吗?我希望我不必为了测试而将我的gem包含到示例应用程序中吗? 最佳答案

  4. ruby - rbenv:未安装版本 `2.2.3'(由 RBENV_VERSION 环境变量设置) - 2

    当我运行部署脚本时,出现错误:[cb123fad]rbenv:version`2.2.3'isnotinstalled(setbyRBENV_VERSIONenvironmentvariable)DEBUG[cb123fad](Backtracerestrictedtoimportedtasks)capaborted!SSHKit::Runner::ExecuteError:Exceptionwhileexecutingasdeploy@xx.xx.xx.xx:Exceptionwhileexecutingasdeploy@xx.xx.xx.xx:bundleexitstatus:1b

  5. ruby-on-rails - 警告 : Nokogiri was built against LibXML version 2. 8.0,但已动态加载 2.7.8 - 2

    我在OSXML上的Ruby2.0.0和Rails4配置中遇到了这个问题。我的ArchLinux工作站上没有这个问题。这个警告之所以如此烦人,是因为它破坏了我的Vim插件(SweetVimRspec),因此终止了我的Vim开发工作流程,因为我无法测试。我在这里尝试了所有赞成的解决方案:MacuserandgettingWARNING:NokogiriwasbuiltagainstLibXMLversion2.7.8,buthasdynamicallyloaded2.7.3而且我无法摆脱它(实际上我在nokogiri本身中摆脱了它,但在rspec和railsconsole中根本没有)。

  6. ruby - 在 gem --version 之后运行时获取 "isn' t a Gem::Specification(改为 NilClass) - 2

    $gem--version[/home/rohit/.rvm/gems/ruby-1.9.3-p125@qnrDashboard/specifications/net-ssh-2.5.2.gemspec]isn'taGem::Specification(NilClassinstead).[/home/rohit/.rvm/gems/ruby-1.9.3-p125@qnrDashboard/specifications/net-sftp-2.0.5.gemspec]isn'taGem::Specification(NilClassinstead).[/home/rohit/.rvm/ge

  7. ruby - 如何停止使 tmux 自动设置 RBENV_VERSION - 2

    当我启动tmux时,tmux会自动设置RBENV_VERSION...有人知道怎么阻止吗?因为它会自动设置,所以我需要做$导出RBENV_VERSION取消设置并使.ruby-version工作。谢谢。 最佳答案 tmux本身永远不会自行设置(或取消设置)RBENV_VERSION。您有一些配置导致了这种情况。我的猜测是RBENV_VERSION是在您启动tmux服务器时设置的,它现在是tmux“全局环境”(所有继承的基础环境)的一部分tmux启动的进程)。你可以检查这个tmuxshow-environment-g|grepRBEN

  8. ruby-on-rails - rake 数据库 :migrate is being aborted due to rake version difference - 2

    这个问题在这里已经有了答案:YouhavealreadyactivatedX,butyourGemfilerequiresY(11个答案)关闭8年前。我遇到了错误rakedb:migrate--tracerakeaborted!Youhavealreadyactivatedrake10.1.1,butyourGemfilerequiresrake10.1.0.Usingbundleexecmaysolvethis./Users/iang/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:

  9. ruby-on-rails - rails - postgres 错误 : Reason: Incompatible library version: libpq. 5.dylib 需要 1.0.0 或更高版本, - 2

    我无法在我的Mac(10.6.8)上设置Ruby(1.9.3)、Rails和Postgres(9.0.8)。每次运行railsconsole时,我都会收到以下错误:/Users/dc/.rvm/gems/ruby-1.9.3-head@global/gems/pg-0.13.2/lib/pg.rb:4:in`require':dlopen(/Users/dc/.rvm/gems/ruby-1.9.3-head@global/gems/pg-0.13.2/lib/pg_ext.bundle,9):Librarynotloaded:@loader_path/../lib/libssl.dy

  10. ruby-on-rails - rails : Why do I get "warning: already initialized constant JSON::VERSION" when running rake cucumber? - 2

    我刚刚设置了一个LinuxMintbox,用于使用rvm进行Rails开发。我继续生成了一个Rails5应用程序,设置了mysql连接,添加了cucumber-railsgem然后尝试运行:rakecucumber出于某种原因,我遇到了:/usr/bin/ruby2.3-Sbundleexeccucumber--profiledefault/usr/lib/ruby/vendor_ruby/json/version.rb:3:warning:alreadyinitializedconstantJSON::VERSION/var/lib/gems/2.3.0/gems/json-1.8.

随机推荐