The minCompileSdk (31) specified in a dependency’s AAR metadata からのエラー3コンボ

2021年11月から更新してなかったFlutterのプロジェクトを2022年3月にAndroidで実行しようとすると、エラーが連続で発生した。せっかくなので、記録を残します。

環境

2022/03/21に実施。


>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.19043.1586], locale ja-JP)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2021.1)
[√] Connected device (4 available)
[√] HTTP Host Availability

一つ目のエラー SDKのバージョンが古い

エラー内容

Launching lib\main.dart on sdk gphone x86 in debug mode...
Running Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
   > One or more issues found when checking AAR metadata values:

     The minCompileSdk (31) specified in a
     dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
     is greater than this module's compileSdkVersion (android-30).
     Dependency: androidx.window:window-java:1.0.0-beta04.
     AAR metadata file: C:\Users\sakushin\.gradle\caches\transforms-3\b547df708ad37f6e2989d2af3e4bc19e\transformed\jetified-window-java-1.0.0-beta04\META-INF\com\android\build\gradle\aar-metadata.properties.

     The minCompileSdk (31) specified in a
     dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
     is greater than this module's compileSdkVersion (android-30).
     Dependency: androidx.window:window:1.0.0-beta04.
     AAR metadata file: C:\Users\sakushin\.gradle\caches\transforms-3\43315534fa954a42403d881ae628209c\transformed\jetified-window-1.0.0-beta04\META-INF\com\android\build\gradle\aar-metadata.properties.

* 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 1s

解決策

android/app/build.gradle のcompileSdkVersionとtargetSdkVersion を31にした。

【追記】さらに古いプロジェクトを編集したときにも、別のエラーが出ましたので、minSdkVersionを23にする、も追記しました(もともと20でした)。

android {
    compileSdkVersion 31
    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 31
    }
}

二つ目のエラー exportedの設定がない

エラー内容

Launching lib\main.dart on sdk gphone x86 in debug mode...
Running Gradle task 'assembleDebug'...
D:\project\udemy\riverpod_future\android\app\src\main\AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for . Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : android:exported needs to be explicitly specified for . Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

* 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 1s
Exception: Gradle task assembleDebug failed with exit code 1

解決策

android/app/src/main/AndroidManifest.xml に android:exported=”true” を追加した

<manifest>
   <application>
        <activity android:exported="true">
         </activity>
     </application>
</manifest>

三つ目のエラー kotolinのバージョンが古い

エラー内容

┌─ Flutter Fix ────────────────────────────────────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin Gradle plugin.                       │
│ Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then │
│ update ${project_path}\android\build.gradle:                                │
│ ext.kotlin_version = ''                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────┘
Exception: Gradle task assembleDebug failed with exit code 1

解決策

android/build.gradle のkotolinのバージョンを新しくした

buildscript {
    ext.kotlin_version = '1.5.0'
}

まとめ

以上で動作するようになった。地味に時間が掛かったので、ご覧の方がすぐに解決できると良いです。

参考