我正在阅读关于Android的一个房间库。我看到他们把android包改成了androidx。我不明白。有人能解释一下吗?

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

甚至这也是可用的android包。

implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"

为什么需要在androidx而不是android中打包新的支持库? 现有项目中的用例和影响因素。


根据文档:

Androidx是新的包结构,使它更清楚哪些包 都是与Android操作系统捆绑的,哪些是打包的 你的应用的APK。展望未来,机器人。*包层次结构 将保留给随操作系统一起发布的Android包 系统;其他软件包将在新的androidx中发布。*包 层次结构。

重新设计的包结构是为了鼓励更小、更集中的库。您可以在这里找到有关工件映射的详细信息。

当支持的最小SDK级别为14时,有命名为“v7”的支持库(包含向后兼容的组件和包),新的命名清楚地理解了平台绑定的api和应用程序开发人员在不同版本的Android上使用的库之间的划分。详情请参考官方公告。

AndroidX - Android扩展库

来自AndroidX文档

我们正在推出一个新的包装结构,以使其更明确 这些软件包都是与Android操作系统捆绑在一起的 与你的应用的APK打包。展望未来,机器人。*包 层次结构将保留给附带的Android包 操作系统。其他软件包将在新的androidx.*中发布 包层次结构作为AndroidX库的一部分。

需要AndroidX

AndroidX是一个重新设计的库,使包的名称更清晰。所以从现在开始,android的层次结构将只适用于android默认类,这与android操作系统和其他库/依赖将是androidx的一部分(更有意义)。所以从现在开始,所有的新开发都将在androidx中更新。

com.android.support。**: androidx。 com.android。支持:appcompat-v7: androidx.appcompat:appcompat com.android。支持:recyclerview-v7: androidx.recyclerview:recyclerview com.android。支持:design: com.google.android.material:material

AndroidX包的完整工件映射

AndroidX使用语义版本

以前,支持库使用SDK版本,而AndroidX使用语义版本。它将从28.0.0→1.0.0进行还原。

如何迁移当前项目

在Android Studio 3.2(2018年9月)中,可以直接选择将现有项目迁移到AndroidX。这将自动重构所有包。

在迁移之前,强烈建议备份您的项目。

现有的项目

Android Studio >重构菜单>迁移到AndroidX… 它将分析并在底部打开折射窗口。接受要完成的更改。

新项目

把这些标志放在gradle.properties中

android.enableJetifier=true
android.useAndroidX=true

检查@Library映射是否有相等的AndroidX包。

查看“迁移到AndroidX”的官方页面

什么是Jetifier?

迁移的bug

如果你构建应用程序,并在迁移后发现一些错误,那么你需要修复这些小错误。你不会被困在那里,因为这很容易解决。 第三方库不会在目录中转换为AndroidX,但它们会在运行时由Jetifier转换,所以不用担心编译时错误,你的应用程序将完美运行。

支持28.0.0是最新版本?

从Android支持修订版28.0.0

这将是android.support下的最后一个特性发布 我们鼓励开发人员迁移到AndroidX 1.0.0

所以选择AndroidX,因为Android从现在开始只更新AndroidX包。

进一步的阅读

https://developer.android.com/topic/libraries/support-library/androidx-overview

https://android-developers.googleblog.com/2018/05/hello-world-androidx.html

它与AppCompat版本的支持相同,但它的v4和v7版本的混乱较少,因此使用android XML元素的不同组件有很大帮助。

AndroidX是一个开源项目,Android团队使用它在Jetpack中开发、测试、打包、版本和发布库。

AndroidX是对原来的Android支持库的一个重大改进。像支持库一样,AndroidX与Android操作系统分开发布,并提供跨Android版本的向后兼容性。AndroidX通过提供特性校验和新库完全取代了支持库。

AndroidX包含以下特性:

All packages in AndroidX live in a consistent namespace starting with the string androidx. The Support Library packages have been mapped into the corresponding androidx.* packages. For a full mapping of all the old classes and build artifacts to the new ones, see the Package Refactoring page. Unlike the Support Library, AndroidX packages are separately maintained and updated. The androidx packages use strict Semantic Versioning starting with version 1.0.0. You can update AndroidX libraries in your project independently. All new Support Library development will occur in the AndroidX library. This includes maintenance of the original Support Library artifacts and introduction of new Jetpack components.

