Fabric plugin MoPub integration in Android studio

android
fabric

#1

Hi,
I can’t use MoPub with Fabric in my android game, I followed this tutorial https://fabric.io/kits/android/mopub/install as well as the plugin instruction but Fabric refuse to se new MoPub() as a Kit and I get a wrong argument type error for the line:

Fabric.with(this, new Crashlytics(), new MoPub());

Error:(89, 15) error: method with in class Fabric cannot be applied to given types;
required: Context,Kit[]
found: AndroidLauncher,Crashlytics,MoPub
reason: varargs mismatch; MoPub cannot be converted to Kit

I Tried

compile('com.mopub:mopub-sdk:3.13.0@aar') {
    transitive = true
}

and

compile('com.mopub:mopub-sdk:4.0.0@aar') {
    transitive = true
}

both worked at compilation time and let me import MoPub dependencies. But I don’t understand why the new MoPub() object is just not recognized by Fabric.

Thanks in advance for any help.


#2

Hey @axldotm,

Happy to help with this! Can you send over your full build.gradle?

-Mike


#3

Hi, since I use the Libgdx crossPlatform library I have several build.gradle here are the one for android:

My project structure:

For my SetupGame Project:

   buildscript {
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url 'https://maven.fabric.io/public' }
        jcenter()
    }
    dependencies {
        classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'org.robovm:robovm-gradle-plugin:1.0.0-beta-04'
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

    version = '1.0'
    ext {
        appName = 'my-gdx-game'
        gdxVersion = '1.5.4'
        roboVMVersion = '1.0.0-beta-04'
        box2DLightsVersion = '1.3'
        ashleyVersion = '1.3.1'
        aiVersion = '1.5.0'
    }

    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":desktop") {
    apply plugin: "java"


    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "com.android.application"
    apply plugin: 'io.fabric'

    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
    }
}

project(":ios") {
    apply plugin: "java"
    apply plugin: "robovm"

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "org.robovm:robovm-rt:${roboVMVersion}"
        compile "org.robovm:robovm-cocoatouch:${roboVMVersion}"
        compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"
    }
}

project(":html") {
    apply plugin: "gwt"
    apply plugin: "war"


    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
    }
}

project(":core") {
    apply plugin: "java"


    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

For my android module:

dependencies {
    compile project(':BaseGameUtils')
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.anjlab.android.iab.v3:library:1.0.+@aar'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.android.support:recyclerview-v7:22.0.0'
    compile 'com.android.support:support-annotations:22.1.1'
    compile 'com.mopub.volley:mopub-volley:1.1.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.2.2@aar') {
        transitive = true;
    }
    compile('com.mopub:mopub-sdk:3.13.0@aar') {
        transitive = true
    }
}

android {
    buildToolsVersion "22"
    compileSdkVersion 20

    dexOptions {
        javaMaxHeapSize "2g"
    }

    defaultConfig {
        multiDexEnabled = true
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}
// needed to add JNI shared libraries to APK when compiling on CLI
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
    pkgTask.jniFolders = new HashSet<File>()
    pkgTask.jniFolders.add(new File(projectDir, 'libs'))
}
// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives() {
    file("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
        if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
        if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
        if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
        if (outputDir != null) {
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}
task run(type: Exec) {
    def path
    def localProperties = project.file("../local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        if (sdkDir) {
            path = sdkDir
        } else {
            path = "$System.env.ANDROID_HOME"
        }
    } else {
        path = "$System.env.ANDROID_HOME"
    }

    def adb = path + "/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.mygdx.game.android/com.mygdx.game.android.AndroidLauncher'
}
// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
    // need to specify Java source sets explicitely, SpringSource Gradle Eclipse plugin
    // ignores any nodes added in classpath.file.withXml
    sourceSets {
        main {
            java.srcDirs "src", 'gen'
        }
    }

    jdt {
        sourceCompatibility = 1.6
        targetCompatibility = 1.6
    }

    classpath {
        plusConfigurations += [project.configurations.compile]
        containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'
    }

    project {
        name = appName + "-android"
        natures 'com.android.ide.eclipse.adt.AndroidNature'
        buildCommands.clear();
        buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
        buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
        buildCommand "org.eclipse.jdt.core.javabuilder"
        buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
    }
}
// sets up the Android Idea project, using the old Ant based build.
idea {
    module {
        sourceDirs += file("src");
        scopes = [COMPILE: [plus: [project.configurations.compile]]]

        iml {
            withXml {
                def node = it.asNode()
                def builder = NodeBuilder.newInstance();
                builder.current = node;
                builder.component(name: "FacetManager") {
                    facet(type: "android", name: "Android") {
                        configuration {
                            option(name: "UPDATE_PROPERTY_FILES", value: "true")
                        }
                    }
                }
            }
        }
    }
}

