Flutter3.19.6にアップグレードして発生したエラー対応記録

※当サイトは、アフィリエイト広告を利用しています

アプリ開発奮闘記

iOSのPrivacy Manifests対応のために、Flutterを3.7.7から3.19.6にアップデートしたが、大量のエラーが発生。

 

Chat-GPTを使っても解決しきれず、ネット上にも全く同じ状況の報告は見つからず。

 

解決に苦労したが、古いプロジェクトから新しいプロジェクトまで、ほぼ同じ内容で対処できたので、自分と類似環境の方のご参考になればと思い記録。

 

概要は以下の通り。

  • iOSの場合
    • targetバージョンを「13.0」に上げる
  • Androidの場合
    • compileSdkVersionとtargetSdkVersionを「34」に上げる
    • Java17をインストールして環境変数を設定する
    • kotlinのバージョンを「1.8.22」に上げる(「1.7.10」でも可)
    • 「gradle」のバージョンを「7.3.0」に上げる(に留める)※「8.##」系だとエラー、「7.4.2」だとAndroid Studioのバージョンが古いとエラー
    • 「google-services」のバージョンを「4.3.14」に上げる
    • 「gradle-wrapper.properties」に「gradle-7.5-all.zip」を設定 ※「6.#」や「8.#」だとエラー
    • 「app/build.gradle」にnamespaceを設定
    • minSdkVersionを「24」に上げる

 

前提とする環境

  • PC:MacBook Pro(Intel Core i5)
  • OS:macOS Sonoma 14.5
  • Flutter:3.7.7→3.19.6 ※fvmを使ってアップデート
  • Android Studio:Dolphin 2021.3.1 ※古いです...
  • Xcode:15.3

 

pubspec.yamlの各パッケージの更新

Privacy Manifests対応のため、firebase_analyticsを10.2.0→11.0.0に上げると、pub getがすんなり通らず、他の様々なパッケージも影響を受け、更新が必要になった。

 

この部分は、プロジェクトによって使用するパッケージも異なり、発生した依存性エラーに従って対処するしか無いので、詳細は省略。

 

generated_plugin_registrant.dartを削除

pub getが成功した後、「lib」フォルダ内にある「generated_plugin_registrant.dart」ファイル内で、いくつかのimport文にエラーが発生していた。

 

「generated_plugin_registrant.dart」は、Flutter webで使用するために自動生成されるファイルらしい。

 

パッケージのバージョンを色々変えてみるも解消せず。。

 

こちらによると、Webの開発予定が無ければ、削除しても良い模様。

 



なので、仕方なく「generated_plugin_registrant.dart」は削除することで解消。

 

iOSのビルド対応(targetバージョン修正)

Androidに比べるとエラーは少なかった。

 

targetを13.0に上げる

パッケージを更新したので、「Podfile.lock」を削除して、「pod install --repo-update」を実行。

 

CocoaPods could not find compatible versions for pod "firebase_analytics":
  In Podfile:
    firebase_analytics (from .symlinks/plugins/firebase_analytics/ios)

Specs satisfying the firebase_analytics (from .symlinks/plugins/firebase_analytics/ios) dependency were found, but they required a higher minimum deployment target.

 

上記のようなエラーが出たので、Podfileのtarget設定を「platform :ios, '13.0'」にし、13.0まで上げたところ、「pod install --repo-update」が通り、Xcodeから問題なくビルドできた。

 

Androidのビルド対応(Java17導入、Sdk・kotlin・gradleバージョン修正等)

なかなかエラーが消えず苦労した。。

 

エラーと対策が正確に対応しているか不明のため、、発生したエラー群と、効果があったと思われる対策群を列記する。

 

発生したエラー群

試行錯誤の過程で、大まかに区分すると、下記種類のエラーが発生した(順不同)。

  • SdkVersionを34にせよ
  • Java17が必要
  • gradleのバージョンが古い
  • namespaceの記述が必要

 

エラー①

One or more plugins require a higher Android SDK version.
Fix this issue by adding the following to /Users/###/android/app/build.gradle:
android {
  compileSdkVersion 34
  ...
}

 

エラー②

Execution failed for task ':connectivity_plus:compileDebugJavaWithJavac'.
> エラー: 17は無効なソース・リリースです

※この時点で、Java17は入っておらず、Javaのバージョンは11だった。

 

エラー③

Build file '/Users/####/android/build.gradle' line: 38

* What went wrong:
A problem occurred evaluating root project 'android'.
> com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: com/android/build/api/extension/AndroidComponentsExtension

※「android/build.gradle」のline:38の内容は「project.evaluationDependsOn(':app')」。

 

エラー④

