原生广告
原生广告可让您以与现有设计一致的方式通过应用获利。 AppLovin MAX SDK 让您可以访问广告的各个资产。 这样,您就可以设计与应用的外观和感觉一致的广告布局。 SDK 自动缓存图像并跟踪指标。 因此您可以专注于如何、何时以及在何处展示广告。
模板
最简单的将原生广告集成到应用中的方法,就是使用模板 API。
AppLovin MAX SDK 允许您加载 android.view.View
,并为您预先填充了原生广告的资源。要使用模板 API,请在 Create New Ad Unit 界面中选择 “Small” (360×120 dp) 或 “Medium” (300×250 dp) 尺寸的模板。“Small” 模板的媒体视图占模板宽度的三分之一,并覆盖模板的高度。
“Medium” 模板的媒体视图会根据媒体内容的宽度和高度进行调整,同时保持内容的原始长宽比例,确保不会出现拉伸或变形。
选择最符合您需求的尺寸。
原生广告的容器视图 (container view) 需要设置合适的高度,以便媒体内容能够正确呈现。
SDK 会自动为您处理视图布局。
加载原生广告
要加载原生广告,请创建对应您广告单元的 MaxNativeAdLoader
对象,并调用 loadAd()
方法。
安装 MaxNativeAdLoadListener
以便在原生广告就绪时收到通知。安装 MaxAdListener
以接收其他广告相关事件的通知。
调用 destroy()
方法清理所有未使用的原生广告实例。
public class ExampleActivity extends Activity{ private MaxNativeAdLoader nativeAdLoader; private MaxAd nativeAd;
void createNativeAd() { FrameLayout nativeAdContainer = findViewById( R.id.native_ad_layout );
nativeAdLoader = new MaxNativeAdLoader( "«ad-unit-ID»", this ); nativeAdLoader.setNativeAdListener( new MaxNativeAdListener() { @Override public void onNativeAdLoaded(final MaxNativeAdView nativeAdView, final MaxAd ad) { // Clean up any pre-existing native ad to prevent memory leaks. if ( nativeAd != null ) { nativeAdLoader.destroy( nativeAd ); }
// Save ad for cleanup. nativeAd = ad;
// Add ad view to view. nativeAdContainer.removeAllViews(); nativeAdContainer.addView( nativeAdView ); }
@Override public void onNativeAdLoadFailed(final String adUnitId, final MaxError error) { // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay }
@Override public void onNativeAdClicked(final MaxAd ad) { // Optional click callback } } );
nativeAdLoader.loadAd(); }
@Override protected void onDestroy() { // Destroy the native ad and native ad loader to prevent memory leaks. if ( nativeAd != null ) { nativeAdLoader.destroy( nativeAd ); }
nativeAdLoader.destroy(); super.onDestroy(); }}
class ExampleActivity : Activity(){ private lateinit var nativeAdLoader: MaxNativeAdLoader private var nativeAd: MaxAd? = null
fun createNativeAd() { val nativeAdContainer = findViewById(R.id.native_ad_layout) nativeAdLoader = MaxNativeAdLoader("«ad-unit-ID»", this) nativeAdLoader.setNativeAdListener(object : MaxNativeAdListener() {
override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) { // Clean up any pre-existing native ad to prevent memory leaks. if (nativeAd != null) { nativeAdLoader.destroy(nativeAd) }
// Save ad for cleanup. nativeAd = ad
// Add ad view to view. nativeAdContainer.removeAllViews() nativeAdContainer.addView( nativeAdView ) }
override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) { // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay }
override fun onNativeAdClicked(ad: MaxAd) { // Optional click callback } }) nativeAdLoader.loadAd() }
override fun onDestroy() { // Destroy the native ad and native ad loader to prevent memory leaks. if (nativeAd != null) { nativeAdLoader.destroy(nativeAd) }
nativeAdLoader.destroy() super.onDestroy() }}
资源规模
对于 AppLovin Exchange 广告需求,标题、描述和 CTA 所允许的最大字符数如下:
资源 | 最大字符数 |
---|---|
标题 | 50 个字符 |
描述 | 150 个字符。可以在第 149 个字符后添加省略号 (... )作为第 150 个字符。 |
CTA | 15 个字符 |
对于 SDK 聚合的广告平台,最大字符数由广告平台设置。
如何获取媒体内容的宽高比
以下代码片段展示了如何获取原生广告的媒体内容宽高比:
@Overridepublic void onNativeAdLoaded(final MaxNativeAdView adView, final MaxAd ad){ MaxNativeAd nativeAd = ad.getNativeAd(); if ( nativeAd != null ) { float aspectRatio = nativeAd.getMediaContentAspectRatio(); }}
override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) { val nativeAd = ad.nativeAd if (nativeAd != null) { val aspectRatio = nativeAd.mediaContentAspectRatio }}
支持的适配器版本
广告渠道 | 适配器版本 |
---|---|
BidMachine | 1.9.4.1 |
Google Ad Manager | 20.5.0.2 |
Google Bidding 和 Google AdMob | 20.5.0.2 |
InMobi | 10.0.5.3 |
LINE | 2021.5.11.9 |
Meta Audience Network | 6.8.0.3 |
Mintegral | 15.8.1.1 |
Pangle | 4.1.1.5.2 |
Smaato | 21.6.7.1 |
VK Ad Network | 5.14.4.2 |
Yandex | 5.3.0.1 |
手动
如果您有自定义视图并且想要手动将原生广告资源加载到这些视图中,请使用此 API。 这种集成方法主要涉及三个步骤:
- 绑定 UI 组件。
- 加载原生广告。
- 销毁原生广告。
要使用手动 API,在 Create New Ad Unit 界面中选择 Manual 模板:
绑定 UI 组件
您可以将自定义 UI 组件绑定到 MAX SDK。 然后,您可以将原生广告资产呈现到这些组件中。 下面的示例通过使用布局编辑器和唯一视图 ID 创建的自定义视图来演示这一点。 但是,如果您以编程方式创建视图,也可以使用此方法。
根据 AppLovin 的政策,您的广告必须包含隐私信息图标。
此图标链接至重要隐私声明。
您可以通过 MaxNativeAdViewBinder.Builder#setOptionsContentViewGroupId(…)
进行绑定。
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView android:id="@+id/icon_image_view" … /> <LinearLayout android:id="@+id/ad_options_view" … /> <TextView android:id="@+id/title_text_view" … /> <FrameLayout android:id="@+id/star_rating_view" … /> <TextView android:id="@+id/advertiser_textView" … /> <TextView android:id="@+id/body_text_view" … /> <FrameLayout android:id="@+id/media_view_container" … /> <Button android:id="@+id/cta_button" … /></androidx.constraintlayout.widget.ConstraintLayout>
下一步,使用实例为 MaxNativeAdViewBinder
的唯一标签 ID 绑定子视图。AppLovin 并不保证某个广告平台会返回特定资源。
public class ExampleActivity extends Activity implements MaxAdRevenueListener{ ⋮ private MaxNativeAdView createNativeAdView() { MaxNativeAdViewBinder binder = new MaxNativeAdViewBinder.Builder( R.layout.native_custom_ad_view ) .setTitleTextViewId( R.id.title_text_view ) .setBodyTextViewId( R.id.body_text_view ) .setStarRatingContentViewGroupId( R.id.star_rating_view ) .setAdvertiserTextViewId( R.id.advertiser_textView ) .setIconImageViewId( R.id.icon_image_view ) .setMediaContentViewGroupId( R.id.media_view_container ) .setOptionsContentViewGroupId( R.id.options_view ) .setCallToActionButtonId( R.id.cta_button ) .build();
return new MaxNativeAdView( binder, this ); } ⋮}
class ExampleActivity : Activity(), MaxAdRevenueListener{ ⋮ private fun createNativeAdView(): MaxNativeAdView { val binder: MaxNativeAdViewBinder = MaxNativeAdViewBinder.Builder(R.layout.native_custom_ad_view) .setTitleTextViewId(R.id.title_text_view) .setBodyTextViewId(R.id.body_text_view) .setStarRatingContentViewGroupId(R.id.star_rating_view ) .setAdvertiserTextViewId(R.id.advertiser_textView) .setIconImageViewId(R.id.icon_image_view) .setMediaContentViewGroupId(R.id.media_view_container) .setOptionsContentViewGroupId(R.id.options_view) .setCallToActionButtonId(R.id.cta_button) .build() return MaxNativeAdView(binder, this) } ⋮}
加载原生广告
加载预渲染广告
要加载预渲染原生广告,请首先创建一个对应您广告单元 ID 的 MaxNativeAdLoader
对象,并调用 loadAd(MaxNativeAdView)
方法。
安装并设置 MaxNativeAdLoadListener
,以便在原生广告的加载状态发生变化时获得通知。
分别加载和呈现广告
要加载原生广告,请首先创建一个对应您广告单元 ID 的 MaxNativeAdLoader
对象,并调用 loadAd()
方法。
安装并设置 MaxNativeAdLoadListener
,以便在原生广告的加载状态发生变化时获得通知。然后使用 onNativeAdLoaded
中返回的 MaxAd
来呈现广告视图。您可以通过调用 MaxNativeAdLoader.render(MaxNativeAdView, MaxAd)
来实现。
销毁原生广告
如果您停止使用原生广告,请调用 destroy()
方法清理资源。
否则随着时间的推移,应用的表现会下降。
以下示例展示了如何加载和销毁原生广告。
这需要在您绑定上一步中的 UI 组件后进行。
public class ExampleActivity extends Activity implements MaxAdRevenueListener{ private ViewGroup nativeAdContainerView; private MaxNativeAdLoader nativeAdLoader; private MaxAd loadedNativeAd;
⋮
private void createNativeAdLoader() { nativeAdLoader = new MaxNativeAdLoader( "«ad-unit-ID»", this ); nativeAdLoader.setRevenueListener( this ); nativeAdLoader.setNativeAdListener( new NativeAdListener() ); }
private void loadNativeAd() { nativeAdLoader.loadAd( createNativeAdView() ); }
@Override public void onAdRevenuePaid(final MaxAd ad) { }
private class NativeAdListener extends MaxNativeAdListener { @Override public void onNativeAdLoaded(final MaxNativeAdView nativeAdView, final MaxAd nativeAd) { // Clean up any pre-existing native ad to prevent memory leaks. if ( loadedNativeAd != null ) { nativeAdLoader.destroy( loadedNativeAd ); }
// Save ad for cleanup. loadedNativeAd = nativeAd;
nativeAdContainerView.removeAllViews(); nativeAdContainerView.addView( nativeAdView ); }
@Override public void onNativeAdLoadFailed(final String adUnitId, final MaxError error) { // Native ad load failed. // AppLovin recommends retrying with exponentially higher delays up to a maximum delay. }
@Override public void onNativeAdClicked(final MaxAd nativeAd) { } }
@Override protected void onDestroy() { // Destroy the native ad and native ad loader to prevent memory leaks. if ( loadedNativeAd != null ) { nativeAdLoader.destroy( loadedNativeAd ); }
nativeAdLoader.destroy(); super.onDestroy(); }}
class ExampleActivity : Activity(), MaxAdRevenueListener{ private val nativeAdContainerView: ViewGroup? = null private var nativeAdLoader: MaxNativeAdLoader? = null private var loadedNativeAd: MaxAd? = null
⋮
private fun createNativeAdLoader() { nativeAdLoader = MaxNativeAdLoader("«ad-unit-ID»", this) nativeAdLoader.setRevenueListener(this) nativeAdLoader.setNativeAdListener(NativeAdListener()) }
private fun loadNativeAd() { nativeAdLoader.loadAd(createNativeAdView()) }
override fun onAdRevenuePaid(ad: MaxAd) {}
private inner class NativeAdListener : MaxNativeAdListener() { override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, nativeAd: MaxAd) { // Clean up any pre-existing native ad to prevent memory leaks. if (loadedNativeAd != null) { nativeAdLoader.destroy(loadedNativeAd) }
// Save ad for cleanup. loadedNativeAd = nativeAd nativeAdContainerView.removeAllViews() nativeAdContainerView.addView(nativeAdView) }
override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) { // Native ad load failed. // AppLovin recommends retrying with exponentially higher delays up to a maximum delay. }
override fun onNativeAdClicked(nativeAd: MaxAd) {}
override fun onDestroy() { // Destroy the native ad and native ad loader to prevent memory leaks. if (loadedNativeAd != null) { nativeAdLoader.destroy(loadedNativeAd) }
nativeAdLoader.destroy() super.onDestroy() } }}
资源规模
AppLovin 建议您在应用程序其余部分的外观环境中适当加入尽可能多的原生元素。 这些可能包括标题和媒体视图或图标。 如果您向用户提供更多信息,这将有助于他们决定是否要点击广告。
对于 AppLovin Exchange 广告需求,标题、描述和 CTA 所允许的最大字符数如下:
资源 | 最大字符数 |
---|---|
标题 | 50 个字符 |
描述 | 150 个字符。可以在第 149 个字符后添加省略号 (... )作为第 150 个字符。 |
CTA | 15 个字符 |
对于 SDK 聚合的广告平台,最大字符数由广告平台设置。
如何获取媒体内容的宽高比
以下代码片段演示了如何获取原生广告的媒体内容宽高比:
@Overridepublic void onNativeAdLoaded(final MaxNativeAdView adView, final MaxAd ad){ MaxNativeAd nativeAd = ad.getNativeAd(); if ( nativeAd != null ) { float aspectRatio = nativeAd.getMediaContentAspectRatio(); }}
override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) { val nativeAd = ad.nativeAd if (nativeAd != null) { val aspectRatio = nativeAd.mediaContentAspectRatio }}
支持的适配器版本
广告渠道 | 适配器版本 |
---|---|
BidMachine | 1.9.4.1 |
Google Ad Manager | 20.5.0.2 |
Google Bidding 和 Google AdMob | 20.5.0.2 |
InMobi | 10.0.5.3 |
LINE | 2021.5.11.9 |
Meta Audience Network | 6.8.0.3 |
Mintegral | 15.8.1.1 |
Pangle | 4.1.1.5.2 |
Smaato | 21.6.7.1 |
VK Ad Network | 5.14.4.2 |
Yandex | 5.3.0.1 |
星级评定
从 AppLovin MAX SDK v11.7.0 开始,您可以访问并呈现所宣传应用的星级评定。 如果可用,该值是 [0.0, 5.0] 范围内的浮点数。
MAX SDK 会自动在您指定为星级评定容器的容器视图中渲染星星。 如果网络不提供星级评定,或者星级评定 < 3,则 SDK 不会填充星级评定容器视图。 您有责任相应地调整您的布局。
要检索当前广告的星级:
@Overridepublic void onNativeAdLoaded(final MaxNativeAdView adView, final MaxAd ad){ MaxNativeAd nativeAd = ad.getNativeAd(); if ( nativeAd != null ) { Double starRating = nativeAd.getStarRating(); if ( starRating == null || starRating < 3 ) { // Star rating not available, or < 3 - hide star rating container view return; }
// Star rating available and >= 3 - show star rating container view }}
override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) { val nativeAd = ad.nativeAd if (nativeAd != null) { val starRating = nativeAd.starRating if (starRating == null || starRating < 3) { // Star rating not available, or < 3 - hide star rating container view return }
// Star rating available and >= 3 - show star rating container view }}
支持的适配器版本(星级)
广告渠道 | 适配器版本 |
---|---|
Google Ad Manager | 20.5.0.2 |
Google Bidding 和 Google AdMob | 20.5.0.2 |
InMobi | 10.1.2.3 |
Mintegral | 16.3.41.1 |
Ad Placer
广告投放器会使用 RecyclerView
自动将原生广告插入您现有的内容流中。
要集成此 API,请进行以下高级步骤:
- 设置原生广告布局。
- 创建 Ad Placer 设置。
- 配置 Ad Placer。
- 配置广告呈现设置。
- 可选地,设置监听器。
下面将更详细地解释这些步骤。
根据 AppLovin 的政策,您的广告必须包含隐私信息图标。
此图标链接至重要隐私声明。
您可以通过 MaxNativeAdViewBinder.Builder#setOptionsContentViewGroupId(…)
进行绑定。
1. 设置原生广告布局
广告投放器支持模板或手动原生广告布局。 请参阅本页的“Tempates”和“Manual”部分了解如何配置此类广告布局。 请参阅下方的 配置广告呈现设置,了解如何根据您的布局设置 Ad Placer。
2. 创建 Ad Placer 设置
使用您的广告单元标识符,创建一个 MaxAdPlacerSettings
对象。该对象用于配置您的 Ad Placer,并提供广告在信息流中的定位信息。
MaxAdPlacerSettings settings = new MaxAdPlacerSettings( "«ad-unit-ID»" );
val settings = MaxAdPlacerSettings("«ad-unit-ID»")
配置广告位置
Ad Placer 将广告放置在您的信息流中。 它至少基于下列之一来实现这一点:
- 指示广告投放位置的固定索引路径列表
- 定期插入广告的重复间隔
- Ad Placer 会在最后一个固定位置后插入重复广告。 如果您未设置任何固定位置,则 Ad Placer 会在流中的第一个位置后重复添加广告。 该间隔值必须大于或等于2。 小于 2 的间隔值会禁用 Ad Placer。
通过修改 MaxAdPlacerSettings
来配置广告位置
- 要添加新的固定广告位置(例如,在第 1 行):
settings.addFixedPosition( 3 );
settings.addFixedPosition( 3 )
- 删除现有的固定位置:
settings.resetFixedPositions();
settings.resetFixedPositions()
- 要更改重复间隔(例如,在 feed 中每五个位置插入一个广告):
settings.setRepeatingInterval( 5 );
settings.repeatingInterval = 5
其他设置
您可以通过 MaxAdPlacerSettings
调整这些设置来配置您的广告投放器:
maxAdCount
- 将其设置为流中的最大广告数量(默认值 = 256)。 如果流包含多个部分,则这将决定每个广告部分的最大广告数量。
maxPreloadedAdCount
- 将其设置为在流中预加载的最大广告数(默认值为 4)。
3. 设置 Ad Placer
设置广告投放器时,请选择以下两个选项之一:
- 如果您的内容流基于
RecyclerView
,请使用MaxRecyclerAdapter
。 该辅助类对原始适配器进行了封装,能够自动渲染并将广告插入到内容流中。 您可以通过调用getAdPlacer()
来访问底层广告投放器。 - 您可能更愿意创建子类或直接使用
MaxAdPlacer
,例如当您的内容流使用了其他自定义 UI 组件时。
使用 MaxRecyclerAdapter
要使用 MaxRecyclerAdapter
设置 Ad Placer,请按照下列说明操作:
- 使用您的设置、原始适配器和活动来对
MaxRecyclerAdapter
进行初始化 :adAdapter = new MaxRecyclerAdapter( settings, adapter, this );
adAdapter = MaxRecyclerAdapter(settings, adapter, this)
- 将您的 Recycler View (回收视图) 的适配器设置为已创建的
MaxRecyclerAdapter
:recyclerView.setAdapter( adAdapter );
recyclerView.adapter = adAdapter
- 更新您的代码以执行以下操作:
- 无论何时您在流中添加或删除项目,请从适配器调用适当的方法。
这些方法会将您信息流的更新通知广告投放者:
notifyItemInserted()
notifyItemRemoved()
notifyItemsInserted()
notifyItemsRemoved()
- 如果您在原始适配器上注册了任何数据观察者,请改为在
MaxRecyclerAdapter
上注册。 这样他们就能在广告插入后收到调整后的内容项位置。 - 如果代码的任何其他部分依赖于广告插入前内容项的位置,请将位置转换为原始位置。
您可以通过调用
adAdapter.getOriginalPosition()
来实现。
- 无论何时您在流中添加或删除项目,请从适配器调用适当的方法。
这些方法会将您信息流的更新通知广告投放者:
- 调用
loadAds()
来启动加载广告:adAdapter.loadAds();
adAdapter.loadAds()
以下代码展示了如何使用 MaxRecyclerAdapter
将广告加载到 Recycler View 中:
@Overridepublic void onCreate(Bundle savedInstanceState){ super.onCreate( savedInstanceState ); // Create your own recycler view and original adapter ⋮
// Configure ad adapter MaxAdPlacerSettings settings = new MaxAdPlacerSettings( "«ad-unit-ID»" ); settings.setRepeatingInterval( 5 ); adAdapter = new MaxRecyclerAdapter( settings, originalAdapter, this );
// Optionally, register listener adAdapter.setListener( this );
// Configure recycler view and load ads recyclerView.setAdapter( adAdapter ); adAdapter.loadAds();}
@Overridepublic void onDestroy(){ adAdapter.destroy(); super.onDestroy();}
override fun onCreate(savedInstanceState: Bundle?){ super.onCreate(savedInstanceState) // Create your own recycler view and original adapter ⋮
// Configure ad adapter val settings = MaxAdPlacerSettings("«ad-unit-ID»") settings.repeatingInterval = 5 adAdapter = MaxRecyclerAdapter(settings, originalAdapter, this)
// Optionally, register listener adAdapter.listener = this
// Configure recycler view and load ads recycler.adapter = adAdapter adAdapter.loadAds()}
override fun onDestroy(){ adAdapter.destroy() super.onDestroy()}
4.配置广告呈现设置
模板
如果您使用默认模板,则广告视图大小将自动调整为“小”模板的 360×120 和“中”模板的 360×300。
您可以在加载广告之前,在 Ad Placer 上设置 setAdSize
属性来自定义渲染广告的大小。
例如,要将广告尺寸设置为宽度为 300,高度为 200,请进行以下调用:
adAdapter.getAdPlacer().setAdSize( 300, 200 );
adAdapter.adPlacer.setAdSize(300, 200)
您也可以传递 -1 (MATCH_PARENT
) 宽度或高度值。
此时广告视图会填充其父级视图的对应维度。
手动
如果您使用手动布局,则必须在加载广告之前调用广告投放器上的 setNativeAdViewBinder 。 使用手动模板时请务必设置广告尺寸。 这样可以优化渲染。
5. 可选设置监听器
广告投放器支持可选的监听器,用于通知您事件:
onAdLoaded()
onAdRemoved()
onAdClicked()
onAdRevenuePaid()