使用AndroidX

参见迁移到AndroidX了解如何迁移现有项目。

如果你想在一个新项目中使用AndroidX,你需要将编译SDK设置为Android 9.0 (API级别28)或更高,并在你的Gradle中将以下两个Android Gradle插件标志设置为true。属性文件。

android。useAndroidX:当设置为true时,Android插件使用适当的AndroidX库而不是支持库。国旗 如果未指定,则默认为false。 android。enableJetifier:当设置为true时,Android插件会自动迁移现有的第三方库,通过重写它们的二进制文件来使用AndroidX。缺省情况下,该标志为false 未指定。

对于工件映射,请参见此

Android Jetpack:最近的公告对Android的支持库意味着什么?解释得很好

Today, many consider the Support Library an essential part of Android app development, to the point where it’s used by 99 percent of apps in the Google Play store. However, as the Support Library has grown, inconsistencies have crept in surrounding the library’s naming convention. Initially, the name of each package indicated the minimum API level supported by that package, for example, support-v4. However, version 26.0.0 of the Support Library increased the minimum API to 14, so today many of the package names have nothing to do with the minimum supported API level. When support-v4 and the support-v7 packages both have a minimum API of 14, it’s easy to see why people get confused! To clear up this confusion, Google is currently refactoring the Support Library into a new Android extension library (AndroidX) package structure. AndroidX will feature simplified package names, as well as Maven groupIds and artifactIds that better reflect each package’s content and its supported API levels. With the current naming convention, it also isn’t clear which packages are bundled with the Android operating system, and which are packaged with your application’s APK (Android Package Kit). To clear up this confusion, all the unbundled libraries will be moved to AndroidX’s androidx.* namespace, while the android.* package hierarchy will be reserved for packages that ship with the Android operating system.

只是从我的角度对所有可用的答案进行了一些补充

需要AndroidX

正如@KhemRaj的惊人回答所说,

根据目前的命名惯例,还不清楚哪些包与Android操作系统捆绑,哪些包与应用程序的APK (Android Package Kit)捆绑。为了消除这种困惑,所有未捆绑的库将被转移到AndroidX的AndroidX上。*命名空间,而android。*包层次结构将保留给Android操作系统附带的包。

除此之外, 最初,每个包的名称表示该包支持的最小API级别,例如support-v4。但是,支持库的26.0.0版本将最低API增加到14,因此今天许多包的名称与受支持的最低API级别无关。当support-v4和support-v7包的最小API都为14时,很容易理解为什么人们会感到困惑!所以现在有了AndroidX,就不再依赖于API级别了。

另一个重要的变化是AndroidX构件将独立更新,因此您可以在项目中更新单个AndroidX库,而不必一次更改每个依赖项。那些令人沮丧的“所有com.android.support库必须使用完全相同的版本规范”消息应该成为过去!

AndroidX是一个开源项目,Android团队使用它在Jetpack中开发、测试、打包、版本和发布库。

经过几个小时的努力,我通过在app/build.gradle中包含以下内容来解决这个问题:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

把这些标志放在gradle.properties中

android.enableJetifier=true
android.useAndroidX=true

Changes in gradle:

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha04'

当在Android studio上迁移时,app/gradle文件会自动更新标准库中的修正库实现

参考网址:https://developer.android.com/jetpack/androidx/migrate

Androidx将在28.0.0之后取代支持库。您应该迁移您的项目来使用它。androidx使用语义版本控制。使用AndroidX不会被库名和包名中显示的版本所混淆。生活变得更容易

[AndroidX和支持兼容性]

我是从这个Android开发峰会的视频中了解AndroidX的。总结是-

No more support library: The android support library will be never maintained by Google under the support library namespace. So if you want to find fixes of a bug in support library you must have to migrate your project in AndroidX Better package management: For standardized and independent versioning.Because previous support library versioning was too confusing. It will release you the pain of “All com.android.support libraries must use the exact same version specification” message. Other Good libraries have migrated to AndroidX: Google play services, Firebase, Mockito 2, etc are migrated to AndroidX. New libraries will be published using AndroidX artifact: All the libraries will be in the AndroidX namespace like Android Jetpack

Android提供了几个不同的库集。一个叫Android支持库,另一个叫AndroidX。选择“使用android。”* artifacts”表示我们想要使用AndroidX。