شروع کنید

Integrating the Google Mobile Ads SDK into an app is the first step toward displaying ads and earning revenue. Once you've integrated the SDK, you can choose an ad format (such as native or rewarded video) and follow the steps to implement it.

قبل از شروع

برای آماده سازی اپلیکیشن خود، مراحل زیر را انجام دهید.

پیش نیازهای اپلیکیشن

  • مطمئن شوید که فایل ساخت برنامه شما از مقادیر زیر استفاده می کند:

    • حداقل نسخه SDK 24 یا بالاتر
    • نسخه SDK 35 یا بالاتر را کامپایل کنید
  • برای برنامه های Kotlin، از حداقل نسخه Kotlin 1.9 استفاده کنید.

برنامه خود را پیکربندی کنید

  1. در فایل تنظیمات Gradle خود، مخزن Maven Google و مخزن مرکزی Maven را قرار دهید:

    کاتلین

    pluginManagement {
      repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
      }
    }
    
    dependencyResolutionManagement {
      repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
      repositories {
        google()
        mavenCentral()
      }
    }
    
    rootProject.name = "My Application"
    include(":app")

    شیار

    pluginManagement {
      repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
      }
    }
    
    dependencyResolutionManagement {
      repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
      repositories {
        google()
        mavenCentral()
      }
    }
    
    rootProject.name = "My Application"
    include ':app'
  2. Add the dependencies for the Google Mobile Ads SDK to your app-level build file:

    کاتلین

    dependencies {
      implementation("com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk:0.17.0-alpha02")
    }

    شیار

    dependencies {
      implementation 'com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk:0.17.0-alpha02'
    }
  3. اکنون روی همگام سازی کلیک کنید. برای جزئیات در مورد همگام‌سازی، به همگام‌سازی پروژه‌ها با فایل‌های Gradle مراجعه کنید.

Google Mobile Ads SDK را راه‌اندازی کنید

با MobileAds.initialize() تماس بگیرید تا Google Mobile Ads SDK را مقداردهی کنید. This must be called on a background thread, failure to do so may cause an "Application Not Responding" (ANR) error.

کاتلین

import com.google.android.libraries.ads.mobile.sdk.MobileAds
import com.google.android.libraries.ads.mobile.sdk.initialization.InitializationConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      // Initialize the Google Mobile Ads SDK on a background thread.
      MobileAds.initialize(
        this@MainActivity,
        // Sample Ad Manager app ID: ca-app-pub-3940256099942544~3347511713
        InitializationConfig.Builder("SAMPLE_APP_ID").build()
      ) {
        // Adapter initialization is complete.
      }
      // Other methods on MobileAds can now be called.
    }
  }
}

جاوا

import com.google.android.libraries.ads.mobile.sdk.MobileAds;
import com.google.android.libraries.ads.mobile.sdk.initialization.InitializationConfig;

public class MainActivity extends AppCompatActivity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(
            () -> {
              // Initialize the Google Mobile Ads SDK on a background thread.
              MobileAds.initialize(
                  this,
                  // Sample Ad Manager app ID: ca-app-pub-3940256099942544~3347511713
                  new InitializationConfig.Builder("SAMPLE_APP_ID")
                      .build(),
                  initializationStatus -> {
                    // Adapter initialization is complete.
                  });
              // Other methods on MobileAds can now be called.
            })
        .start();
  }
}

این روش SDK را مقداردهی اولیه می‌کند و یک شنونده تکمیل را پس از تکمیل مقداردهی اولیه SDK و آداپتور Google Mobile Ads یا پس از 30 ثانیه بازخوانی می‌کند. این کار باید فقط یک بار انجام شود، ایده آل در هنگام راه اندازی برنامه.

Ads may be preloaded by the Google Mobile Ads SDK or mediation partner SDKs upon initialization. اگر نیاز به کسب رضایت از کاربران در منطقه اقتصادی اروپا (EEA) دارید، هر گونه پرچم خاص درخواست را تنظیم کنید، مانند RequestConfiguration.TagForChildDirectedTreatment یا RequestConfiguration.TagForUnderAgeOfConsent ، یا در غیر این صورت قبل از بارگیری تبلیغات اقدامی انجام دهید، مطمئن شوید که این کار را قبل از مقداردهی اولیه SDK Mobile Google انجام دهید.

یک قالب تبلیغ را انتخاب کنید

Google Mobile Ads SDK اکنون وارد شده است و شما آماده اجرای یک تبلیغ هستید. Ad Manager قالب‌های مختلف تبلیغاتی را ارائه می‌دهد، بنابراین می‌توانید یکی را انتخاب کنید که با تجربه کاربری برنامه‌تان مطابقت دارد.

واحدهای تبلیغات بنری، تبلیغات مستطیلی را نشان می‌دهند که بخشی از طرح‌بندی برنامه را اشغال می‌کنند. They can refresh automatically after a set period of time. This means users view a new ad at regular intervals, even if they stay on the same screen in your app. They're also the simplest ad format to implement.

پیاده سازی تبلیغات بنری

بینابینی

Interstitial ad units show full-page ads in your app. Place them at natural breaks and transitions in your app's interface, such as after level completion in a gaming app.

اجرای تبلیغات بینابینی

پاداش داده شد

Rewarded ad units enable users to play games, take surveys, or watch videos to earn in-app rewards, such as coins, extra lives, or points. You can set different rewards for different ad units, and specify the reward values and items the user received.

اجرای تبلیغات با پاداش

بینابینی با پاداش

بینابینی پاداش‌دار نوع جدیدی از قالب‌های تبلیغاتی با انگیزه است که به شما امکان می‌دهد برای تبلیغاتی که به‌طور خودکار در طول انتقال طبیعی برنامه ظاهر می‌شوند، پاداش‌هایی مانند سکه یا زندگی اضافی ارائه دهید.

Unlike rewarded ads, users aren't required to opt in to view a rewarded interstitial.

به جای اعلان انتخاب در تبلیغات دارای پاداش، مطالب بینابینی دارای پاداش نیاز به یک صفحه مقدماتی دارند که پاداش را اعلام می کند و به کاربران این فرصت را می دهد که در صورت تمایل از انجام این کار انصراف دهند.

تبلیغات بینابینی با پاداش را پیاده سازی کنید

برنامه باز شد

App open is an ad format that appears when users open or switch back to your app. آگهی روی صفحه بارگیری همپوشانی دارد.

پیاده سازی تبلیغات باز برنامه