1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Flutter】Flutter 3.24.0にアップデートした際に発生したビルドエラーの解決方法

Last updated at Posted at 2024-09-30

はじめに

Flutter 3.24.0にアップデートした際に発生したビルドエラーの解決方法についてまとめます。

この記事は、私自身の環境で直面したエラーとその解決方法を記載しています。
依存関係やバージョンによって異なるエラーが発生する可能性があるため、その際にはエラーメッセージをもとに適切な対応方法をお探しいただければと思います。

また、ここで紹介する方法が最適解というわけではないかもしれないので参考程度にご覧いただけると幸いです。

前提

Flutter 3.16 ⇨ 3.19以降ではbuild.gradleの記述方法が変更されており、その影響で発生するエラーや対応策について詳しくは、こちらの記事公式で解説されています。この変更点を理解しておくことで、ビルドエラーの解決がスムーズに進むかもしれません。

確認したエラー

1. Error: Type 'UnmodifiableUint8ListView' not found.

エラー文

Error (Xcode): ../../../.pub-cache/hosted/pub.dev/win32-5.5.0/lib/src/guid.dart:32:9:
Error: Type 'UnmodifiableUint8ListView' not found.

原因

UnmodifiableUint8ListView クラスは、以前の Dart バージョンで利用されていましたが、Dart 3.5 で削除されました。このため、古いバージョンの win32 パッケージを使用している場合、このクラスが見つからないというエラーが発生します。

package_info_plusやdevice_info_plus等がWin32 APIに依存している?

実際に pubspec.lock を確認すると、win32 のバージョンが 5.1.1 になっていました。

pubspec.lock
packages:

  // 省略
  win32:
    dependency: transitive
    description:
      name: win32
      sha256: b0f37db61ba2f2e9b7a78a1caece0052564d1bc70668156cf3a29d676fe4e574
      url: "https://pub.dev"
    source: hosted
    version: "5.1.1"

pubspec.lockは現在のプロジェクトで使用されているすべての依存関係のバージョンが固定されています。詳しくは、以下の記事がわかりやすかったです。

対応方法

対応①: win32 のバージョンを上げる

issuesにある通り、win32: ^5.5.1以降にすればOK
win32 パッケージのバージョンを手動でアップグレードするには、以下のコマンドを実行します。

flutter pub upgrade win32
flutter clean
flutter pub get
対応②: pubspec.lock を削除する

pubspec.lock を削除してから flutter pub get を実行して依存関係を再解決することで、不要なキャッシュが原因で発生している問題を回避できるため、この方法が簡単かもしれません

更新されたpubspec.lockのwin32が5.5.1以降になっているか確認

pubspec.lock
packages:

  // 省略
  
  win32:
    dependency: transitive
    description:
      name: win32
      sha256: "68d1e89a91ed61ad9c370f9f8b6effed9ae5e0ede22a270bdfa6daf79fc2290a"
      url: "https://pub.dev"
    source: hosted
    version: "5.5.4"
対応③: 直接 archive と win32 を追加する

「だめだ...うまくいかない...」という場合は、直接archivewin32を追加する対応もあるそうです。

dependencies:
    archive: ^3.6.1
    win32: ^5.5.3

2. Execution failed for task ':********:verifyReleaseResources'.

エラー文

* What went wrong:
Execution failed for task ':flutter_app_badger:verifyReleaseResources'.
> A failure occurred while executing com.android.build.gradle.tasks.VerifyLibraryResourcesTask$Action
   > Android resource linking failed
     ERROR:/Users/user/****/build/flutter_app_badger/intermediates/merged_res/release/values/values.xml:194: AAPT: error: resource android:attr/lStar not found.

原因

このエラーは Android リリースビルド時に発生します。flutter_app_badgerで発生することもありますが、他のパッケージでも同様のエラーが報告されています。

依存関係のバージョンの不整合が起きている?
実際の原因はあまりよくわかっていないです。。有識者の方がいましたらご教授いただけますと幸いです。。

対応方法

android/build.gradleのsubprojectsを以下の様に修正する。

subprojects {
+   afterEvaluate {
+       android {
+           compileSdkVersion 34
+       }
+   }
}
subprojects {
+   project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

3. Dependency ':flutter_local_notifications' requires core library desugaring to be enabled

エラー文

このエラーは Android リリースビルド時に発生します。
flutter_local_notificationsで発生したエラーです。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkReleaseAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
   > An issues were found when checking AAR metadata:

       1.  Dependency ':flutter_local_notifications' requires core library desugaring to be enabled
           for :app.

           See https://developer.android.com/studio/write/java8-support.html for more
           details.

原因

このエラーはFlutter 3.24.0自体には関係しておらず、flutter_local_notificationsの特定のバージョンによるものです。

バージョン10以降では、Androidの古いバージョンとの下位互換性を保ちながら通知機能をサポートするには、densugaringを有効にする必要があるとのこと。

対応方法

compileOptionsと、desugaringコアライブラリへの依存関係を追加する必要があります。
android/app/build.gradleに以下追加

android {
    // 省略

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
+       coreLibraryDesugaringEnabled true
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }
    
    // 省略

}

    // 省略
dependencies {
    implementation ***********
    implementation ***********
+   coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
}

coreLibraryDesugaringは、使用しているAGP(Android Gradle Plugin)のバージョンによって適宜設定してください。

4.compileReleaseJavaWithJavac' (1.8) and 'compileReleaseKotlin' (17).

エラー文

このエラーは Android リリースビルド時に発生しました。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':qr_code_scanner:compileReleaseKotlin'.
> Inconsistent JVM-target compatibility detected for tasks 'compileReleaseJavaWithJavac' (1.8) and 'compileReleaseKotlin' (17).

  Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain
  Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation

原因

このエラーは Android リリースビルド時に発生します。
JVMのターゲットバージョンがJavaとKotlinで異なっているために発生している可能性が高い??

対応方法

この設定はエラーメッセージを無視してビルドを強制的に進めるため、根本的な解決にはならない可能性があります。

android/gradle.propertiesに以下追加

org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
+ kotlin.jvm.target.validation.mode = IGNORE

5. Namespace not specified.

エラー文

このエラーは Android リリースビルド時に発生しました。

* What went wrong:
A problem occurred configuring project ':flutter_app_badger'.
> 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.

原因

Gradle8.xになって「namespace」の記載場所がいくつかのFlutterのライブラリがそれを認識してくれない??

対応方法

android/build.gradleに以下追加

allprojects {
    repositories {
        google()
        mavenCentral()
    }
+    subprojects {
+       afterEvaluate { project ->
+           if (project.hasProperty('android')) {
+               project.android {
+                   if (namespace == null) {
+                       namespace project.group
+                   }
+               }
+           }
+       }
+   }
}

6. Error: 'CarouselController' is imported from both

エラー文

Error (Xcode): ../../../.pub-cache/hosted/pub.dev/carousel_slider-4.2.1/lib/carousel_slider.dart:9:1: Error: 'CarouselController' is imported from both
'package:carousel_slider/carousel_controller.dart' and 'package:flutter/src/material/carousel.dart'.

原因

carousel_slider: ^4.2.1を使用していたためエラー発生。

package:carousel_slider/carousel_controller.dart
package:flutter/src/material/carousel.dartの両方からCarouselControllerがインポートされているみたい。

対応方法

carousel_slider: ^5.0.0にバージョンを上げる
② 上記に伴い、CarouselControllerを使用していればCarouselSliderControllerへ変更する。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?