A problem occurred configuring project ':firebase_analytics'.
> Failed to create Jar file /Users/####/.gradle/caches/jars-8/#####/lint-model-31.1.4.jar.
> Failed to notify project evaluation listener.
   > Could not get unknown property 'android' for project ':firebase_analytics' of type org.gradle.api.Project.
   > Could not find method implementation() for arguments [project ':firebase_core'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
   > Could not get unknown property 'android' for project ':firebase_analytics' of type org.gradle.api.Project.

 

エラー⑤

A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.version-check'.
   > Minimum supported Gradle version is 7.5. Current version is 7.4.2. If using the gradle wrapper, try editing the distributionUrl in /Users/####/android/gradle/wrapper/gradle-wrapper.properties to gradle-7.5-all.zip

 

エラー⑥

A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
      Your current JDK is located in /Applications/Android Studio.app/Contents/jre/Contents/Home
      You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing org.gradle.java.home in gradle.properties.

 

エラー⑦

A problem occurred evaluating root project 'android'.
> A problem occurred configuring project ':app'.
   > Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl.
      > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

        If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

 

エラー⑧

A problem occurred configuring project ':external_path'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

     If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

 

エラー⑨

Execution failed for task ':flutter_archive:compileDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

 

最後のエラーは、こちらに似たような報告はあるが、現時点では回答されていない。。

 

 

効果があった対策群

対策①

「android/app/build.gradle」の「compileSdkVersion」と「targetSdkVersion」を34に上げた

 

対策②

下記流れで、Java17をインストールし、環境変数を設定した。

  • 「brew install openjdk@17」でJava17をインストール
  • 「.zshrc」ファイル(bashの方は「.bash_profile」ファイル)に以下を追記
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
export PATH=$JAVA_HOME/bin:$PATH
  • Android Studioで、「Project Structure」→左メニューの「Platform Settings」の「SDKs」で、現在選択されている「Android API ## Platform」の「Build target」を「Android API 34」に変更し、「Java SDK」を「17」に変更

    ※通常は、Android Studioの「Preferences」からBuild, Execution, Deployment > Build Tools > Gradleに移動し、「Gradle JDK」を「Java 17」に設定できるらしいが、自分のAndroid Studioのバージョンが古いためか、そもそも「Gradle JDK」の欄が出てこず、試行錯誤の末、上記の方法になった。
     
  • 「android/gradle.properties」に以下を追記
org.gradle.java.home=/Library/Java/JavaVirtualMachines/openjdk-17.jdk/Contents/Home

 

対策③

「android/build.gradle」の「ext.kotlin_version」が、元々「1.6.10」だったが、「1.8.22」に変更し、kotlinのバージョンを上げた。

 

なお、「1.7.10」でもエラーにならず、ビルドできた。

 

対策④

「android/build.gradle」の「gradle」と「google-services」のバージョンを下記の通り「7.3.0」と「4.3.14」に上げた(元々は「4.1.0」と「4.3.10」)。

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0' // 4.1.0から上げた。8.1.0だとエラーになる

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        classpath 'com.google.gms:google-services:4.3.14'  // 4.3.10から上げた
    }

 

これと連動して、「android/gradle/wrapper/gradle-wrapper.properties」に、以下を追記した。

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

 

このバージョンは、6.7、7.4.2、8.0等だと、いずれもエラーを解消できなかった。

 

※当初、「com.android.tools.build:gradle」は7.4.2まで上げても、エミュレーター上では問題なく動作していたが、実際のリリースビルドを作成しようとすると、自分のAndroid Studioのバージョン(Dolphin)が古すぎるせいで、
 

The project is using an incompatible version (AGP 7.4.2) of the Android Gradle plugin. Latest supported version is AGP 7.3.0

 

というエラーが出てしまい、結局7.3.0まで下げる必要があった。Android Studioのバージョンが新しい方は、7.4.2で問題ないと思われる。

 

対策⑤

「android/app/build.gradle」の「android {・・}」のゾーンに、下記のとおりnamespaceとしてパッケージ名を追記した。

android {

    compileSdkVersion 34

    namespace 'com.###.####'  // ←namespace 'パッケージ名' を追記
    ・・・以下略・・・
}

 

対策⑥

「android/app/build.gradle」の「minSdkVersion」を21から24に上げた

 

当初は、「com.android.tools.build:gradle:」のバージョンを「8.1.0」に上げていたが、上に書いたエラー⑧ または エラー⑨(プロジェクトによって異なる)を解消できず、最終的に上記対策④のとおり「7.3.0」に下げ対策⑥ のとおり「minSdkVersion」を24に上げたら、ようやくエラーを解消できた。

 

特に、エラー⑧は、導入しているパッケージ毎に「namespace」を設定せよ、と言っているようだが、その通り対策することは、パッケージの数が多すぎて現実的ではない印象だった。。

 

こちらのQA記事を見ても、

 

 

「jvmToolchain(17)」の設定(後述)などでは解消できず、最終的に、gradleのバージョンを7系に下げたら解消したことが報告されている。

 

もっと良い対策があるのかもしれないが、、一旦ここまでで調査は打ち止めにした。

 

効果がなかった or うまくいかなかった対策

以下は、試行錯誤の中で対処したものの、効果がなかったものについて記載。

 


Android Studioのアップデート

 

Android Studioをアップデートすれば、Java17絡みの問題が自動的に解消される、という情報を多く見かけたのでやってみたが、FlutterとDartのPluginがエラーとなり、試行錯誤しても解消できなかったので、いったんアップデートは見送ることにした。。

 


こちらの記事を参考に、

 

 

「android/build.gradle」に、以下の内容を追記したが、エラーの解消にはならなかった。

  

    subprojects {
        afterEvaluate { project ->
            if (project.hasProperty('android')) {
                project.android {
                    if (namespace == null) {
                        namespace project.group
                    }
                }
            }
        }
    }

 

エラー⑧ にあるように、パッケージ毎に「namespace」の設定が求められる問題を解消できるのではと期待したが、自分の状況では効果がなかった。

 


「android/build.gradle」に、以下の内容を追記(もしくは、元々同様の記述はあったが、数字部分が「1_8」や「1.8」となっていた場合は、「17」に修正)したが、これもエラーの解消には直接影響はなかった。

 

android {
・・・略・・・
// 以下の内容を追記
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    kotlin {
        jvmToolchain(17)
    }
・・・略・・・
}

 

以上、理解不足ゆえ、恐らくもっと良い解決法があったと思われるが、、、同じ状況の方のご参考になれば幸いです。

 

最後までお読みいただき、ありがとうございました。

 

リリースしたアプリ(全てFlutterで開発)

コメント

タイトルとURLをコピーしました