flutter.minSdkVersionとlintでビルドエラーが出た時の対処法
最終更新から4年以上たっています。ライブラリのバージョンや手順が変わっている可能性があります。
こんにちは。趣味グラマのNobu(@nm_aru)です。
久しぶりにAndroidエミュレーターで動作確認をしようとしたら、2つのエラーにぶつかったので、備忘録として残しておこうと思います。
uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library〜
利用しているライブラリが求めているAPIレベルを満たしていない場合に発生するエラーになります。
エラーメッセージが丁寧過ぎて、修正方法もググる必要も無く丸分かりです。
┌─ Flutter Fix ───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ The plugin google_mobile_ads requires a higher Android SDK version. │
│ Fix this issue by adding the following to the file /Users/hoge/app_name/android/app/build.gradle: │
│ android { │
│ defaultConfig { │
│ minSdkVersion 19 │
│ } │
│ } │
│ │
│ │
│ Note that your app won't be available to users running Android SDKs below 19. │
│ Alternatively, try to find a version of this plugin that supports these lower versions of the Android SDK. │
│ For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘早速該当ファイルを開いてみると、以下のようになっています。
android {
// 省略
defaultConfig {
applicationId "com.example.app_name"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
// 省略
}minSdkVersionのところに「flutter.minSdkVersion」という文字があり、どこかで定義されている値のように見えますが、プロジェクト全体をgrepしても見つかりません。
build.gradleと同階層に「android/app/local.properties」というファイルがあり、どうやらこの中で定義すれば良さそうに見えますが、ここに定義してもbuild.gradle側では読み込んでくれません。
sdk.dir=/Users/hoge/Library/Android/sdk
flutter.sdk=/opt/homebrew/Caskroom/flutter/3.0.4/flutter
flutter.buildMode=release
flutter.versionName=1.0.0
flutter.versionCode=1
flutter.minSdkVersion=21 # ←これを追加しても反映されない既にissue及びプルリクは上がっていましたが、どうやらlocal.propertiesを使う方法はAndroidの標準的なやり方ではなく、これをマージすると後方互換性を破壊するようで諦めたようです。
代わりにbuild.gradleに分かりやすいコメントをデフォルトで追加する方向になったのかな?

ただ、local.propertiesを使う方法はあるので、一応そのやり方を載せておきます。
build.gradle側でlocal.propertiesから値を取得するようにするだけです。
android {
// 省略
defaultConfig {
applicationId "com.example.app_name"
minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
// 省略
}Execution failed for task ‘:app:lintVitalProductionRelease’.
次はこんなエラーが出たのですが、凄い事に修正方法の中にissueへのリンクまで記載されています。
┌─ Flutter Fix ───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ This issue appears to be https://github.com/flutter/flutter/issues/58247. │
│ Fix this issue by adding the following to the file /Users/hoge/app_name/android/app/build.gradle: q│
│ android { │
│ lintOptions { │
│ checkReleaseBuilds false │
│ } │
│ } │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘まとめ
Flutterの開発をしている時、自分が普段使っている端末がiPhoneという事もあり、基本的にはiPhoneシミュレーターで動作確認をしているのですが、Androidでたまに確認しようとすると、ビルド周りでエラーになったりします。
ただ、Flutterはエラーメッセージがとても丁寧なので、こういう時は非常に助かります。
Flutter の記事
コメントの受け付けは終了しました。ご意見・ご質問は X(@nm_aru)へどうぞ。


