LoginSignup
2
1
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

FlutterでAndroidビルド時の「 Execution failed for task ':app:compileDebugKotlin'.(Kotlinでの)Compilation error. See log for more details(コンパイルエラー)」について

Last updated at Posted at 2024-07-01

こんにちわ、だいちです。

約1週間悩まされたAndroidのビルドエラーがやっと解決しました!!
関連記事も少なく、
Flutterが詳しい方に聞いたりChat-GPTに聞いたり
参考文献を読み漁りと時間がかかったのでこちらに載せておきます。

目次

-開発環境
-1.経緯
-2.原因
-3.Androidビルドエラー①_android/build.gradleのバージョン変更
-4.Androidビルドエラー②_android/settings.gradleでバージョン指定
-5.Androidビルドエラー③_android/app/build.gradleで削除・追加処理
-6.Androidビルドエラー④_androidディレクトリでキャッシュのクリア
-7.全体コード
-参考文献
-最後に

開発環境

・macOS Sonoma 14.5
・Flutter 3.22.2

1.経緯

・FirebaseをFlutterアプリに取り入れた
・AndroidのエミュレータでFlutterアプリのデバッグのためビルドしたところ、
 デバックコンソール内に下記が表示された
・また下記エラーはFirebaseを入れたFlutterアプリにのみ発生した

デバッグコンソール
 * What went wrong:
 Execution failed for task ':app:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
  > Compilation error. See log for more details

・調べても対応方法が一致しているものが見つからなかったため
 備忘録として残すこととした
・今回は対処前の記載の記録がないため対処した処理内容を記載する

2.原因

・要求するKotlinおよびGradleのバージョンが不一致であること
・また色々と調べすぎるあまり、不要な処理を記載していた箇所もあった

3.Androidビルドエラー①_android/build.gradleのバージョン変更

・android/build.gradeファイルのKotlinのバージョンを変更する
 ※android→build.gradeファイルとandroid→app→build.gradeファイル
   が存在するので注意が必要

android/build.gradle
buildscript {
    ext.kotlin_version = '1.9.0' //←ここで使用するKotlinのバージョンを設定します
    repositories {
        google()
        mavenCentral()
    }

4.Androidビルドエラー②_android/settings.gradleでバージョン指定

・android/settings.gradeファイルのKotlinのバージョンを指定する
 ※こちらの処理は下記記事を参考にしました!

android/settings.gradle
plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "com.google.gms.google-services" version "4.3.15" apply false
    id "org.jetbrains.kotlin.android" version "1.9.0" apply false//←こちらでバージョン指定処理を追加
}

5.Androidビルドエラー③_android/app/build.gradleで削除・追加処理

・android/app/build.gradleファイルの拡張機能有効処理を削除する
 ※おそらく初期にはなかったのですが、調べていくうちに追加してしまった処理です

android/app/build.gradle
plugins {
    id "com.android.application"
    id "com.google.gms.google-services"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id "kotlin-android-extensions"//←こちらの処理を削除する
}

・下記URLを基に
 android Widget内にviewBindingビルドオプションを追加
 ※下記URLには、上記の削除処理に関しても書いてます
 https://developer.android.com/topic/libraries/view-binding/migration#groovy

android/app/build.gradle
android {
    compileSdkVersion 34
    namespace = "your applicationId"

        buildFeatures {//←この処理を追加
        viewBinding true
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    defaultConfig {
        applicationId = "your applicationId"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.debug
        }
    }
}

6.Androidビルドエラー④_androidディレクトリでキャッシュのクリア

・ターミナルでandroidディレクトリに移動し、キャッシュのクリアを実行
([your user name]、[your folder name]、[your project name]は個人でそれぞれ)

sh
cd /Users/[your user name]/development/Flutter/[your folder name]/[your project name]/android

./gradlew clean

./gradlew cleanBuildCache

この処理後にビルドすれば成功しました!!!

7.全体コード

android/build.gradle
buildscript {
    ext.kotlin_version = '1.9.0' 
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.8'
    }
    }


allprojects {
    repositories {
        google()
        mavenCentral()
    }
}



rootProject.buildDir = "../build"
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

android/settings.gradle
pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }

        resolutionStrategy {
        eachPlugin {
            if (requested.id.namespace == "org.jetbrains.kotlin") {
                useVersion("1.9.0")
            }
        }
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "com.google.gms.google-services" version "4.3.15" apply false
    id "org.jetbrains.kotlin.android" version "1.9.0" apply false
}

include ":app"

android/app/build.gradle
plugins {
    id "com.android.application"
    id "com.google.gms.google-services"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader("UTF-8") { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "1"
}

def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
    flutterVersionName = "1.0"
}

android {
    compileSdkVersion 34
    namespace = "your applicationId"

        buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    defaultConfig {
        applicationId = "your applicationId"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation platform("com.google.firebase:firebase-bom:33.1.1")
    implementation "com.google.firebase:firebase-analytics"
    implementation "androidx.multidex:multidex:2.0.1"
}

参考文献

@169ryo . 「FlutterでAndroidビルド時に「your project requires a newer version of the kotlin gradle plugin.」というエラーになる」. Qiita. 2024/06,
https://qiita.com/169ryo/items/5f1c56744e54cd0c7978, (参照 2024-07-01)

Developers.
「Kotlin シンセティックから Jetpack ビュー バインディングに移行する」. 2023/07,
https://developer.android.com/topic/libraries/view-binding/migration#groovy,
(参照 2024-07-01)

最後に

・Swiftと違いまだまだ日本語の参考文献が少ないイメージがありました
・エラーの解決によりさらにFlutterの知識が深まったと感じています
・Flutterの更新に関しては様々なファイルでの変更が必要と感じました
・エラーに相談乗ってくださった方々、有難うござました!!

2
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
2
1