Loading...  今天下午将Android studio升级到了3.2版本,界面上有了小小的变化,多了一个新助手  另外侧边的Build Variants也变大了,多了当前module的info信息,可以查看当前模块的依赖关系   ####不过,最最重要的当属今天的主人公:**AndroidX**了 ##1.说明 [官方原文如下](https://developer.android.google.cn/topic/libraries/support-library/revisions#28-0-0-alpha1) >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> 当然涉及的不止这些库,更详细的变化内容可以查看[官方文档](https://developer.android.google.cn/topic/libraries/support-library/refactor)。 我们可以添加appcompat依赖对比一下: ```java implementation 'com.android.support:appcompat-v7:28.0.0-beta1' ``` ** 或** ```java 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以上。 ```java classpath <span class="hljs-string">'com.android.tools.build:gradle:3.2.0+' ``` 如果你是一个新项目,如果使用AndroidX相关依赖,需要在```gradle.properties```文件里添加配置: ```java android.useAndroidX=true android.enableJetifier=true ``` 如果你想使用AndroidX,但是之前的不迁移,可以这样配置: ```java android.useAndroidX=true android.enableJetifier=false ``` #####通过Android Studio自带的功能将support转换为AndroidX  #####然后我们就获得了下面的dependences了 ```java 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' } ``` 最后修改:2021 年 07 月 22 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