开始之前
Vungle SDK 已构建并测试到 API 版本 31、Android 12。
谷歌隐私政策合规
为确保遵守将于 2022 年 4 月 1 日生效的Google 隐私权政策变更:
- 混合受众应用程序:我们强烈建议升级到 Vungle SDK v.6.10.4 或更高版本,并使用 SDK COPPA API 通知 Vungle 用户是否未满 13 岁。
- 家庭应用程序:我们强烈建议升级到 Vungle SDK v.6.10.4 或更高版本。
要求
- Android 5.0 (API version 21) 或更高版本
- Amazon OS 5.4 or higher
- 集成需要一个Vungle帐户 ,请在该帐户中创建一个新的Android 或 Amazon应用。请参阅我们的使用发布者仪表板文章中的添加您的应用和展示位置部分,以了解如何在Vungle Dashboard中设置广告位。
下载SDK
下载 Vungle SDK for Android or Amazon.
Reference: Sample App and Java Doc
示例应用: 请参考示例应用来集成您的应用 https://github.com/Vungle/Android-SDK.
JavaDoc: https://vungle.github.io/vungle-android-sdk/javadoc.
步骤 1. 将Vungle SDK添加到您的项目中
您可以使用 Gradle 或使用 JAR or AAR 的方式来集成Vungle SDK。
// API version compatibility
minSdkVersion 21
compileSdkVersion 31
targetSdkVersion 31
Gradle
打开项目级别的
打开应用程序级别的
build.gradle
,并在“all projects”部分添加 Maven URL。allprojects {
repositories {
mavenCentral()
}
}
build.gradle
文件,并在“dependencies”部分添加其他依赖项。
dependencies {
// Vungle SDK
implementation 'com.vungle:publisher-sdk-android:6.11.0'
implementation 'androidx.core:core:1.1.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
// When appcompat is being used, core and localbroadcastmanager are the dependencies
// that is getting included
// implementation 'androidx.appcompat:appcompat:1.3.0'
// Recommended for SDK to be able to get Android Advertising ID
implementation 'com.google.android.gms:play-services-basement:18.0.1'
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
// Starting with v6.10.3, the Vungle SDK supports app set ID.
// To take advantage of this feature, include Google Play Services tasks and appset libraries
// inside build.gradle
implementation 'com.google.android.gms:play-services-tasks:18.0.1'
implementation 'com.google.android.gms:play-services-appset:16.0.2'
}
JAR
-
下载Vungle SDK 并解压,然后前往
libs
文件夹,复制所有的.jar
文件并将其添加到您的项目工程的libs
文件件:vungle-android-sdk-6.11.0.jar
gson-2.8.6.jar
okhttp-3.14.9.jar
okio-1.17.3.jar
- 打开应用程序级别的
build.gradle
文件,并在“dependencies”部分添加其他依赖项。dependencies { // Vungle SDK implementation files('libs/vungle-android-sdk-6.11.0.jar') // Required Third-party Dependencies implementation files('libs/gson-2.8.6.jar') implementation files('libs/okhttp-3.14.9.jar') implementation files('libs/okio-1.17.3.jar') implementation 'androidx.core:core:1.1.0' implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' // When appcompat is being used, core and localbroadcastmanager are the dependencies // that is getting included // implementation 'androidx.appcompat:appcompat:1.3.0' // Recommended for SDK to be able to get Android Advertising ID implementation 'com.google.android.gms:play-services-basement:18.0.1' implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1' // Starting with v6.10.3, the Vungle SDK supports app set ID. // To take advantage of this feature, include Google Play Services tasks and appset libraries // inside build.gradle implementation 'com.google.android.gms:play-services-tasks:18.0.1' implementation 'com.google.android.gms:play-services-appset:16.0.2' }
- 通过添加以下行并将应用程序项名称分配给您的应用程序类名称来更新您的
AndroidManifest.xml
:<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:hardwareAccelerated="true" android:supportsRtl="true" > <activity android:name="com.vungle.warren.ui.VungleActivity" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:launchMode="singleTop" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" /> <provider android:name="com.vungle.warren.utility.VungleProvider" android:authorities="${applicationId}.vungle-provider" android:exported="false" /> </application>
AAR
-
下载 SDK 并解压,然后前往
libs
文件夹,复制SDK.aar
和所有.jar
并添加到您的libs
文件夹中:vungle-android-sdk-6.11.0.aar
gson-2.8.6.jar
okhttp-3.14.9.jar
okio-1.17.3.jar
- 打开应用程序级别的
build.gradle
文件,并在“dependencies”部分添加其他依赖项。dependencies { // Vungle SDK implementation files('libs/vungle-android-sdk-6.11.0.aar') // Required Third-party Dependencies implementation files('libs/gson-2.8.6.jar') implementation files('libs/okhttp-3.14.9.jar') implementation files('libs/okio-1.17.3.jar') implementation 'androidx.core:core:1.1.0' implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' // When appcompat is being used, core and localbroadcastmanager are the dependencies // that is getting included // implementation 'androidx.appcompat:appcompat:1.3.0' // Recommended for SDK to be able to get Android Advertising ID implementation 'com.google.android.gms:play-services-basement:18.0.1' implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1' // Starting with v6.10.3, the Vungle SDK supports app set ID. // To take advantage of this feature, include Google Play Services tasks and appset libraries // inside build.gradle implementation 'com.google.android.gms:play-services-tasks:18.0.1' implementation 'com.google.android.gms:play-services-appset:16.0.2' }
步骤 2:导入 Vungle SDK
import com.vungle.warren.Vungle;
import com.vungle.warren.AdConfig; // Custom ad configurations
import com.vungle.warren.InitCallback; // Initialization callback
import com.vungle.warren.LoadAdCallback; // Load ad callback
import com.vungle.warren.PlayAdCallback; // Play ad callback
import com.vungle.warren.VungleNativeAd; // MREC ad
import com.vungle.warren.Banners; // Banner ad
import com.vungle.warren.VungleBanner; // Banner ad
import com.vungle.warren.Vungle.Consent; // GDPR consent
import com.vungle.warren.VungleSettings // Minimum disk space
import com.vungle.warren.error.VungleException // onError message
步骤3.添加代码
初始化 SDK
public static void init(@NonNull final String appId,
@NonNull final Context context,
@NonNull final InitCallback callback)
public static void init(@NonNull final String appId,
@NonNull final Context context,
@NonNull final InitCallback callback,
@NonNull final VungleSettings settings)
初始化方法采用以下参数:
- Vungle application ID
- Application context
-
InitCallback
-
onSuccess
: notifies when the SDK has successfully initialized -
onError
: notifies when the initialization has failed- Throws
IllegalArgumentException
ifInitCallback
is null.
- Throws
-
onAutoCacheAdAvailable
: notifies when the auto-cached placement has an ad available to play
-
-
VungleSettings
(optional)- Minimum disk space - 设置初始化要求的最小存储空间
- Restrict Android ID - 设置是否使用Android device ID
Vungle.init(appId, getApplicationContext(), new InitCallback() {
@Override
public void onSuccess() {
// SDK has successfully initialized
}
@Override
public void onError(VungleException exception) {
// SDK has failed to initialize
}
@Override
public void onAutoCacheAdAvailable(String placementId) {
// Ad has become available to play for a cache optimized placement
}
};
// 6.4.x & below
Vungle.init(appId, getApplicationContext(), new InitCallback() {
@Override
public void onSuccess() {
// SDK has successfully initialized
}
@Override
public void onError(Throwable throwable) {
// SDK has failed to initialize
}
@Override
public void onAutoCacheAdAvailable(String placementId) {
// Ad has become available to play for a cache optimized placement
}
};
Overridable Methods | Description |
---|---|
onSuccess() |
当SDK初始化成功时触发,此时可以开始请求广告或播放广告 |
onError(VungleException exception) |
当初始化失败时触发,此时请查看错误信息getLocalizedMessage 以便获知getExceptionCode 。 |
onAutoCacheAdAvailable(String placementId) |
当广告缓存好时触发。 |
您可以随时通过调用 isInitialized
方法来检查 Vungle SDK 是否已初始化:
public static boolean isInitialized()
Integrate Ad Formats
根据文档来集成各种广告格式:
更多定制广告
请查看 Advanced Settings 来完成您需要的SDK设置,例如GDPR, CCPA 和其他设置。