본문 바로가기
프로그래밍/Flutter <Dart>

[Flutter] Deprecated imperative apply of Flutter's Gradle plugins

by TaeGyeong Lee 2024. 9. 25.

개요 

Flutter의 Android 앱 빌드 방식이 3.16 버전부터 변경되었습니다. 플러그인을 가져오는 Gradle 의 명령형 방식이 바뀌었습니다. 추후 Deprecated될 예정이므로 코드를 수정하겠습니다. 

 

android/build.gradle 수정 

원본 파일에서 

  • ext.kotlin_version 이 1.7.10 이고 
  • com.android.tools.build:gradle이 7.3.0 

임을 명심하세요. 각 프로젝트마다 다를 수 있습니다. 

< 원본 > 

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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
}

 

아래 블록을 지워주세요. 

  • buildscript { } 

결과는 아래와 같습니다. 

< 수정본 > 

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
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

    plugins {
        id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false
    }
}

include ":app"

apply from: "${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle/app_plugin_loader.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
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

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

plugins {
    // id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

include ":app"

// apply from: "${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle/app_plugin_loader.gradle"

 

android/app/build.gradle 수정 

참고 자료를 참고해 주세요. 저는 수정할 사항이 없어 패스하겠습니다. 

 

Google Mobile Services and Crashlytics 대응 

Google의 특정 서비스를 사용하는 경우, 참고자료를 참고해 주세요. 저는 수정할 사항이 없어 패스하겠습니다. 

 

참고) Unresolved reference: io 대응 

수정 후 Android SDK 를 Android Studio에서 인식하지 못해 아래와 같이 빌드 에러가 발생할 수 있습니다. 

e: /.../android/app/src/main/kotlin/com/example/flutter_application_1/MainActivity.kt: (3, 8): Unresolved reference: io
e: /.../android/app/src/main/kotlin/com/example/flutter_application_1/MainActivity.kt: (5, 21): Unresolved reference: FlutterActivity

* 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


File > Project Structure > Project 에서 아래와 같이 SDK가 선택되어 있지 않음을 확인할 수 있습니다. SDK를 선택 후 apply 해주세요. 

 

모든 수정을 완료 후 Flutter run 명령을 시행하여 잘 빌드되는 지 확인해 주세요.

 

참고 자료 

 

Deprecated imperative apply of Flutter's Gradle plugins

How to migrate your Flutter app's Android Gradle build files to the new, declarative format.

docs.flutter.dev