Fork me on GitHub

Flutter - Migrate to AndroidX

一段时间没玩Flutter,今天打开一个项目编译了一下,突然发现不能编译了,出现

Launching lib\main.dart on Nokia X6 in debug mode...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:preDebugBuild'.
> Android dependency 'androidx.appcompat:appcompat' has different version for the compile (1.0.0-beta01) and runtime (1.1.0-alpha02) classpath. You should manually set the same version via DependencyResolution

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 15s
*******************************************************************************************
The Gradle failure may have been because of AndroidX incompatibilities in this Flutter app.
See https://goo.gl/CP92wY for more information on the problem and how to fix it.
*******************************************************************************************
Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

 

 

大概意思是说,安卓依赖在编译时和运行时出现了不同的版本,因为有了AndroidX的出现。

可能在你使用的依赖package的时候,你会注意到他们的更新日志,其中会有Migrate to AndroidX。

 

原因:

由于之前的Android.support包管理过于混乱,所以Google推出了AndroidX。如果各位开发者有时间的话,还是要尽量升级一下项目到AndroidX,毕竟这个是未来,谷歌的新标准。

 

怎么升级,错误信息已经给出了提示,进入 https://goo.gl/CP92wY  

 

①使用Android Studio自动升级

确保Android Studio版本是最新版,或者至少3.2以上。

打开你的项目,选中android文件夹,右键选择,Flutter,Open Android Module in Android Studio。

 

 

这个时候会打开一个新窗口,等它分析完成之后,选择Refactor——Migrate to AndroidX。

 

 

 

 检测完成后,下方出现提示,提示需要升级的地方,确认后点击“Do Refector”

 

 

升级完成后,关闭窗口即可。

这个时候你再编译Flutter,就没问题啦~~~~~~~~~~~~

 

 

 

 

②手动升级到AndroidX(不推荐)

 谷歌提示不推荐这个方法,麻烦,而且搞不好容易出错。

不过如果你没有安装Android Studio,你得采用这个手动升级的办法。

 

a)打开android/gradle/wrapper/gradle-wrapper.properties,修改distributionUrl的值,

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

 

b)打开android/build.gradle,修改com.android.tools.build:gradle 到3.3.0

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
}

 

c)打开android/gradle.properties,添加两行

android.enableJetifier=true
android.useAndroidX=true

 

d)打开android/app/build.gradle,在android{里面,确保compileSdkVersion 和 targetSdkVersion 值为28。

替换所有过时的依赖库android.support到androidx。

例如替换,需要一一查找。

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

为新的

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

 

在dependencies{里面,替换

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

为新的

androidTestImplementation 'androidx.test.runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

 

posted @ 2019-03-20 10:47  猫叔Vincent  阅读(8136)  评论(0编辑  收藏  举报