본문 바로가기
기리's Android 이야기

[Android/Kotlin] 안드로이드 애드몹(Admob) 배너광고 넣기(1)

by 꿈꾸는블로그왕 2022. 7. 25.

안녕하세요. 

 

오늘은 안드로이드 앱에 구글 애드몹 광고를 넣는 방법에 대해서 알아보도록 하겠습니다.

 

애플리케이션으로 수익을 창출하고 싶을때는

구글에서 제공하는 애드몹을  통해 광고를 게재 및 이를 통해 이익을 낼 수 있습니다.

 

광고 형식으로는 배너광고, 네이티브광고, 전면광고, 보상형광고가 있으며,

앱의 디자인 및 수익성 등을 고려하여 개발자가 전략적으로 선택 및 사용할 수 있습니다.

 

먼저 가장 간단한 배너 광고에 대해서 알아보도록 하겠습니다.

배너 광고는 말 그대로 배너(인터넷 홈페이지에 띠 모양으로 부착하는 광고)의 형태를 말하며,

이를 구현시 아래와 같은 광고를 달 수 있습니다.

 

실제앱에 광고를 달기위해서는 애드몹 홈페이지에서 앱 아이디와 광고단위 아이디 생성이 필요하지만

테스트 아이디로 진행 하도록 하겠습니다.

 

 

 

Step 1: 앱 구성

먼저 아래와 같이 빌드 dependency를 추가 및 sync를 합니다.

build.gradle (Module: app) 

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

	// 추가하기
    implementation 'com.google.android.gms:play-services-ads:21.0.0'
}

 

메니페스트 파일에 application 태그안에 meta-data를 아래와 같이 입력합니다.

value 값에 테스트 광고 ID를 입력했습니다.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest>
    <application
        ...
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>
    </application>

</manifest>

 

Step 2: 애드몹 레이아웃 구성하기

레이아웃에 광고 배너를 추가해줍니다.

보통 하단에 많이 배치하기 때문에 하단에 배치했습니다.

adSize는 배너, adUnitId에는 테스트 광고단위 ID를 입력했습니다.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:text="Hello World!!!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

	// 추가하기
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>


</androidx.constraintlayout.widget.ConstraintLayout>

 

Step 3: 메인 액티비티에 코드 작성하기

메인 액티비티의 onCreate메소드 안에 MobileAds를 Intiailize 하는 코드를 넣어줍니다.

빌더패턴을 통해 AdRequest 객체를 생성하고, AdView의 loadAd를 호출 및 파라미터로 전달합니다.

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        MobileAds.initialize(this)

        val adRequest = AdRequest.Builder().build()
        binding.adView.loadAd(adRequest)
    }
}

 

Step 4: 결과

목차