
今天下午将 Android studio 升级到了 3.2 版本,界面上有了小小的变化,多了一个新助手

另外侧边的 Build Variants 也变大了,多了当前 module 的 info 信息,可以查看当前模块的依赖关系


# 不过,最最重要的当属今天的主人公:AndroidX 了
# 1. 说明
官方原文如下
We hope the division between android.* and androidx.* makes it more obvious which APIs are bundled with the platform, and which are static libraries for app developers that work across different versions of Android.
简单地说就是新的库可以在不同的 Android 版本上使用。比如之前我们如果使用 support 为 27.1.1 的相关依赖库时。可能需要所有相关的 support 库都为 27.1.1。如果其中有 bug 的话,可能需要所有的都去升级,存在一个绑定关系,而且正式版的发布周期也很长。
通过 AndroidX,我们可以看到实时实现的特性和 bug 修复。升级个别依赖,不需要对使用的所有其他库进行更新。这就和我们使用 Github 上的开源库一样的,出了问题,我们可以提出 bug 和意见。作者修复后,发布新版本,我们就可以直接替换使用了。更加的透明便捷。


# 2. 变化
我选取了几个常用依赖库,我们可以看看变化:
<table style="height: 240px;" width="783">
<thead>
<tr>
<th>Old build artifact</th>
<th align="center">AndroidX build artifact</th>
</tr>
</thead>
<tbody>
<tr>
<td>com.android.support:support-compat</td>
<td align="center">androidx.core:core:1.0.0+</td>
</tr>
<tr>
<td>com.android.support:appcompat-v7</td>
<td align="center">androidx.appcompat:appcompat:1.0.0+</td>
</tr>
<tr>
<td>com.android.support:design</td>
<td align="center">com.google.android.material:material:1.0.0+</td>
</tr>
<tr>
<td>com.android.support:multidex</td>
<td align="center">androidx.multidex:multidex:2.0.0+</td>
</tr>
<tr>
<td>com.android.support:recyclerview-v7</td>
<td align="center">androidx.legacy:legacy-support-v4:1.0.0+</td>
</tr>
<tr>
<td>com.android.support:viewpager</td>
<td align="center">androidx.viewpager:viewpager:1.0.0+</td>
</tr>
<tr>
<td>com.android.support:support-fragment</td>
<td align="center">androidx.fragment:fragment:1.0.0+</td>
</tr>
</tbody>
</table>
当然涉及的不止这些库,更详细的变化内容可以查看官方文档。
我们可以添加 appcompat 依赖对比一下:
implementation 'com.android.support:appcompat-v7:28.0.0-beta1' |
或
implementation 'androidx.appcompat:appcompat:1.0.0-beta01' |
可以看到详细变化

同时我们看到 viewpager 、 swiperefreshlayout 、 coordinatorlayout 等一些 UI 组件被分离了出来,这样也是便于更好的使用,职责分明,以减轻不使用 ProGuard 或 Multidex 的应用程序和测试的压力。
# 3. 影响
官方博客中有说道,为了给开发者一定迁移的时间,所以 28.0.0 的稳定版本还是采用 android.support 。但是所有后续的功能版本都将采用 androidx 。
其实对于我们影响也不是很大,我们可以选择不使用,毕竟不是强制的。但长远看来还是有好处的。接受变化,拥抱变化也是我们程序猿需要有的精神,欢迎尝试。
对于有写一些开源项目的人,可能会有一些影响。比如你有一个关于 RecyclerView 的拓展库,那么你就需要去让他支持 <code>AndroidX</code>,否则你懂的。
我有去看了一下我们常用的 butterknife 、 glide 等都已经适配了 AndroidX ,不得不说真是很良心。

首先你的 gradle 版本至少为 3.2.0 以上,以及 compileSdkVersion 为 28 以上。
classpath <span class="hljs-string">'com.android.tools.build:gradle:3.2.0+' |
如果你是一个新项目,如果使用 AndroidX 相关依赖,需要在 gradle.properties 文件里添加配置:
android.useAndroidX=true | |
android.enableJetifier=true |
如果你想使用 AndroidX,但是之前的不迁移,可以这样配置:
android.useAndroidX=true | |
android.enableJetifier=false |
# 通过 Android Studio 自带的功能将 support 转换为 AndroidX

# 然后我们就获得了下面的 dependences 了
dependencies { | |
implementation fileTree(dir: 'libs', include: ['*.jar']) | |
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | |
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01' | |
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' | |
testImplementation 'junit:junit:4.12' | |
androidTestImplementation 'androidx.test:runner:1.1.1' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' | |
implementation 'com.yanzhenjie:permission:2.0.0-rc12' | |
implementation 'cn.bingoogolapple:bga-qrcode-zbar:1.3.6' | |
implementation 'com.cv4j:cv4j:0.1.2' | |
implementation 'com.tapadoo.android:alerter:4.0.0' | |
} |