Thanks.


#4

Hmm, that’s a bit strange. Here’s a sample build.gradle and my MainActivity that is compiling:

MainActivity.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.mopub.common.MoPub;
import io.fabric.sdk.android.Fabric;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new MoPub());
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

and my build.gradle from my App Module:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}


android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.twitter.mbonnell.mikesampleandroidmopub"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile('com.mopub.sdk.android:mopub:4.0.0@aar') {
        transitive = true;
    }
}

Can you try pulling out volley and updating to 4.0.0 and let me know how it goes?


#5

ok , I tried with:

dependencies {
    compile project(':BaseGameUtils')
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.anjlab.android.iab.v3:library:1.0.+@aar'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.android.support:recyclerview-v7:22.0.0'
    compile 'com.android.support:support-annotations:22.1.1'
    //compile 'com.mopub.volley:mopub-volley:1.1.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.2.2@aar') {
        transitive = true;
    }
    compile('com.mopub:mopub-sdk:4.0.0@aar') {
        transitive = true
    }
}

but no success:

this is what my mainActivity looks like:

    public class AndroidLauncher extends AndroidApplication implements
            GameHelperListener, ActionResolver, RequestHandler,PlayStorePurchaseListener, BillingProcessor.IBillingHandler {
    
        //private MoPubAdAdapter adAdapter;
        //private MoPubRewardedVideoListener rewardedVideoListener;
       // more members...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //MoPub.initializeRewardedVideo(this);
           // MoPub.onCreate(this);
           
            Fabric.with(this, new Crashlytics(), new MoPub());
    
            FacebookSdk.sdkInitialize(getApplicationContext());
            display = getWindowManager().getDefaultDisplay();
            //for admob
            //interstitialAd = new InterstitialAd(this);
            //interstitialAd.setAdUnitId(getResources().getString(R.string.ad_unit_id_interstitial));
            AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
            config.numSamples = 0;
            initialize(new MyGdxGame(this, this), config);
            ....
            }  
           ...
    }

#6

Thanks @axldotm, I’m wondering if there is a cached version of Mopub causing this issue.

Can you try running this and let me know how it goes?

./gradlew assemble --refresh-dependencies 

If that doesn’t do the trick, can you try moving maven.fabric.io above jCenter or mavenCentral?


#7

Thanks, I tried in my windows 7 cmd:

I changed maven { url 'https://maven.fabric.io/public' } position so that its above mavenCentral() and jcenter() as well and tried to clean my project but no success.


#8

Thanks @axldotm, as a quick test, can you comment out the MoPub init and then try running the Gradle command once more? Essentially, I want to get your Gradle build working once more to ensure that no cached or older copies of Mopub are being used.


#9

I commented MoPub init like so:
Fabric.with(this, new Crashlytics()/*, new MoPub()*/);
And ran again the gradle command and get a “buils successful” message. But after de-commenting MoPub init I was back to the same issue.


#10

ok, I changed this

compile('com.mopub:mopub-sdk:4.0.0@aar') {
        transitive = true
    }

to

compile('com.mopub.sdk.android:mopub:4.0.0@aar') {
            transitive = true;
        }

and it worked. Thank you for your time.


#11

Glad to hear that did the trick and happ coding!


#12

I have done all above things however having same problem.

Using

compile('com.mopub.sdk.android:mopub:4.3.0@aar') { transitive = true; }

Update:
I downloaded arr files from https://twittersdk.artifactoryonline.com/twittersdk/public/com/mopub/sdk/android/mopub/4.3.0/

and added them to project. When I saw decompiled code MoPub class was not extending Kit class however other plugin like Answers extends from Kit class. I think this is the issue.

Update 2:

I have decompiled com.mopub.sdk.android:mopub:4.0.0@aar file and it extends from Kit. public class MoPub extends Kit

So Please update this also com.mopub.sdk.android:mopub:4.3.0@aar