快速入門 - IDroi/AdZodiac GitHub Wiki

快速入門

這篇文件在於協助你整合AdZodiac廣告平台應用程式介面於你的安卓應用程式。 若你遇到任何問題或困難,請寄信至[email protected]

以下為基本整合 AdZodiac API 步驟:

  1. 申請AdZodiac帳號與廣告單位識別碼
  2. 下載AdZodiac Android API
  3. 更新你的應用程式Manifest
  4. 整合AdZodiac API至你的應用程式

申請AdZodiac帳號與廣告單位識別碼

(敬請期待...)

下載AdZodiac Android API

Android Studio使用Gradle來建構Android應用程式,Gradle可以直接使用Maven套件,我們建議直接使用此方式安裝AdZodiac Android SDK,避免手動下載與更新上的不便。你只需要更新Projectbuild.gradleappbuild.gradle,並且添加AdZodiac Android SDK相關的libraries即可:

相關的libraries

  • Android 4.1 (API version 16) 或者更新版
  • DroiBaaS-SDK-Android Core Library (Core.jar - 放在 github)
  • Facebook Ad Library, r4 (audience-network-sdk.jar - 放在 jcenter)
  • Flurry Ad Library, r6 (ads.jar - 放在 jcenter)
  • Google Play Service Library, r10 (play-services-ads.jar - 放在 jcenter)
  • Millennial Android Ad SDK (One by AOL)
  • Fresco Library

Projectbuild.gradle裡更新如下:

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.google.gms:google-services:3.1.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }
    }
}

appbuild.gradle裡更新如下:

repositories {
    jcenter()
    maven {
        url "https://github.com/DroiBaaS/DroiBaaS-SDK-Android/raw/master/"
    }
    maven {
        url "https://github.com/IDroi/AdZodiac/raw/master/"
    }
}

dependencies {
    
    compile 'com.idroi:adzodiac:+'
    compile 'com.droi.sdk:Core:+'
    compile 'com.squareup.okhttp3:okhttp:3.8.0'
    compile 'com.flurry.android:ads:6.4.2'
    compile 'com.facebook.android:audience-network-sdk:4.+'
    compile 'com.google.android.gms:play-services-base:10.2.1'
    compile 'com.google.android.gms:play-services-ads:10.2.1'

    compile 'com.facebook.fresco:fresco:1.4.0'
    compile 'com.facebook.fresco:animated-gif:1.4.0'
    compile 'com.facebook.fresco:animated-webp:1.4.0'
    compile 'com.facebook.fresco:webpsupport:1.4.0'

    compile 'com.adcolony:sdk:3.3.2'

    // 當開發者有開啟One by AOL網路時需要加入Millennial Media的Android Ad SDK
    compile files('libs/millennialmedia-android-ad-sdk.aar')

    // 當開發者有開啟ChartBoost網路時需要加入ChartBoost的Android Ad SDK
    compile files('libs/chartboost.jar')
}

更新你的應用程式Manifest

AndroidManifest.xml裡添加以下的宣告在PermissionsActivity

宣告下列的uses-permission標籤:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

宣告下列的activity標籤於你的<application>

<activity android:name="com.flurry.android.FlurryFullscreenTakeoverActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

<activity android:name="com.facebook.ads.AudienceNetworkActivity" android:configChanges="keyboardHidden|orientation|screenSize" />

<activity android:name="com.adcolony.sdk.AdColonyInterstitialActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:hardwareAccelerated="true"/>

<activity android:name="com.adcolony.sdk.AdColonyAdViewActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:hardwareAccelerated="true"/>

<activity android:name="com.chartboost.sdk.CBImpressionActivity" android:excludeFromRecents="true" android:hardwareAccelerated="true" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" android:configChanges="keyboardHidden|orientation|screenSize" />

<activity android:name="com.adzodiac.common.AdZodiacBrowser" android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.adzodiac.mobileads.AdZodiacActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.adzodiac.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.adzodiac.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.adzodiac.mobileads.RewardedMraidActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>

<meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
<!--當開發者已經有使用卓易BaaS服務時可以不用填下列應用程式識別碼-->
<meta-data android:name="com.droi.sdk.application_id" 
        android:value="hlqvmbzhC6LuapuYJPwEVziIUxMql1MAlQDblHgI" />
<!--當開發者有開啟One by AOL網路時需要加入的provider-->
<provider
    android:name="com.millennialmedia.internal.utils.MediaContentProvider"
    android:authorities="${applicationId}.MediaContentProvider"
    android:grantUriPermissions="true"
    android:exported="false" />

加入Fresco的初始化在應用程式的開始

AdZodiac目前使用Fresco 來達成圖片的內存管理,開發者可以依據自己的應用需求調整內存使用量規範的設定檔,以下列出常用的設定範例,若需要有進階的要求可以直接參考Fresco的官方網站文件

