状況
Flutter のバージョンを上げたらビルドできなくなりました。
Android Studio: 3.5.3
Flutter: 1.12.13+hotfix.7
やったこと
端的に言うと、次の2点です。
- 依存ライブラリのアップデート
- Gradleのアップデート
- guava の問題の解消
以下、詳細です。
依存ライブラリのアップデート
具体的には下のようなエラーが出ていました。
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:preDebugBuild'.
> Android dependency 'androidx.lifecycle:lifecycle-viewmodel' has different version for the compile (2.0.0) and runtime (2.1.0) 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 6s
なにやらバージョンが違うらしいので android/app/build.gradle
を確認してみると、下図のようにいくつかのライブラリのバージョン古いと警告されていました。
この警告に従って、バージョンを最新に更新しました。
※ $kotlin_version
は android/build.gradle
の ext.kotlin_version
で定義されている値を更新します。
念のため File > Invalidate Cache / Restart...
を実行しました。
Gradleのアップデート
再度ビルドをすると、下のようなエラーに変わりました。
FAILURE: Build failed with an exception.
* Where:
Build file '/path/to/project/android/app/build.gradle' line: 24
* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin [id 'com.android.application']
> Minimum supported Gradle version is 5.4.1. Current version is 4.10.2. If using the gradle wrapper, try editing the distributionUrl in /path/to/project/android/gradle/wrapper/gradle-wrapper.properties to gradle-5.4.1-all.zip
* 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 10s
Finished with error: Gradle task assembleDebug failed with exit code 1
Gradle のバージョンが古いと言っています。
ドキュメントによると、5.4.1
〜5.6.4
にアップグレードすれば良さそうです。
https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
android/gradle/wrapper/gradle-wrapper.properties
を開き、バージョンを書き換え、再度ビルドします。
# distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
# ↓
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
guavaの問題の解消
今度は guava まわりの依存解決?がうまくいっていないようで、下記のエラーが出ました。
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules guava-20.0.jar (com.google.guava:guava:20.0) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)
Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>.
* 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 2s
Finished with error: Gradle task assembleDebug failed with exit code 1
下記記事を参考に、android/app/build.gradle
の dependencies
の前に configurations
を追加しました。
https://stackoverflow.com/questions/25792398/gradle-transitive-dependency-exclusion-is-not-working-as-expected-how-do-i-get
configurations {
all*.exclude group: 'com.google.guava', module: 'listenablefuture'
}
以上で、従来通りビルドが通るようになりました。