一段時間沒玩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'