MemoryCacheParams mBitmapCacheParams = new MemoryCacheParams(
            10 * ByteConstants.MB,  // The maximum size of the cache, in bytes.
            Integer.MAX_VALUE,      // The maximum number of items that can live in the cache.
            3 * ByteConstants.MB,   // The eviction queue is an area of memory that stores items ready for eviction but have not yet been deleted. This is the maximum size of that queue in bytes.
            Integer.MAX_VALUE,      // The maximum number of entries in the eviction queue.
            ByteConstants.MB        // The maximum size of a single cache entry. 
            );

protected void onCreate(Bundle savedInstanceState){

    Supplier<MemoryCacheParams> mSupplierMemoryCacheParams = new Supplier<MemoryCacheParams>() {
            @Override
            public MemoryCacheParams get() {
                return mBitmapCacheParams;
            }
    };

    DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(this)
            .setMaxCacheSize(100 * ByteConstants.MB)                    // This is the default maximum size of the cache.
            .setMaxCacheSizeOnLowDiskSpace(30 * ByteConstants.MB)       // This is the maximum size of the cache that is used when the device is low on disk space.
            .setMaxCacheSizeOnVeryLowDiskSpace(10 * ByteConstants.MB)   // This is the maximum size of the cache when the device is extremely low on disk space.
            .build();

    ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
            .setBitmapMemoryCacheParamsSupplier(mSupplierMemoryCacheParams)
            .setMainDiskCacheConfig(diskCacheConfig)
            .setDownsampleEnabled(true)
            .setBitmapsConfig(Bitmap.Config.RGB_565)
            .build();

    Fresco.initialize(getApplicationContext(), config);
}

混淆規則

若你的應用程式需要進行混淆,請在混淆規則的文件中加入以下的內容:

-keepattributes *Annotation*, Exceptions, InnerClasses, Signature, EnclosingMethod

-dontwarn com.droi.sdk.**
-dontwarn okio.**
-keep class com.droi.sdk.** { *; }
-keep interface com.droi.sdk.** { *; }
-keep class com.tyd.aidlservice.internal.** { *; }
-keep interface com.tyd.aidlservice.internal.** { *; }
-keep class * extends com.droi.sdk.core.DroiObject { @com.droi.sdk.core.DroiExpose *; @com.droi.sdk.core.DroiReference *;}
-keep class * extends com.droi.sdk.core.DroiUser { @com.droi.sdk.core.DroiExpose *; @com.droi.sdk.core.DroiReference *;}

# Keep public classes and methods.
-keepclassmembers class com.adzodiac.** { public *; }
-keep public class com.adzodiac.**
-keep public class android.webkit.JavascriptInterface {}
-dontwarn com.adzodiac.**

# Explicitly keep any custom event classes in any package.
-keep class * extends com.adzodiac.mobileads.CustomEventBanner {}
-keep class * extends com.adzodiac.mobileads.CustomEventInterstitial {}
-keep class * extends com.adzodiac.nativeads.CustomEventNative {}
-keep class * extends com.adzodiac.nativeads.CustomEventRewardedAd {}

# Keep methods that are accessed via reflection
-keepclassmembers class ** { @com.adzodiac.common.util.ReflectionTarget *; }

# Support for Android Advertiser ID.
-keep class com.google.android.gms.common.GooglePlayServicesUtil {*;}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient {*;}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info {*;}

# Support for Google Play Services
# http://developer.android.com/google/play-services/setup.html
-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

# For communication with AdColony's WebView (AdColony)
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

# Keep ADCNative class members unobfuscated (AdColony)
-keepclassmembers class com.adcolony.sdk.ADCNative** {
    *;
}

# ChartBoost
-keep class com.chartboost.** { *; }

# Millennial
-keepclassmembers class com.millennialmedia** {
public *;
}
-keep class com.millennialmedia**
-keep class com.google.ads.mediation.millennial.** {public *;}
-dontwarn com.millennialmedia.**

# Required to preserve the Flurry SDK
-keep class com.flurry.** { *; }
-dontwarn com.flurry.**
-keepclasseswithmembers class * {
   public <init>(android.content.Context, android.util.AttributeSet, int);
}

# Preserve Facebook Audience Network Ads classes
-keep class com.facebook.** { *; }
-dontwarn com.facebook.**

# Preserve Google Ads classes
-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

-keep class com.google.android.exoplayer.**
-dontwarn com.google.android.exoplayer.**

恭喜

你已經完成整合AdZodiac應用程式介面於你的應用程式的設定。現在,你可以開始藉由下列所提供的API在你的應用程式裡展示廣告來獲取收益。

對於每個廣告類型,你可以選擇性地在下載廣告時使用特定的關鍵字,這將有助於你瞄準你的廣告客戶族群。

範例程式下載:

⚠️ **GitHub.com Fallback** ⚠️