前言
看文档写的,文档集成和初始化和布局文件的代码都不在一起,找的我乱七八糟的,头大!记录一下这个流程吧。
关于moPub账户的注册,以及新建程序我就不一一贴出来了,费劲,只把代码贴出来。也足够Demo用了。
不论如何,也得贴个官方的文档地址:https://developers.mopub.com/publishers/android/get-started/
要求
- Android 4.1(API Version 16)及更高版本(在4.12.0中更新)
- android-support-v4.jar,r22(在3.7.0中更新)
- android-support-annotations.jar,r22(在3.7.0中更新)
- android-support-v7-recyclerview.jar,r22(在3.9.0中更新)
- MoPub排球图书馆(mopub-volley:2.1.0.aar - JCenter上提供)(5.6.0更新)
- 所需的 Google Play服务11.4.0+
步骤
- 项目级的build.gradle
buildscript {
repositories {
...
jcenter()
maven { url "https://s3.amazonaws.com/moat-sdk-builds" }
...
}
}
- app的build.gradle
注意:如果使用完整的SDK,使用第一个。如果在模块化SDK中使用选择的广告格式,则应仅声明后面适用的。
dependencies {
...
implementation 'com.android.support:support-v4:28.0.0'
//mopub all
implementation('com.mopub:mopub-sdk:5.6.0@aar') {
transitive = true
}
// For banners
// implementation('com.mopub:mopub-sdk-banner:5.3.0@aar') {
// transitive = true
// }
// For interstitials
// implementation('com.mopub:mopub-sdk-interstitial:5.6.0@aar') {
// transitive = true
// }
// For rewarded videos. This will automatically also include interstitials
// implementation('com.mopub:mopub-sdk-rewardedvideo:5.6.0@aar') {
// transitive = true
// }
// For native static (images).
// implementation('com.mopub:mopub-sdk-native-static:5.6.0@aar') {
// transitive = true
// }
// For native video. This will automatically also include native static
// implementation('com.mopub:mopub-sdk-native-video:5.6.0@aar') {
// transitive = true
// }
}
- AndroidManifest.xml
注意: ACCESS_COARSE_LOCATION
或ACCESS_FINE_LOCATION
仅在您希望设备自动发送用户的定位位置时才需要。WRITE_EXTERNAL_STORAGE
是可选的,仅适用于MRAID 2.0 storePicture广告。
<!-- Required permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Optional permissions. Will pass Lat/Lon values when available. Choose either Coarse or Fine -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Optional permissions. Used for MRAID 2.0 storePicture ads -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--================以下是MoPub相关================-->
<!--注意:如果使用完整的SDK,您应该声明所有上述活动。如果在模块化SDK中使用选择的广告格式,则应仅声明适用的活动。-->
<!-- MoPub's consent dialog -->
<activity
android:name="com.mopub.common.privacy.ConsentDialogActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<!-- All ad formats -->
<activity
android:name="com.mopub.common.MoPubBrowser"
android:configChanges="keyboardHidden|orientation|screenSize" />
<!-- Interstitials -->
<activity
android:name="com.mopub.mobileads.MoPubActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="com.mopub.mobileads.MraidActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<!-- Rewarded Video and Rewarded Playables -->
<activity
android:name="com.mopub.mobileads.RewardedMraidActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="com.mopub.mobileads.MraidVideoPlayerActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<!--================以上是MoPub相关================-->
- 添加网络安全配置文件
1、在AndroidManifest.xml文件中,添加以下内容:
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
</application>
2、在res下新建xml文件夹,然后新建network_security_config.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
...
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system"/>
</trust-anchors>
</base-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">example.com</domain>
<domain includeSubdomains="true">cdn.example2.com</domain>
</domain-config>
...
</network-security-config>
配置到此完成,加入布局文件以及java代码。
- 布局文件:
<com.mopub.mobileads.MoPubView
android:id="@+id/adview"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp"/>
- 初始化广告
注意:
1、建议放在Application中初始化,要不然在界面初始化,并立刻显示广告的话,广告第一次会加载失败。返回码是未知错误。
2、当前的AD_UNIT_ID是横幅的测试ID,正式的广告需要替换。
3、记得销毁。
private String AD_UNIT_ID = "252412d5e9364a05ab77d9396346d73d";
private void initAd() {
SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder(AD_UNIT_ID).build();
MoPub.initializeSdk(getApplicationContext(), sdkConfiguration, new SdkInitializationListener() {
@Override
public void onInitializationFinished() {
/* MoPub SDK initialized.
Check if you should show the consent dialog here, and make your ad requests. */
}
});
}
private MoPubView moPubView;
private String AD_UNIT_ID = "252412d5e9364a05ab77d9396346d73d";
private void initView() {
moPubView = findViewById(R.id.adview);
moPubView.setAdUnitId(AD_UNIT_ID); // Enter your Ad Unit ID from www.mopub.com
moPubView.loadAd();
moPubView.setBannerAdListener(new MoPubView.BannerAdListener() {
@Override
public void onBannerLoaded(MoPubView banner) {
Log.e("--->whr", "onBannerLoaded");
Toast.makeText(getApplicationContext(), "Banner successfully loaded.", Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode) {
Log.e("--->whr", "onBannerFailed:" + errorCode.toString());
Toast.makeText(getApplicationContext(), "Banner onBannerFailed loaded.", Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerClicked(MoPubView banner) {
Log.e("--->whr", "onBannerClicked");
}
@Override
public void onBannerExpanded(MoPubView banner) {
Log.e("--->whr", "onBannerExpanded");
}
@Override
public void onBannerCollapsed(MoPubView banner) {
Log.e("--->whr", "onBannerCollapsed");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
moPubView.destroy();
}
- 贴一下测试的单元ID
Format | Size | Ad unit ID |
---|---|---|
小橫幅 Banner | 320x50 | b195f8dd8ded45fe847ad89ed1d016da |
大橫幅 Banner(Mrect) | 300x250 | 252412d5e9364a05ab77d9396346d73d |
插屏 Interstitial | 320x480 | 24534e1901884e398f1253216226017e |
激励视频 Rewarded Video | 320x480 | 920b6145fb1546cf8b5cf2ac34638bb7 |
互动式激励 Rewraded Playable(MRAID) | 320x480 | 15173ac6d3e54c9389b9a5ddca69b34b |
原生 Native | n/a | 11a17b188668469fb0412708c3d16813 |
原生视频 Native video | n/a | b2b67c2a8c0944eda272ed8e4ddf7ed4 |