1. 상황


플러터 내부에 AdMob 배너를 설치하던 중에 다음과 같은 에러가 발생했다.

The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers should follow the instructions here:
https://googlemobileadssdk.page.link/admob-android-update-manifest
to add a valid App ID inside the AndroidManifest.
Google Ad Manager publishers should follow instructions here:
https://googlemobileadssdk.page.link/ad-manager-android-update-manifest.

에러 메세지는 이러하고, 발생 원인은 간단했다. Admob을 초기화해줄 프로바이더를 찾을 수 없어서 런타임 익셉션이 발생한 것.

java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException

웬만하면 해결한 글을 찾을 수 있는데, 내가 겪은 에러에 대한 포스팅은 없었다.



2. 해결방법


일단 내 문제는 AndroidManifest.xmlmeta-data 를 추가하지 않아서 생긴 게 아닌데,
다들 meta-data의 값을 제대로 넣지 않아서 생긴 에러라고 짚는 걸까...


문제를 해결하고나니 내가 정말 바보였다는 것을 새삼 깨달았다.

문서에서 지시한대로 meta-data와 해당 값을 제대로 넣었음에도 불구하고 계속 위와 같은 에러가 발생한 경우에는 아래와 같이 위치를 변경해 본다.


변경 전 코드

<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:label="cookbook"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">

		<!-- 이 메타데이터의 위치가 잘못됨 !! (ID는 공개된 테스트용 아이디) -->          
         <meta-data
              android:name="com.google.android.gms.ads.APPLICATION_ID"
              android:value="ca-app-pub-3940256099942544~6300978111"
              />
          
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
            
    </application>
</manifest>

meta-data<application> 태그 내에 위치해 있다.
그동안 대부분의 작업들을 <application> 내부에 작성했기 때문에 당연하게 저기에다가 작성했다.

이게 잘못이었다. meta-data 의 위치를 애초에 <application> 바깥으로 빼자.
생각해보면, flutter 에서도 runApp 을 실행하기 전에 init을 수행하는데, 나는 뭔 생각으로 저 안에다가 넣었을까.


변경 코드

<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:label="cookbook"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <!-- activity 안에 넣으면 에러뜸 -->
         <meta-data
              android:name="com.google.android.gms.ads.APPLICATION_ID"
              android:value="ca-app-pub-3940256099942544~6300978111"
              />
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
           
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
            
    </application>
</manifest>

그냥 meta-data 태그를 <application> 바깥으로 빼줬다. 그러니까 바로 앱이 실행되더라. (바보라서 허탈...)


아무쪼록 다른 분들은 이런 삽질은 안겪었으면 하는 바람으로 포스팅을 남긴다.



3. 참고

github issue - java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException

profile
꽃길만 걸어요 우리

0개의 댓글