본문 바로가기

Side Project

[flutter] 안드로이드 스튜디오 빌드 오류

flutter로 앱을 만드는 중에 XCode와 IOS Simulator를 통해 IOS 환경에서 테스트 빌드는 잘 실행되어 UI도 확인하고 기능도 테스트해볼 수 있었다.

이제 android 환경에서도 테스트 해보기 위해 android studio 설치해서 테스트 빌드 실행하는 순간...!

에러가 발생했다...

에러가 한두개가 아니니 단계별로 에러의 원인과 해결방법을 기록해두려고 한다.

1. settings.gradle의 plugins 블록 위치 오류

FAILURE: Build failed with an exception.

\* Where:

Settings file '/Users/yiju/dev/shifter/android/settings.gradle' line: 28

\* What went wrong:

Could not compile settings file '/Users/yiju/dev/shifter/android/settings.gradle'.

\> startup failed:

settings file '/Users/yiju/dev/shifter/android/settings.gradle': 28: only buildscript {}, pluginManagement {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

For more information on the plugins {} block, please refer to [https://docs.gradle.org/8.3/userguide/plugins.html#sec:plugins\_block](https://docs.gradle.org/8.3/userguide/plugins.html#sec:plugins_block) in the Gradle documentation.

@ line 28, column 1.
plugins {
^
1 error

Gradle 8.x 버전의 경우 plugins 블록의 위치를 엄격하게 제한한다.
때문에 settings.gradle 파일의 pluginManagement {}, plugins {}, dependencyResolutionManagement {} 의 순서대로 위치를 수정해준다.

해결

pluginManagement {
    ...
}

plugins {
    ...
}

dependencyResolutionManagement {
    ...
}

2. settings.gradle과 build.gradle 중복 오류

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/yiju/dev/shifter/android/build.gradle' line: 3

* What went wrong:
A problem occurred evaluating root project 'shifter'.
> Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'

이 에러는 settings.gradle 파일에 이미 저장소를 정의했는데, build.gradle 파일에서도 중복 정의하고 있어서 발생한 에러이다.
/android/build.gradle 파일을 다음과 같이 수정하여 해결한다.

해결

// /android/build.gradle

// 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. flutter gradle 플러그인이 프로젝트 레벨에서 저장소 추가 오류

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/yiju/dev/shifter/android/app/build.gradle' line: 5

* What went wrong:
An exception occurred applying plugin request [id: 'dev.flutter.flutter-gradle-plugin']
> Failed to apply plugin 'dev.flutter.flutter-gradle-plugin'.
   > Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by plugin 'dev.flutter.flutter-gradle-plugin'

위 오류는 flutter plugin이 프로젝트 레벨에서 저장소를 추가하려고 시도했을 때 발생하는 오류이다.
settings.gradle 파일의 부분을 수정하여 해결한다.

해결

// /android/settings.gradle
...

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        google()
        mavenCentral()
        // Flutter 엔진 컴포넌트를 위한 Maven 저장소
        maven {
            url 'https://storage.googleapis.com/download.flutter.io'
        }
    }
}

...
  • PREFER_SETTINGS 로 설정을 변경해주면 프로젝트 레벨의 저장소 설정을 허용하면서 settings 레벨의 설정을 우선시한다.
  • flutter plugin에서 필요한 maven 설정도 추가했다. url은 flutter의 공식 Maven 저장소를 의미한다.

4. android 애플리케이션 클래스 못찾아서 발생하는 오류

E/AndroidRuntime(10794): FATAL EXCEPTION: main
E/AndroidRuntime(10794): Process: com.example.shifter, PID: 10794
E/AndroidRuntime(10794): java.lang.RuntimeException: Unable to instantiate application com.example.shifter package com.example.shifter: java.lang.ClassNotFoundException: Didn't find class "com.example.shifter" on path: ...

이 경우 AndroidManifest.xml 파일에서 android:name을 알맞게 할당해줘야한다.

해결

// /android/app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
    <application
        android:name="${applicationName}" // 이 부분 수정!
...

5. Google Mobile Ads SDK 버전 호환성 오류

setRequestAgent(Ljava/lang/String;)Lcom/google/android/gms/ads/AdRequest$Builder; in class ...

Lost connection to device.
the Dart compiler exited unexpectedly.

이 경우 현재 사용 중인 google mobile ads sdk 버전과 Flutter google_mobile_ads 플러그인 버전이 맞지 않아서 발생한 오류이다.

해결

// /android/app/build.gradle

plugins {
  ...
}

dependencies {
      implementation 'com.google.android.gms:play-services-ads:22.6.0'
}

...

play-services-ads의 버전을 호환 가능한 버전으로 수정해준다.