Flutter
[Flutter][해결]Android 실행 시 에러(Migration하기)
코린이탈출기
2024. 3. 10. 15:52
728x90
에러
이번에 플러터 3.7.12에서 3.19.2로 업그레이드 후 Android 빌드 중 만난 에러.
You are applying Flutter's app_plugin_loader Gradle plugin imperatively using the apply script method, which is deprecated and will be removed in a future release. Migrate to applying Gradle plugins with the declarative plugins block: https://flutter.dev/go/flutter-gradle-plugin-apply
You are applying Flutter's main Gradle plugin imperatively using the apply script method, which is deprecated and will be removed in a future release. Migrate to applying Gradle plugins with the declarative plugins block:
https://flutter.dev/go/flutter-gradle-plugin-apply
링크의 flutter 문서에서 다음과 같이 설명하고 있다.
In Flutter 3.16, support was added for applying these plugins with Gradle’s declarative plugins {} block
(also called the Plugin DSL) and it is now the recommended approach. Since Flutter 3.16, projects generated with
flutter create use the Plugin DSL to apply Gradle plugins. Projects created with versions of Flutter prior to 3.16 need to be migrated manually.
Flutter 3.16에서는 Gradle의 선언적 플러그인 {} 블록(플러그인 DSL이라고도 함)을 사용하여 이러한 플러그인을 적용하기 위한 지원이 추가되었으며 이제는 권장되는 접근 방식입니다.
Flutter 3.16부터 flutter create로 생성된 프로젝트는 플러그인 DSL을 사용하여 Gradle 플러그인을 적용합니다.
3.16 이전 버전의 Flutter로 생성된 프로젝트는 수동으로 마이그레이션해야 합니다.
Migrate(마이그레이션 하기)
1. 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()
}
}
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "{agpVersion}" apply false
id "org.jetbrains.kotlin.android" version "{kotlinVersion}" apply false
}
include ":app"
{apgVersion} 과 {kotlinVersion}은 'android/build.gradle'에서 찾을 수 있다.
이 버전 숫자를 적어주면 된다.
plugins {
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
}
2. android/build.gradle에서 buildscript 블록 제거
-buildscript {
- ext.kotlin_version = '{kotlinVersion}'
- repositories {
- google()
- mavenCentral()
- }
-
- dependencies {
- classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
- }
-}
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
}
3. android/app/build.gradle에 플러그인 DSL이 적용된 플러그인 코드 작성
먼저, 아래의 코드들을 제거한다.
-def flutterRoot = localProperties.getProperty('flutter.sdk')
-if (flutterRoot == null) {
- throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
-}
-apply plugin: 'com.android.application'
-apply plugin: 'kotlin-android'
-apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
그리고 제일 상단에 플러그인 DSL이 적용된 플러그인 코드를 작성한다.
+plugins {
+ id "com.android.application"
+ id "kotlin-android"
+ id "dev.flutter.flutter-gradle-plugin"
+}
4. 앱 빌드 확인
프로젝트 터미널에서 명령어를 실행한다.
flutter run
앱을 빌드시키고 Android 애뮬레이터도 잘 실행되었다.
만약 빌드 시 Could not get unknown property 'kotlin_version' 에러가 났다면,
android/app/build.gradle에서 dependencies 블록의 kotlin_version 코드를 제거해주면 된다.
[참고]
https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply
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
728x90