Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio AdMob

I've switched over from Eclipse to use Android Studio and have everything working, except when trying to incorporate AdMob into my project. I had the code set up correctly from my previous project, so it's a matter of telling Android Studio to build with it.

I've been reading a lot of questions about adding the .jar into the libs folder, except I think the newest version of Android Studio changed the layout. So I just dragged it into .idea > libraries and then Right-Clicked and 'Add As Library'.

Within Project Structure, I have two modules, and -. Under -, it shows all the dependencies listed which includes support-v4-18.0.0 and GoogleAdMobAdsSdk-6.4.1, marked as Export and Compile for Scope.

Within the Preview window in Android Studio, it shows the AdView correctly, but when running it it crashes. I've tried using 'gradlew clean' and rebuilding the project numerous times without success.

Currently, the project works correctly unless I have the AdView on any layout. If I do have it on a page, I get "java.lang.ClassNotFoundException: com.google.ads.AdView" pointing to the line where I use an inflater to inflate my layout.

Callstack:

    android.view.InflateException: Binary XML file line #155: Error inflating class com.google.ads.AdView
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at com.awestruck.pokedex.Display_Main.onCreateView(Display_Main.java:53)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
        at android.view.View.measure(View.java:15279)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
        at android.view.View.measure(View.java:15279)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15279)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
        at android.view.View.measure(View.java:15279)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2271)
        at android.view.View.measure(View.java:15279)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2091)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1253)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1483)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1151)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4661)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
        at android.view.LayoutInflater.createView(LayoutInflater.java:552)

Which points to the line:

        v = inflater.inflate(R.layout.display_main, container, false);

build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'



repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.0"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 18
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:18.0.0'
    compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
}

Thanks for any help.

like image 539
Chris Avatar asked Feb 02 '26 06:02

Chris


1 Answers

Problem solved, after awhile, I managed to get it.

For anyone looking to figure out this issue, create a new libs folder and put the GoogleAdMobAdsSdk-6.4.1.jar within it. And make sure your dependencies is:

dependencies {
    compile 'com.android.support:appcompat-v7:18.0.0'
    compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
}

Although, I'm unsure exactly where the folder needs to go, I just copy-pasted the same folder everyone and then ran:

gradlew clean

Then hopefully that should work.

like image 71
Chris Avatar answered Feb 04 '26 22:02

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!