๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Android

[Android/Kotlin] ์•ˆ๋“œ๋กœ์ด๋“œ ๊ตฌ๊ธ€ ์• ๋“œ๋ชน(admob) ๊ด‘๊ณ  ๋„ฃ๋Š” ๋ฒ•

by Jay Din 2024. 3. 2.
728x90
๋ฐ˜์‘ํ˜•

1. ์• ๋“œ๋ชน ํšŒ์›๊ฐ€์ž… / ์•ฑ ๋“ฑ๋ก

๊ตฌ๊ธ€ ์• ๋“œ๋ชน ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ฉด ์ฒซ ํŽ˜์ด์ง€์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์•ฑ ์ถ”๊ฐ€๋ฅผ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์•ฑ ์ถ”๊ฐ€ ํด๋ฆญ
์งˆ๋ฌธ์— ๋ฐ›๊ฒŒ ํด๋ฆญํ›„ ๊ณ„์†
์•ฑ์Šคํ† ์–ด์— ๋“ฑ๋กํ•œ ์•ฑ์ด ์žˆ์œผ๋ฉด ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ถ”

 

2. ์• ๋“œ๋ชน - ๊ด‘๊ณ  ๋‹จ์œ„(์ „๋ฉด, ๋ฐฐ๋„ˆ ๊ด‘๊ณ  ์ถ”๊ฐ€)

๊ด‘๊ณ ๋‹จ์œ„ - ์‹œ์ž‘ํ•˜
์„ ํƒ
๊ด‘๊ณ ๋‹จ์œ„ ์ƒ์„ฑ ์™„๋ฃŒ

 

3. Android ์ฝ”๋“œ ์ถ”๊ฐ€

build.gradle (Module: app) 

๊ตฌ๊ธ€ ์• ๋“œ๋ชน(google admob) dependencies๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

dependencies {
	// ์ถ”๊ฐ€ํ•˜๊ธฐ
    implementation 'com.google.android.gms:play-services-ads:22.6.0'
}

 

AndroidManifest.xml

AndroidManifest ํŒŒ์ผ์˜ application ํƒœ๊ทธ์•ˆ์— meta-data๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

value ๊ฐ’์— ํ…Œ์ŠคํŠธ ๊ด‘๊ณ  ID๋ฅผ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ตฌ๊ธ€ ์• ๋“œ๋ชน(google admob)์˜ ํ…Œ์ŠคํŠธ ์•„์ด๋””๋Š” ca-app-pub-3940256099942544~3347511713 ์ž…๋‹ˆ๋‹ค.

<?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>

 

์ž์‹ ์˜ com.google.android.gms.ads.APPLICATION_ID ๋Š” ์• ๋“œ๋ชน - ์•ฑ ์„ค์ •์—์„œ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์• ๋“œ๋ชน ๋ ˆ์ด์•„์›ƒ ๊ตฌ์„ฑ (activity_main.xml)

๋ ˆ์ด์•„์›ƒ์— ๊ด‘๊ณ  ๋ฐฐ๋„ˆ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

๋ณดํ†ต ํ•˜๋‹จ์— ๋งŽ์ด ๋ฐฐ์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‹จ์— ๋ฐฐ์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

adSize๋Š” ๋ฐฐ๋„ˆ, adUnitId์—๋Š” ํ…Œ์ŠคํŠธ ๊ด‘๊ณ ๋‹จ์œ„ ID๋ฅผ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค.

adUnitId๋Š” ์• ๋“œ๋ชน - ๊ด‘๊ณ ๋‹จ์œ„ ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<?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/6300978123">
    </com.google.android.gms.ads.AdView>


</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

MainActivity.kt ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

๋ฉ”์ธ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ onCreate๋ฉ”์†Œ๋“œ ์•ˆ์— MobileAds๋ฅผ Intiailize ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

๋นŒ๋”ํŒจํ„ด์„ ํ†ตํ•ด AdRequest ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , AdView์˜ loadAd๋ฅผ ํ˜ธ์ถœ ๋ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

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)
    }
}

 

๊ฒฐ๊ณผ

 

728x90
๋ฐ˜์‘ํ˜•