I am trying to add the MoPub SDK to my application. I try to import and add the library in Android Studio, but I do not see any ad in my app.

I used Eclipse to add the library originally, but I am trying Android Studio because it is the new Google-supported tool now.

How do I add MoPub's SDK to my app using Android Studio?

有帮助吗?

解决方案

Maybe Android studio has updated, I couldn't solve it according to abaar or pyus13's answer.

This answer is based on Android Studio 0.6.1.

  1. Unzip the mopub-sdk.zip that you downloaded

  2. Open project structure(File -> Project Structure) in Android Studio

  3. Click the '+' button on the upper left to add a new module

  4. You can see the New Module wizzard, choose Import Existing Project and click next

  5. Choose the mopub-sdk folder you just unzipped

  6. Click next and finish. Now you can see that Android Studio import the mopub-sdk into your project. And a settings.gradle file has been added into your project

  7. Open settings.gradle, and add a line include ':'

  8. Open the build.gradle file of your project, in the dependencies entry, add compile project('mopubsdk') (mopubsdk is the project name you just added)

  9. Open project structure again, you can see the mopubsdk module on the left, click on it, then choose Dependencies tab on the top, change com.android.support:support-v4:+'s scope from Compile to Provided

Now it's done. It works for me.

其他提示

I couldnt get pyus13's answer to work so here's how I did it in gradle. Put mopub-sdk folder in your projects libraries folder, which should be on the same level as your project's gradle folder. Put this build.gradle in your mopub-sdk

apply plugin: 'android-library'

android {
compileSdkVersion 19
buildToolsVersion '19.0.1'

sourceSets {
    main {
        java.srcDirs = ['src/main/java']
        res.srcDirs = ['res']
        manifest.srcFile 'AndroidManifest.xml'
    }
}
}

dependencies {
compile 'com.android.support:support-v4:19.0.0'
// compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar') //not relevant anymore
compile files('libs/MMSDK.jar')
}

then add compile project(':libraries:mopub-sdk') to the build.gradle dependencies at your app level and include ':app', ":libraries:mopub-sdk" to your settings.gradle on the level above that.

The above build.gradle is for the full bundle which includes millenial media and admob. Millenial media might not work because android studio doesn't seem to be recognizing the videoplayer activity definition in my android manifest. admob is working ok but you might run into a conflict if you have google play services in your app as admob is now packaged in there. If you have either of these problems download the plain mopub bundle and lose the compile files lines.

EDIT: integrate google play services like it says on the github wiki then put google admob banner.java and inserstitial.java into your own mopub src directory next to your own.

You can integrate it by simply edit 2 files, as shown in their GitHub repo README:

build.gradle () ---- Project one add jcenter() inside repositories

repositories {
    jcenter()
}

build.gradle () ---- Module one add the following dependencie

dependencies {
       compile fileTree(include: ['*.jar'], dir: 'libs')
       testCompile 'junit:junit:4.12'

       ........

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

Sync / rebuild project and you are done!

Steps to add Mopub or any library project as jar/maven dependency in Android Studio(0.4.3) :

1. First search if their is any maven central repository available for your library if it is then no need to download anything in your local machine. For example actionbarsherlock is available on maven so you just need to add this in your module's build.gradle file under dependencies section.

  compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'

You can search and add maven dependency from File>Project Structure > Modules > Dependency Tab > "+" Sign > Maven Dependency or search.maven.org .

2. If it is not available as maven dependency(like in case of mopub) you need to download the whole library project in to your local machine. As mopub is using maven and there is no gradle based project repository available. The process is little bit panic.

First you have to create a jar file of your mopub SDK using Ant/Maven (I have seen there is no resources in the library, only java code so jar will work ).

Now create a directory inside your Main Module's directory and call it libs(same as in eclipse) and copy your mopub jar here.

Go to File > Project Structure or right Click on project and Go to Open Module Settings and Select Modules in opened Window .

Now Select the module for which you want to add the dependency from left panel and click on Dependency tab on right panel.

Click on Green "+" sign button, placed right to dependency window in a vertical bar and choose "File Dependency"

It will show your libs directory there in a windows with your jar, select the jar and click Ok.

Doing this will just add this line in your module's build.gradle file under dependency so you can verify.

   compile files(libs/yourmopublib.jar)

You can achieve the same by adding this line manually in build.gradle file as well, I like to add it manually.

Sync Your Project with Gradle.

Done!

Note : Things can be changed for later releases.

Currently, I think the easiest way to integrate mopub with AS is to use fabric.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top