Native ads let you monetize your app in a way that’s consistent with its existing design. The AppLovin MAX SDK gives you access to an ad’s individual assets. This way, you can design the ad layout to be consistent with the look and feel of your app. The SDK automatically caches images and tracks metrics. You can focus on how, when, and where to display ads.
模板集成已弃用。 如果您通过模板在应用中集成了原生广告,这些广告会在 开始出现未填充 (no-fill) 问题。即将在未来的 SDK 版本中完全移除对模板集成的支持。
如果您有自定义视图并且想要手动将原生广告资源加载到这些视图中,请使用此 API。 这种集成方法主要涉及三个步骤:
要使用手动 API,请在 Create New Ad Unit 界面中选择 Manual:

You can bind custom UI components to the MAX SDK. Then you can render native ad assets into those components. The example below demonstrates this with custom views that you create with the Layout Editor and unique view IDs. You can also use this method if you create your views programmatically.
To accord with AppLovin’s policy, your ad must contain the Privacy Information icon.
This icon links to an important privacy notice.
You can bind to it via 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) 来实现。
Native ads are expected to be shown in the ad expiration window of four hours. Impressions associated with ads that show outside of this window are invalidated.
If you stop using a native ad, destroy its resources by calling the destroy() method.
If you do not, the performance of your app degrades over time.
Below is an example of how you load and destroy a native ad.
This takes place after you have bound the UI components from the previous step.
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»" );
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)
{
// Destroy 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»")
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)
{
// Destroy 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 recommends that you incorporate as many of the native elements as are appropriate in the context of what the rest of your app looks like. These may include the Title and Media View or Icon. If you give the user more information, this helps them decide whether they want to click the ad.
对于 AppLovin Exchange 广告需求,标题、描述和 CTA 所允许的最大字符数如下:
| 资源 | 最大字符数 |
|---|---|
| 标题 | 50 个字符 |
| 描述 | 150 个字符。可以在第 149 个字符后添加省略号 (...)作为第 150 个字符。 |
| CTA | 15 个字符 |
对于 SDK 聚合的广告平台,最大字符数由广告平台设置。
下面的代码片段演示了如何获取您原生广告的媒体内容长宽比:
@Override
public 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
}
}有些网络不提供媒体内容的宽高比。 对于这些网络,该值为零。
您可以访问并渲染所推广应用的星级评分。 在可用情况下,该值是 [0.0, 5.0] 范围内的浮点数。
MAX SDK 会自动在您指定为星级评定容器的容器视图中渲染星星。 如果网络不提供星级评定,或者星级评定 < 3,则 SDK 不会填充星级评定容器视图。 您有责任相应地调整您的布局。
要检索当前广告的星级:
@Override
public 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
}
}广告投放器会使用 RecyclerView 自动将原生广告插入您现有的内容流中。
要集成此 API,请进行以下高级步骤:
This page explains these steps in greater detail below.
To accord with AppLovin’s policy, your ad must contain the Privacy Information icon.
This icon links to an important privacy notice.
You can bind to it via MaxNativeAdViewBinder.Builder#setOptionsContentViewGroupId(…).
Ad placer supports manual native ad layouts. See the “Manual” section of this page to learn how to configure such ad layouts. See Configure Ad Rendering Settings below to learn how to configure the ad placer to support your layout.
遵循每个广告平台的指导并绑定其所需的广告资源。 否则它们可能会使你的展示失效。
使用您的广告单元标识符,创建一个 MaxAdPlacerSettings 对象。该对象用于配置您的 Ad Placer,并提供广告在信息流中的定位信息。
MaxAdPlacerSettings settings = new MaxAdPlacerSettings( "«ad-unit-ID»" );val settings = MaxAdPlacerSettings("«ad-unit-ID»")Ad Placer 将广告放置在您的信息流中。 它至少基于下列之一来实现这一点:
通过修改 MaxAdPlacerSettings 来配置广告位置
settings.addFixedPosition( 3 );
settings.addFixedPosition( 3 )
settings.resetFixedPositions();
settings.resetFixedPositions()
settings.setRepeatingInterval( 5 );
settings.repeatingInterval = 5
您可以通过 MaxAdPlacerSettings 调整这些设置来配置您的广告投放器:
maxAdCountmaxPreloadedAdCountChoose one of these two options when you configure your ad placer:
RecyclerView,请使用 MaxRecyclerAdapter。
该辅助类对原始适配器进行了封装,能够自动渲染并将广告插入到内容流中。
您可以通过调用 getAdPlacer() 来访问底层广告投放器。MaxAdPlacer,例如当您的内容流使用了其他自定义 UI 组件时。To configure your ad placer with MaxRecyclerAdapter follow these instructions:
MaxRecyclerAdapter 进行初始化 :adAdapter = new MaxRecyclerAdapter( settings, adapter, this );
adAdapter = MaxRecyclerAdapter(settings, adapter, this)
MaxRecyclerAdapter:recyclerView.setAdapter( adAdapter );
recyclerView.adapter = adAdapter
notifyItemInserted()notifyItemRemoved()notifyItemsInserted()notifyItemsRemoved()MaxRecyclerAdapter 上注册。
这样他们就能在广告插入后收到调整后的内容项位置。adAdapter.getOriginalPosition() 来实现。loadAds() 来启动加载广告:adAdapter.loadAds();
adAdapter.loadAds()
以下代码展示了如何使用 MaxRecyclerAdapter 将广告加载到 Recycler View 中:
@Override
public 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();
}
@Override
public 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()
}在加载广告前,调用 ad plcer 上的 setNativeAdViewBinder。
请始终设置广告尺寸,以优化渲染。
广告投放器支持可选的监听器,用于通知您事件:
onAdLoaded()onAdRemoved()onAdClicked()onAdRevenuePaid()