要接收版本更新,请订阅 AppLovin-MAX-SDK-Android GitHub 库。
解压缩下载的文件,然后将 aar 文件拖拽至项目中的 libs 文件夹。
(如果您的项目没有 libs 文件夹,您可以在 app 文件夹中创建。)
将下列内容添加至您的 build.gradle 文件:
repositories {
google()
mavenCentral()
flatDir {
dirs 'libs'
}
⋮
}
dependencies {
implementation 'com.applovin:applovin-sdk:«x.y.z»@aar'
⋮
}
repositories {
google()
mavenCentral()
flatDir {
dirs("libs")
}
⋮
}
dependencies {
implementation("com.applovin:applovin-sdk:«x.y.z»@aar")
⋮
}
将下列行添加至您的 AndroidManifest.xml。
这需要放置在 application 标签中:
<meta-data android:name="applovin.sdk.key" android:value="«your-SDK-key»"/>
您可以在 AppLovin dashboard 的 Account > General > Keys 部分找到您的 SDK key。
要启用 MAX Ad Review 服务,请将下列内容添加至您的 build.gradle 文件:
build.gradle 文件的补充内容buildscript {
repositories {
maven { url 'https://artifacts.applovin.com/android' }
}
dependencies {
classpath "com.applovin.quality:AppLovinQualityServiceGradlePlugin:+"
}
}
buildscript {
repositories {
maven { url = uri("https://artifacts.applovin.com/android") }
}
dependencies {
classpath ("com.applovin.quality:AppLovinQualityServiceGradlePlugin:+")
}
}
build.gradle 文件的补充内容apply plugin: 'applovin-quality-service'
applovin {
apiKey "«your-ad-review-key»"
}
plugins {
id("applovin-quality-service")
}
applovin {
apiKey = "«your-ad-review-key»"
}
您可以在 AppLovin dashboard 的 Account > General > Keys 部分找到您的 Ad Review Key。
在初始化 SDK 之前,请为 SDK 创建一个初始化配置对象。
该对象允许您配置 SDK 初始化时使用的属性。
这些初始化属性是不可变的,但 AppLovinSdkSettings 除外,它包含可在应用生命周期内更改的可变属性。
// Create the initialization configuration
AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder( "«SDK-key»" )
.setMediationProvider( AppLovinMediationProvider.MAX )
// Perform any additional configuration/setting changes
.build();
// Create the initialization configuration
val initConfig = AppLovinSdkInitializationConfiguration.builder("«SDK-key»")
.setMediationProvider(AppLovinMediationProvider.MAX)
// Perform any additional configuration/setting changes
.build()
您可以在 AppLovin dashboard 的 Account > General > Keys 部分找到您的 SDK key。
请尽早使用初始化配置对象来初始化 AppLovin SDK(例如,在启动 activity 或 Application 类的 onCreate() 中)。
这可以最大限度地延长 SDK 缓存 mediated networks 广告的时间,从而带来更好的用户体验。
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
// Create the initialization configuration
AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder( "«SDK-key»" )
.setMediationProvider( AppLovinMediationProvider.MAX )
.build();
// Initialize the SDK with the configuration
AppLovinSdk.getInstance( this ).initialize( initConfig, new AppLovinSdk.SdkInitializationListener()
{
@Override
public void onSdkInitialized(final AppLovinSdkConfiguration sdkConfig)
{
// Start loading ads
}
} );
}
}
class MainActivity : Activity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
// Create the initialization configuration
val initConfig = AppLovinSdkInitializationConfiguration.builder("«SDK-key»")
.setMediationProvider(AppLovinMediationProvider.MAX)
.build()
// Initialize the SDK with the configuration
AppLovinSdk.getInstance(this).initialize(initConfig) { sdkConfig ->
// Start loading ads
}
}
}
以下是集成示例。
// Create the initialization configuration
AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder( "«SDK-key»" )
.setMediationProvider( AppLovinMediationProvider.MAX )
.setSegmentCollection( MaxSegmentCollection.builder()
.addSegment( new MaxSegment( 849, Arrays.asList( 1, 3 ) ) )
.build() )
.build();
// Configure the SDK settings if needed before or after SDK initialization.
val settings = AppLovinSdk.getInstance( this ).getSettings();
settings.setUserIdentifier( "«user-ID»" );
settings.setExtraParameter( "uid2_token", "«token-value»" );
settings.getTermsAndPrivacyPolicyFlowSettings().setEnabled( true );
settings.getTermsAndPrivacyPolicyFlowSettings().setPrivacyPolicyUri( Uri.parse( "«https://your-company-name.com/privacy-policy»" ) );
settings.getTermsAndPrivacyPolicyFlowSettings().setTermsOfServiceUri( Uri.parse( "«https://your-company-name.com/terms-of-service»" ) );
// Initialize the SDK with the configuration
AppLovinSdk.getInstance( this ).initialize( initConfig, new AppLovinSdk.SdkInitializationListener()
{
@Override
public void onSdkInitialized(final AppLovinSdkConfiguration sdkConfig)
{
// Start loading ads
}
} );
// Create the initialization configuration
val initConfig = AppLovinSdkInitializationConfiguration.builder("«SDK-key»")
.setMediationProvider(AppLovinMediationProvider.MAX)
.setSegmentCollection(MaxSegmentCollection.builder()
.addSegment(MaxSegment(849, listOf(1, 3)))
.build()
)
.build()
// Configure the SDK settings if needed before or after SDK initialization.
val settings = AppLovinSdk.getInstance(this).settings
settings.userIdentifier = "«user-ID»"
settings.setExtraParameter("uid2_token", "«token-value»")
settings.termsAndPrivacyPolicyFlowSettings.apply {
isEnabled = true
privacyPolicyUri = Uri.parse("«https://your-company-name.com/privacy-policy»")
termsOfServiceUri = Uri.parse("«https://your-company-name.com/terms-of-service»")
}
// Initialize the SDK with the configuration
AppLovinSdk.getInstance(this).initialize(initConfig) { sdkConfig ->
// Start loading ads
}
要加载 interstitial 广告,请使用您的广告单元实例化一个 MaxInterstitialAd 对象并调用 loadAd()。
实现 MaxAdListener,以便在广告准备就绪以及发生其他广告相关事件时收到通知。
public class ExampleActivity extends Activity
implements MaxAdListener
{
private MaxInterstitialAd interstitialAd;
private int retryAttempt;
void createInterstitialAd()
{
interstitialAd = new MaxInterstitialAd( "«ad-unit-ID»" );
interstitialAd.setListener( this );
// Load the first ad
interstitialAd.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd)
{
// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0;
}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error)
{
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++;
long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );
new Handler().postDelayed( new Runnable()
{
@Override
public void run()
{
interstitialAd.loadAd();
}
}, delayMillis );
}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error)
{
// Interstitial ad failed to display. AppLovin recommends that you load the next ad
interstitialAd.loadAd();
}
@Override
public void onAdDisplayed(final MaxAd maxAd) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdHidden(final MaxAd maxAd)
{
// Interstitial ad is hidden. Pre-load the next ad
interstitialAd.loadAd();
}
}
class ExampleActivity : Activity(), MaxAdListener
{
private lateinit var interstitialAd: MaxInterstitialAd
private var retryAttempt = 0.0
fun createInterstitialAd()
{
interstitialAd = MaxInterstitialAd( "«ad-unit-ID»", this )
interstitialAd.setListener( this )
// Load the first ad
interstitialAd.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd)
{
// Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0.0
}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?)
{
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++
val delayMillis = TimeUnit.SECONDS.toMillis( Math.pow( 2.0, Math.min( 6.0, retryAttempt ) ).toLong() )
Handler().postDelayed( { interstitialAd.loadAd() }, delayMillis )
}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?)
{
// Interstitial ad failed to display. AppLovin recommends that you load the next ad
interstitialAd.loadAd()
}
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd)
{
// Interstitial ad is hidden. Pre-load the next ad
interstitialAd.loadAd()
}
}
要展示 interstitial 广告,请在上方创建的 MaxInterstitialAd 对象上调用 showAd()。
if ( interstitialAd.isReady() )
{
interstitialAd.showAd();
}
if ( interstitialAd.isReady )
{
interstitialAd.showAd()
}
要加载 rewarded 广告,请使用您的 rewarded 广告单元获取一个 MaxRewardedAd 对象并对其调用 loadAd()。
实现 MaxRewardedAdListener,以便在广告准备就绪以及发生其他广告相关事件时收到通知。
public class ExampleActivity extends Activity
implements MaxRewardedAdListener
{
private MaxRewardedAd rewardedAd;
private int retryAttempt;
void createRewardedAd()
{
rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»" );
rewardedAd.setListener( this );
rewardedAd.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd)
{
// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0;
}
@Override
public void onAdLoadFailed(final String adUnitId, final int errorCode)
{
// Rewarded ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++;
long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );
new Handler().postDelayed( new Runnable()
{
@Override
public void run()
{
rewardedAd.loadAd();
}
}, delayMillis );
}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad
rewardedAd.loadAd();
}
@Override
public void onAdDisplayed(final MaxAd maxAd) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdHidden(final MaxAd maxAd)
{
// rewarded ad is hidden. Pre-load the next ad
rewardedAd.loadAd();
}
@Override
public void onUserRewarded(final MaxAd maxAd, final MaxReward maxReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}
class ExampleActivity : Activity(), MaxRewardedAdListener
{
private lateinit var rewardedAd: MaxRewardedAd
private var retryAttempt = 0.0
fun createRewardedAd()
{
rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»" )
rewardedAd.setListener( this )
rewardedAd.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd)
{
// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0.0
}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?)
{
// Rewarded ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++
val delayMillis = TimeUnit.SECONDS.toMillis( Math.pow( 2.0, Math.min( 6.0, retryAttempt ) ).toLong() )
Handler().postDelayed( { rewardedAd.loadAd() }, delayMillis )
}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad
rewardedAd.loadAd()
}
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd)
{
// rewarded ad is hidden. Pre-load the next ad
rewardedAd.loadAd()
}
override fun onUserRewarded(maxAd: MaxAd, maxReward: MaxReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}
要展示 rewarded 广告,请在上方创建的 MaxRewardedAd 对象上调用 showAd()。
if ( rewardedAd.isReady() )
{
rewardedAd.showAd();
}
if ( rewardedAd.isReady() )
{
rewardedAd.showAd();
}
要了解如何向您的货币服务器接收回调,请参阅 MAX S2S rewarded callback API 指南,并在您的 Edit Ad Unit 页面中更新 S2S Rewarded Callback URL。
要加载 banner 广告或 MREC,请使用您的广告单元创建一个 MaxAdView 对象并调用 loadAd()。
要展示广告,请将 MaxAdView 对象添加为视图层级的子视图。
实现 MaxAdViewAdListener,以便在广告准备就绪以及发生其他广告相关事件时收到通知。
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
void createBannerAd()
{
adView = new MaxAdView( "«ad-unit-ID»" );
adView.setListener( this );
// Stretch to the width of the screen for banners to be fully functional
int width = ViewGroup.LayoutParams.MATCH_PARENT;
// Banner height on phones and tablets is 50 and 90, respectively
int heightPx = getResources().getDimensionPixelSize( R.dimen.banner_height );
adView.setLayoutParams( new FrameLayout.LayoutParams( width, heightPx ) );
// Set background or background color for banners to be fully functional
adView.setBackgroundColor( ... );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
// Load the ad
adView.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd) {}
@Override
public void onAdLoadFailed(final String adUnitId, final int errorCode) {}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdExpanded(final MaxAd maxAd) {}
@Override
public void onAdCollapsed(final MaxAd maxAd) {}
@Override
public void onAdDisplayed(final MaxAd maxAd) { /* use this for impression tracking */ }
@Override
public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
class ExampleActivity : Activity(), MaxAdViewAdListener
{
private var adView: MaxAdView? = null
fun createBannerAd()
{
adView = MaxAdView("«ad-unit-ID»")
adView?.setListener(this)
// Stretch to the width of the screen for banners to be fully functional
val width = ViewGroup.LayoutParams.MATCH_PARENT
// Banner height on phones and tablets is 50 and 90, respectively
val heightPx = resources.getDimensionPixelSize(R.dimen.banner_height)
adView?.layoutParams = FrameLayout.LayoutParams(width, heightPx)
// Set background or background color for banners to be fully functional
adView?.setBackgroundColor(...)
val rootView = findViewById<ViewGroup>(android.R.id.content)
rootView.addView(adView)
// Load the ad
adView?.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* use this for impression tracking */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
void createMrecAd
{
adView = new MaxAdView( "«ad-unit-ID»", MaxAdFormat.MREC );
adView.setListener( this );
// MREC width and height are 300 and 250 respectively, on phones and tablets
int widthPx = AppLovinSdkUtils.dpToPx( this, 300 );
int heightPx = AppLovinSdkUtils.dpToPx( this, 250 );
adView.setLayoutParams( new FrameLayout.LayoutParams( widthPx, heightPx ) );
// Set background or background color for MRECs to be fully functional
adView.setBackgroundColor( ... );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
// Load the ad
adView.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd) {}
@Override
public void onAdLoadFailed(final String adUnitId, final int errorCode) {}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdExpanded(final MaxAd maxAd) {}
@Override
public void onAdCollapsed(final MaxAd maxAd) {}
@Override
public void onAdDisplayed(final MaxAd maxAd) { /* use this for impression tracking */ }
@Override
public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
class ExampleActivity : Activity(), MaxAdViewAdListener
{
private var adView: MaxAdView? = null
fun createMrecAd
{
adView = MaxAdView("«ad-unit-ID»", MaxAdFormat.MREC)
adView?.setListener(this)
// MREC width and height are 300 and 250 respectively, on phones and tablets
val widthPx = AppLovinSdkUtils.dpToPx(this, 300)
val heightPx = AppLovinSdkUtils.dpToPx(this, 250)
adView?.layoutParams = FrameLayout.LayoutParams(widthPx, heightPx)
// Set background or background color for MRECs to be fully functional
adView?.setBackgroundColor(...)
val rootView = findViewById<ViewGroup>(android.R.id.content)
rootView.addView(adView)
// Load the ad
adView?.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* use this for impression tracking */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
您也可以将 MAX banner 或 MREC 添加到您的视图布局 XML 中。
通过设置背景或背景颜色 (android:background) 来确保您的广告功能完整。
对于 banner,请将宽度 (android:layout_width) 拉伸至屏幕宽度。
对于 MREC,请相应地设置 android:adFormat:
<com.applovin.mediation.ads.MaxAdView
xmlns:maxads="http://schemas.applovin.com/android/1.0"
maxads:adUnitId="«ad-unit-ID»"
android:background="@color/banner_background_color"
android:layout_width="match_parent"
android:layout_height="@dimen/banner_height" />
<com.applovin.mediation.ads.MaxAdView
xmlns:maxads="http://schemas.applovin.com/android/1.0"
maxads:adUnitId="«ad-unit-ID»"
maxads:adFormat="MREC"
android:background="@color/mrec_background_color"
android:layout_width="300dp"
android:layout_height="250dp" />
在 res/values/attrs.xml 中声明 50 dp 的基础 banner 高度:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>
在 res/values-sw600dp/attrs.xml 中声明 90 dp 的平板电脑 banner 高度:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">90dp</dimen>
</resources>
要隐藏 banner 或 MREC 广告,请进行以下调用:
adView.setVisibility( View.GONE );
adView.stopAutoRefresh();
adView.visibility = View.GONE
adView.stopAutoRefresh()
要展示 banner 或 MREC 广告,请进行以下调用:
adView.setVisibility( View.VISIBLE );
adView.startAutoRefresh();
adView.visibility = View.VISIBLE
adView.startAutoRefresh()
选择要集成的 ad networks。 然后按照具体说明操作。
将 AndroidX 库集成到您的项目中。 请参阅 Migrate to AndroidX 指南,了解有关如何迁移项目的更多信息。
使用 Google AdSense、AdManager 或 AdMob 的开发者和发行商必须使用经过 Google 认证的许可管理平台 (CMP)。 当您向欧洲经济区 (EEA) 或英国的用户投放广告时,该 CMP 必须与 IAB 的 Transparency and Consent Framework 集成。 请参阅 Privacy: “TCF v2 consent” 了解更多信息。
在应用的 AndroidManifest.xml 中,在 <application> 标签内添加一个 <meta-data> 标签。
以下示例展示了此标签的正确属性。
请用您的 Google bidding 和 Google AdMob / Google Ad Manager app ID 替换 «your-admob-app-id»。
<?xml version="1.0" encoding="utf-8"?>
<manifest … >
<application … >
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="«your-admob-app-id»"/>
⋮
</application>
</manifest>
Google AdMob 需要 Android Gradle plugin 版本 4.2.0 或更高版本,以及 Gradle 版本 6.7.1 或更高版本。 如果您看到以下错误,请更新您的 Android Gradle plugin 和 Gradle 版本:
AAPT: error: unexpected element <property> found in <manifest><application>.
compileSdkVersionGoogle Mobile Ads SDK 23.1.0 及更新版本要求 compileSdkVersion 至少为 34。
在应用的 build.gradle 中将 compileSdkVersion 设置为 34 或更高。
请参阅 Google Mobile Ads SDK Release Notes 了解最新的 compileSdkVersion 要求。
Amazon Publisher Services SDK 需要在 MAX SDK 之外进行初始化:
// Amazon requires an 'Activity' instance
AdRegistration.getInstance( "AMAZON_APP_ID", this );
AdRegistration.setMRAIDSupportedVersions( new String[] { "1.0", "2.0", "3.0" } );
AdRegistration.setMRAIDPolicy( MRAIDPolicy.CUSTOM );
要在 MAX 中集成 Amazon 广告,您必须先加载 Amazon 广告。
在加载 MAX 广告之前,将 DTBAdResponse 或 AdError 传递至 MaxAdView 实例。
您可以通过调用 MaxAdView#setLocalExtraParameter() 来完成此操作。
对于自动刷新的 banner 广告,您只需要加载广告一次。
class ExampleActivity
extends Activity
{
⋮
private void loadAd()
{
String amazonAdSlotId;
MaxAdFormat adFormat;
if ( AppLovinSdkUtils.isTablet( getApplicationContext() ) )
{
amazonAdSlotId = "«Amazon-leader-slot-ID»";
adFormat = MaxAdFormat.LEADER;
}
else
{
amazonAdSlotId = "«Amazon-banner-slot-ID»";
adFormat = MaxAdFormat.BANNER;
}
// Raw size will be 320x50 for BANNERs on phones, and 728x90 for LEADERs on tablets
AppLovinSdkUtils.Size rawSize = adFormat.getSize();
DTBAdSize size = new DTBAdSize( rawSize.getWidth(), rawSize.getHeight(), amazonAdSlotId );
DTBAdRequest adLoader = new DTBAdRequest( getApplicationContext(), new DTBAdNetworkInfo( ApsAdNetwork.MAX ) );
adLoader.setSizes( size );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
adView.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_error", adError );
adView.loadAd();
}
} );
}
}
class ExampleActivity : Activity()
{
private val adView: MaxAdView? = null
private fun loadAd()
{
val amazonAdSlotId: String
val adFormat: MaxAdFormat
if (AppLovinSdkUtils.isTablet(applicationContext))
{
amazonAdSlotId = "«Amazon-leader-slot-ID»"
adFormat = MaxAdFormat.LEADER
}
else
{
amazonAdSlotId = "«Amazon-banner-slot-ID»"
adFormat = MaxAdFormat.BANNER
}
// Raw size will be 320x50 for BANNERs on phones, and 728x90 for LEADERs on tablets
val rawSize = adFormat.size
val size = DTBAdSize(rawSize.width, rawSize.height, amazonAdSlotId)
val adLoader = DTBAdRequest( applicationContext, DTBAdNetworkInfo( ApsAdNetwork.MAX ) )
adLoader.setSizes(size)
adLoader.loadAd(object : DTBAdCallback
{
override fun onSuccess(dtbAdResponse: DTBAdResponse)
{
// 'adView' is your instance of MaxAdView
adView?.setLocalExtraParameter("amazon_ad_response", dtbAdResponse)
adView?.loadAd()
}
override fun onFailure(adError: AdError)
{
// 'adView' is your instance of MaxAdView
adView?.setLocalExtraParameter("amazon_ad_error", adError)
adView?.loadAd()
}
})
}
}
class ExampleActivity
extends Activity
{
⋮
private void loadAd()
{
String amazonAdSlotId;
DTBAdRequest adLoader = new DTBAdRequest( getApplicationContext(), new DTBAdNetworkInfo( ApsAdNetwork.MAX ) );
adLoader.setSizes( new DTBAdSize( 300, 250, amazonAdSlotId ) );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
adView.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'adView' is your instance of MaxAdView
adView.setLocalExtraParameter( "amazon_ad_error", adError );
adView.loadAd();
}
} );
}
}
class ExampleActivity : Activity()
{
private val adView: MaxAdView? = null
private fun loadAd()
{
val amazonAdSlotId: String
val adLoader = DTBAdRequest( applicationContext, DTBAdNetworkInfo( ApsAdNetwork.MAX ) )
adLoader.setSizes(DTBAdSize(300, 250, amazonAdSlotId))
adLoader.loadAd(object : DTBAdCallback
{
override fun onSuccess(dtbAdResponse: DTBAdResponse)
{
// 'adView' is your instance of MaxAdView
adView!!.setLocalExtraParameter("amazon_ad_response", dtbAdResponse)
adView.loadAd()
}
override fun onFailure(adError: AdError)
{
// 'adView' is your instance of MaxAdView
adView!!.setLocalExtraParameter("amazon_ad_error", adError)
adView.loadAd()
}
})
}
}
要在 MAX 中集成 Amazon interstitial 广告,您必须先加载 Amazon 广告。
在加载 MAX 广告之前,将 DTBAdResponse 或 AdError 传递至 MaxInterstitialAd 实例。
您可以通过调用 MaxInterstitialAd#setLocalExtraParameter() 来完成此操作。
在每个 session 中,您只能加载一次 Amazon DTBAdResponse 或 DTBAdErrorInfo 并将其传递至 MaxInterstitialAd 实例。
class ExampleActivity
extends Activity
{
private static MaxInterstitialAd interstitialAd; // static to ensure only one instance exists
private static boolean isFirstLoad = true;
private void loadAd()
{
if ( isFirstLoad )
{
isFirstLoad = false;
if ( interstitialAd == null )
{
interstitialAd = new MaxInterstitialAd( "«MAX-inter-ad-unit-ID»" );
}
DTBAdRequest adLoader = new DTBAdRequest( getApplicationContext(), new DTBAdNetworkInfo( ApsAdNetwork.MAX ) );
adLoader.setSizes( new DTBAdSize.DTBInterstitialAdSize( "«Amazon-inter-slot-ID»" ) );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
interstitialAd.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd.setLocalExtraParameter( "amazon_ad_error", adError );
interstitialAd.loadAd();
}
} );
}
else
{
interstitialAd.loadAd();
}
}
}
class ExampleActivity : Activity()
{
private var interstitialAd: MaxInterstitialAd? = null // static to ensure only one instance exists
private var isFirstLoad = true
private fun loadAd()
{
if (isFirstLoad)
{
isFirstLoad = false
if (interstitialAd == null)
{
interstitialAd = MaxInterstitialAd("«MAX-inter-ad-unit-ID»", this)
}
val adLoader = DTBAdRequest( applicationContext, DTBAdNetworkInfo( ApsAdNetwork.MAX ) )
adLoader.setSizes(DTBAdSize.DTBInterstitialAdSize("«Amazon-inter-slot-ID»"))
adLoader.loadAd(object : DTBAdCallback
{
override fun onSuccess(dtbAdResponse: DTBAdResponse)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd!!.setLocalExtraParameter("amazon_ad_response", dtbAdResponse)
interstitialAd!!.loadAd()
}
override fun onFailure(adError: AdError)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd!!.setLocalExtraParameter("amazon_ad_error", adError)
interstitialAd!!.loadAd()
}
})
}
else
{
interstitialAd!!.loadAd()
}
}
}
要在 MAX 中集成 Amazon 视频 interstitial 广告,您必须先加载 Amazon 广告。
在加载 MAX 广告之前,将 DTBAdResponse 或 AdError 传递至 MaxInterstitialAd 实例。
您可以通过调用 MaxInterstitialAd#setLocalExtraParameter() 来完成此操作。
在每个 session 中,您只能加载一次 DTBAdResponse 或 DTBAdErrorInfo 并将其传递至 MaxInterstitialAd 实例。
class ExampleActivity
extends Activity
{
private static MaxInterstitialAd interstitialAd; // static to ensure only one instance exists
private static boolean isFirstLoad = true;
private void loadAd()
{
if ( isFirstLoad )
{
isFirstLoad = false;
if ( interstitialAd == null )
{
interstitialAd = new MaxInterstitialAd( "«MAX-inter-ad-unit-ID»" );
}
DTBAdRequest adLoader = new DTBAdRequest( getApplicationContext(), new DTBAdNetworkInfo( ApsAdNetwork.MAX ) );
// Switch video player width and height values(320, 480) depending on device orientation
adLoader.setSizes( new DTBAdSize.DTBVideo(320, 480, "«Amazon-video-inter-slot-ID»") );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
interstitialAd.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd.setLocalExtraParameter( "amazon_ad_error", adError );
interstitialAd.loadAd();
}
} );
}
else
{
interstitialAd.loadAd();
}
}
}
class ExampleActivity : Activity()
{
private var interstitialAd: MaxInterstitialAd? = null // static to ensure only one instance exists
private var isFirstLoad = true
private fun loadAd()
{
if (isFirstLoad)
{
isFirstLoad = false
if (interstitialAd == null)
{
interstitialAd = MaxInterstitialAd("«MAX-inter-ad-unit-iD»", this)
}
val adLoader = DTBAdRequest( applicationContext, DTBAdNetworkInfo( ApsAdNetwork.MAX ) )
// Switch video player width and height values(320, 480) depending on device orientation
adLoader.setSizes(DTBAdSize.DTBVideo(320, 480, "«Amazon-video-inter-slot-ID»"))
adLoader.loadAd(object : DTBAdCallback
{
override fun onSuccess(dtbAdResponse: DTBAdResponse)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd!!.setLocalExtraParameter("amazon_ad_response", dtbAdResponse)
interstitialAd!!.loadAd()
}
override fun onFailure(adError: AdError)
{
// 'interstitialAd' is your instance of MaxInterstitialAd
interstitialAd!!.setLocalExtraParameter("amazon_ad_error", adError)
interstitialAd!!.loadAd()
}
})
}
else
{
interstitialAd!!.loadAd()
}
}
}
要在 MAX 中集成 Amazon rewarded 视频,请先加载 Amazon 广告。
在加载 MAX 广告之前,将 DTBAdResponse 或 AdError 传递至 MaxRewardedAd 实例。
您可以通过调用 MaxRewardedAd#setLocalExtraParameter() 来完成此操作。
在每个 session 中,您只能加载一次 DTBAdResponse 或 AdError 并将其传递至 MaxRewardedAd 实例。
class ExampleActivity
extends Activity
{
private static MaxRewardedAd rewardedAd; // static to ensure only one instance exists
private static boolean isFirstLoad = true;
private void loadAd()
{
if ( isFirstLoad )
{
isFirstLoad = false;
if ( rewardedAd == null )
{
rewardedAd = MaxRewardedAd.getInstance( "«MAX-rewarded-ad-unit-ID»" );
}
DTBAdRequest adLoader = new DTBAdRequest( getApplicationContext(), new DTBAdNetworkInfo( ApsAdNetwork.MAX ) );
// Switch video player width and height values(320, 480) depending on device orientation
adLoader.setSizes( new DTBAdSize.DTBVideo( 320, 480, "«Amazon-video-rewarded-slot-ID»" ) );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'rewardedAd' is your instance of MaxRewardedAd
rewardedAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
rewardedAd.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'rewardedAd' is your instance of MaxRewardedAd
rewardedAd.setLocalExtraParameter( "amazon_ad_error", adError );
rewardedAd.loadAd();
}
} );
}
else
{
rewardedAd.loadAd();
}
}
}
class ExampleActivity
extends Activity
{
private static MaxRewardedAd rewardedAd; // static to ensure only one instance exists
private static boolean isFirstLoad = true;
private void loadAd()
{
if ( isFirstLoad )
{
isFirstLoad = false;
if ( rewardedAd == null )
{
rewardedAd = MaxRewardedAd.getInstance( "«MAX-rewarded-ad-unit-ID»" );
}
DTBAdRequest adLoader = new DTBAdRequest( getApplicationContext(), new DTBAdNetworkInfo( ApsAdNetwork.MAX ) );
// Switch video player width and height values(320, 480) depending on device orientation
adLoader.setSizes( new DTBAdSize.DTBVideo( 320, 480, "«Amazon-video-rewarded-slot-ID»" ) );
adLoader.loadAd( new DTBAdCallback()
{
@Override
public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
{
// 'rewardedAd' is your instance of MaxRewardedAd
rewardedAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
rewardedAd.loadAd();
}
@Override
public void onFailure(@NonNull final AdError adError)
{
// 'rewardedAd' is your instance of MaxRewardedAd
rewardedAd.setLocalExtraParameter( "amazon_ad_error", adError );
rewardedAd.loadAd();
}
} );
}
else
{
rewardedAd.loadAd();
}
}
}
AppLovin 建议您为 Amazon SDK 启用测试模式。 这允许您接收测试广告。 通过进行以下调用来启用测试模式:
AdRegistration.enableTesting( true );
AdRegistration.enableLogging( true );
您可以过滤您的 waterfall,使其仅包含 Amazon 广告。 为此,请导航至 Mediation Debugger 中的 Select Live Network,然后选择 Amazon network。
如果您不想启用 Limited Data Use (LDU) 模式,请向 SetDataProcessingOptions() 传递一个空字符串数组:
import com.facebook.ads.AdSettings;
⋮
AdSettings.setDataProcessingOptions( new String[] {} );
⋮
// Initialize MAX SDK
import com.facebook.ads.AdSettings
⋮
AdSettings.setDataProcessingOptions( arrayOf<String>() )
⋮
// Initialize MAX SDK
要为用户启用 LDU 并指定用户地理位置,请以如下形式调用 SetDataProcessingOptions():
import com.facebook.ads.AdSettings;
⋮
AdSettings.setDataProcessingOptions( new String[] {"LDU"}, «country», «state» );
⋮
// Initialize MAX SDK
import com.facebook.ads.AdSettings
⋮
AdSettings.setDataProcessingOptions( arrayOf("LDU"), «country», «state» )
⋮
// Initialize MAX SDK
如果您使用 Google UMP 作为您的 CMP,您可以判断用户是否已向 Meta 授予许可。 为此,请使用如下 code:
Boolean hasMetaConsent = AppLovinPrivacySettings.getAdditionalConsentStatus( 89 );
if ( hasMetaConsent != null )
{
// Set Meta Data Processing Options accordingly.
}
else
{
// AC String is not available on disk. Please check for consent status after the user completes the CMP flow.
}
val hasMetaConsent = AppLovinPrivacySettings.getAdditionalConsentStatus(89)
if ( hasMetaConsent != null )
{
// Set Meta Data Processing Options accordingly.
}
else
{
// AC String is not available on disk. Please check for consent status after the user completes the CMP flow.
}
要了解如何在加利福尼亚州实现 Meta Audience Network 的 “Limited Data Use” 标记,请访问 Meta for Developers 文档。
一些 network SDK 会在其打包的 Android Manifest 文件中使用 <queries> 元素。
如果您使用的 Android Gradle plugin 版本不兼容,则会导致以下构建错误之一:
com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
error: unexpected element <queries> found in <manifest>.
Missing 'package' key attribute on element package at [:com.my.target.mytarget-sdk-5.11.3:]
AndroidManifest Validation failed
要修复此错误,请升级至以下 Android Gradle plugin 版本之一。
这些版本支持 <queries> 元素:
请升级 Android Gradle Plugin,而非 Gradle Build Tools。
| 当前 Android Gradle plugin 版本 | 支持 <queries> 元素的版本 |
|---|---|
| 4.1.* | 全部 |
| 4.0.* | 4.0.1+ |
| 3.6.* | 3.6.4+ |
| 3.5.* | 3.5.4+ |
| 3.4.* | 3.4.3+ |
| 3.3.* | 3.3.3+ |