본문 바로가기

개발/Android

[안드로이드] 애드몹(adMob) 내 앱에 광고 달기 #2 - 소스 설정

1. res-values-string.xml 편집

이전에 발급받은 app id와 ad unit id를 기입해 준다.

test를 위한 ad unit id는 google에서 제공하는 것이므로 하단 코드블럭의 id를 그대로 쓴다.

<resources>
    <string name="app_name">FlashLight</string>
    <string name="admob_app_id">"발급 받은 app id 기입!!"</string>
    <string name="banner_ad_unit_id">"발급 받은 단위 ad id 기입!!!"</string>
    <string name="banner_ad_unit_id_for_test">ca-app-pub-3940256099942544/6300978111</string>
</resources>

 

광고 형식샘플 광고 단위 ID

배너 광고 ca-app-pub-3940256099942544/6300978111
전면 광고 ca-app-pub-3940256099942544/1033173712
전면 동영상 광고 ca-app-pub-3940256099942544/8691691433
보상형 동영상 광고 ca-app-pub-3940256099942544/5224354917
네이티브 광고 고급형 ca-app-pub-3940256099942544/2247696110
네이티브 동영상 광고 고급형 ca-app-pub-3940256099942544/1044960115

 

2. activity XML 편집

 

gradle 창에서 adview를 추가해 준 뒤 XML 설정을 하기와 같이 변경해 준다.

추가되는 부분은 adSize와 adUnitId.

하단 adUnitId에 banner_ad_uhnit_id_for_test 는 테스트할때만 쓰고 앱을 올리기 전에 꼭 banner_ad_unit_id로 변경해 주도록 한다!!

 

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="406dp"
        android:layout_height="51dp"
        app:adSize="BANNER"
        app:adUnitId="@string/banner_ad_unit_id_for_test"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

 

3. AndroidManifest.xml 편집

 

하기 내용을 추가해 준다.

<!-- 인터넷을 사용 권한 획득 -->
<uses-permission android:name="android.permission.INTERNET"/>

 

<!-- app id 입력-->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_app_id"/>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gozz.flashlight">

    <!-- 인터넷을 사용 권한 획득  -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    <!--  app id 입력-->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/admob_app_id"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MorseActivity" />
    </application>

</manifest>

 

4. build.gradle (:app) 편집

dependency에 하기 문구를 추가해준다. 

implementation 'com.google.android.gms:play-services-ads:19.2.0'

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.gms:play-services-ads:19.2.0'
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

    implementation 'androidx.appcompat:appcompat:1.0.0'
    
}

 

5. 소스 편집

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
             MobileAds.initialize(this, getString(R.string.admob_app_id));
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        
        // 광고출력 여부 리스너
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
                // 광고가 출력 성공
                Log.d("TAG", "onAdLoaded");
            }
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
                // 광고 출력 실패
                Log.d("TAG", "onAdFailedToLoad : " + errorCode);
            }
            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }
            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
            }
            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }
            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
        }

 

6. 결과

하기와 같이 테스트 광고가 뜨는 것을 확인 할 수 있다.

앱스토어에 올리기 전에 꼭 adUnitId를  banner_ad_unit_id로 변경해 주도록 한다!!