AndroidX 是什么?

What is AndroidX?

我正在阅读 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"

基于documentation

androidx is new package structure to make it clearer which packages are bundled with the Android operating system, and which are packaged with your app's APK. Going forward, the android.* package hierarchy will be reserved for Android packages that ship with the operating system; other packages will be issued in the new androidx.* package hierarchy.

重新设计的包结构旨在鼓励更小、更专注的库。您会找到有关工件映射的详细信息 here.

当支持的最低 SDK 级别为 14 时,有名为 "v7" 的支持库(包含用于向后兼容的组件和包),新命名清楚地理解了与平台捆绑的 API 与Android 的不同版本上使用的应用程序开发人员库。详情可参考official announcement

AndroidX - Android 扩展库

来自AndroidX documentation

We are rolling out a new package structure to make it clearer which packages are bundled with the Android operating system, and which are packaged with your app's APK. Going forward, the android.* package hierarchy will be reserved for Android packages that ship with the operating system. Other packages will be issued in the new androidx.* package hierarchy as part of the AndroidX library.

需要 AndroidX

AndroidX 是一个重新设计的库,使包名更加清晰。所以从现在开始 android 层次结构将仅适用于 android 默认 类,android 操作系统和其他 library/dependencies 将成为 androidx 的一部分(更有意义)。所以从现在开始,所有的新开发都将在 androidx.

中更新

com.android.support.** : androidx.
com.android.support:appcompat-v7 : androidx.appcompat:appcompat com.android.support:recyclerview-v7 : androidx.recyclerview:recyclerview com.android.support:设计:com.google.android.material:material

Complete Artifact mappings for AndroidX packages

AndroidX 使用 Semantic-version

以前,support library 使用 SDK 版本,但 AndroidX 使用 Semantic-version。将从 28.0.0 → 1.0.0 重新版本。

如何迁移当前项目

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

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

Existing project

  • Android Studio > 重构菜单 > 迁移到 AndroidX...
  • 它将分析并在底部打开 Refractor window。接受要完成的更改。

New project

将这些标志放入您的 gradle.properties

android.enableJetifier=true
android.useAndroidX=true

检查@Library mappings for equal AndroidX package.

检查@Official page of Migrate to AndroidX

迁移错误

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

支持 28.0.0 是最后一个版本吗?

来自Android Support Revision 28.0.0

This will be the last feature release under the android.support packaging, and developers are encouraged to migrate to 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团队用来在[=内开发、测试、打包、版本和发布库的开源项目15=].

AndroidX是对原来的Android Support Library的重大改进。与支持库一样,AndroidX 与 Android OS 分开发布,并提供跨 Android 版本的向后兼容性。 AndroidX 通过提供功能对等和新库完全取代了支持库。

AndroidX includes the following features:

  • AndroidX 中的所有包都位于以字符串 androidx 开头的一致命名空间中。已映射支持库包 进入相应的 androidx.* 包。对于所有的完整映射 旧 类 并为新的构建工件,请参阅包 重构页面。

  • 与支持库不同,AndroidX 包是单独维护和更新的。 androidx 包从版本 1.0.0 开始使用严格的 Semantic Versioning。您可以更新 AndroidX 项目中独立的库。

  • 所有新的支持库开发都将在 AndroidX 库中进行。这包括维护原始支持库 工件和新 Jetpack 组件的引入。

使用 AndroidX

请参阅 Migrating to AndroidX 了解如何迁移现有项目。

如果要在新项目中使用AndroidX,需要将编译SDK设置为Android9.0(API28级)或更高版本,并同时设置以下 Android Gradle 插件标志在您的 gradle.properties 文件中为真。

  • android.useAndroidX:当设置为 true 时,Android 插件使用适当的 AndroidX 库而不是支持库。国旗 不指定默认为false

  • android.enableJetifier:设置为 true 时,Android 插件会通过重写二进制文件自动迁移现有的第三方库以使用 AndroidX。如果是,则该标志默认为 false 未指定。

对于工件映射请参阅this

这篇文章Android Jetpack: What do the recent announcements mean for Android’s Support Library?解释的很好

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

  1. 正如@KhemRaj 在惊人的回答中所说,

根据当前的命名约定,尚不清楚哪些软件包与 Android 操作系统 捆绑在一起,哪些与应用程序的 APK 捆绑在一起(Android 套件)。为了消除这种混乱,所有未捆绑的库都将移至 AndroidX 的 androidx.* 命名空间,而 android。 * 包层次结构将保留给 Android 操作系统附带的包。

  1. 除此之外,

    最初,每个包的名称表示该包支持的最低 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 之后的 support library。您应该迁移您的项目以使用它。 androidx 使用 Semantic Versioning。使用 AndroidX 不会与库名称和包名称中显示的版本混淆。生活变得更轻松

我是从这个Android Dev Summit video开始了解AndroidX的。总结是-

  1. 不再支持库: android 支持库将永远不会由 Google 在支持库命名空间下维护。所以如果你想在支持库中找到错误的修复,你必须在 AndroidX
  2. 中迁移你的项目
  3. 更好的包管理:为了标准化和独立versioning.Because以前的支持库版本控制太混乱了。它会让你摆脱 “所有 com.android.support 库必须使用完全相同的版本规范” 消息的痛苦。
  4. 其他好的库已迁移到 AndroidX:Google 播放服务、Firebase、Mockito 2 等已迁移到 Android X.
  5. 将使用 AndroidX 工件发布新库: 所有库都将位于 AndroidX 命名空间中,如 Android喷气背包

Android 提供了几个不同的库集。一个称为 Android 支持库,另一个称为 AndroidX。选择 "Use android.* artifacts" 表示我们要使用 AndroidX.