Get Started

Proguard & MoPub


What is Proguard?

ProGuard is an Android tool that shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names. Our proguard.txt file will keep all fundamental classes that are referenced by the MoPub SDK. It’s located in both the mopub-sdk and the mopub-sample app on GitHub. If your application is approaching the DEX 65K methods limit, ProGuard will assist in stripping unnecessary methods and shrinking the size of your APK.

Implementing ProGuard in Android Studio

To use ProGuard with Android Studio, you must enable the ProGuard setting in your build.gradle buildTypes. The minifyEnabled property is part of the buildTypes release block that controls the settings applied to release builds. Set the minifyEnabled property to true to enable ProGuard:

buildTypes { 
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Implementing ProGuard in Eclipse

Set the proguard.config property in the /project_root/project.properties file. The path can be an absolute path or a path relative to the project’s root.

    • If you left the proguard.cfg file in the it’s default location (the project’s root directory), you can specify it’s location like this:
proguard.config = proguard.config
    • You can also move the file to anywhere you want and specify the absolute path to it:
proguard.config = /path/to/proguard.cfg

Understanding the MoPub Proguard File

The MoPub proguard.txt file in it’s entirety will keep the classes for all ad formats, as well as support the Android Advertiser ID and Google Play Services libraries. However, if you’re interested in a more customized solution, below breaks the ProGuard file down line by line:

MoPub Requirements:

6  -keepclassmembers class com.mopub.** { public *; }
7  -keep public class com.mopub.**
8  -keep public class android.webkit.JavascriptInterface {}

Lines 6-8 are required by the SDK regardless of ad format used. These lines specify the entry point of the application that has to be preserved and keeps all classes and class members that implement com.mopub.

11  -keep class * extends com.mopub.mobileads.CustomEventBanner {}
12  -keepclassmembers class com.mopub.mobileads.CustomEventBannerAdapter {!private !public !protected *;}
13  -keep class * extends com.mopub.mobileads.CustomEventInterstitial {}
14  -keep class * extends com.mopub.mobileads.CustomEventNative {}

Lines 11-14 can be included or excluded based on ad format used in the UI. For example, if you are using Banner and Native ad units but are not using Interstitials, you can remove line 12 from the ProGuard file. Removing these lines of code should not affect your project’s ability to build unless an ad format is being used in your UI but is removed from the ProGuard file.

Android Advertiser ID:

16  -keep class com.google.android.gms.common.GooglePlayServicesUtil {*;}
17  -keep class com.google.android.gms.ads.identifier.AdvertisingIdClient {*;}
18  -keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info {*;}

Lines 16-18 are required only if you are interested in using the Android Advertiser ID, a user-resettable, unique, anonymous ID for advertising provided by Google Play services. IT gives users better controls and provides developers with a simple, standard system to continue to monetize their apps. If you are not interested in using the Android Advertiser API, you can remove lines 16-18.

Google Play Services:

22  -keep class * extends java.util.ListResourceBundle {
23      protected Object[][] getContents();
24  }
25
26  -keep public class 
27  com.google.android.gms.common.internal.safeparcel.SafeParcelable {
28      public static final *** NULL;
29  }
30
31  -keepnames @com.google.android.gms.common.annotation.KeepName class *
32  -keepclassmembernames class * {
33      @com.google.android.gms.common.annotation.KeepName *;
34  }
35
36  -keepnames class * implements android.os.Parcelable {
37      public static final ** CREATOR;
38  }

If you are building Proguard in Android Studio, then the ProGuard directives are already included in the Play services client libraries to preserve the required classes. The Android Plugin for Gradle automatically appends ProGuard configuration files in an AAR (Android ARchive) package and appends that package to your ProGuard configuration.

However, if you are using ProGuard in Eclipse, include lines 22-38 to ensure that the Google Play Services library is included when your project builds.