1

I'm using new Android Studio. I've done an application and works fine, now I've to add AdMob sdk. so I put the jar in 'libs' folder and right-click "add as library". I run the project on my smartphone but the application crashes on startup.

How can i solve this? I think it'll be something wrong with the import of AdMob sdk.

Thank you!

This is the xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.application"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        //MY ACTIVITY
    </activity>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

    <meta-data android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" android:value="true"/>
    <meta-data android:name="ADMOB_PUBLISHER_ID" android:value="a151964f48b17a7"/>

</application>

This is the layout xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            tools:context=".MainActivity">

<com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="MY_UNIT_ID"
        ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

And this is the logcat

5-20 15:38:00.835  26121-26121/com.mnt.crudelibestmoments     E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mnt.crudelibestmoments/com.mnt.crudelibestmoments.MainActivity}: android.view.InflateException: Binary XML file line #195: Error inflating class com.google.ads.AdView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
    at android.app.ActivityThread.access$600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4448)
    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:823)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #195: Error inflating class com.google.ads.AdView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
    at android.app.Activity.setContentView(Activity.java:1835)
    at com.mnt.crudelibestmoments.MainActivity.onCreate(MainActivity.java:40)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
    ... 11 more
    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)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)

If i hide the AdMob code in layout xml, the application works properly

thank you!

Nerd
  • 175
  • 1
  • 3
  • 8

6 Answers6

7

it's a bit tricky but here is the solution:

The current preview release doesn't edit the build.gradle file, even if you have added the admob library to the dependencies in the menu. What you can do is to add the dependency manually, it's very easy:

double click on the file "build.gradle" on the left in the navigation menu. Add the following line:

compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')

It should look like:

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

    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}
Chris623
  • 2,464
  • 1
  • 22
  • 30
  • I did what you said, but I got the same error again. Rebuilding the project afterward did solve the problem. – MrIveck Nov 05 '13 at 07:47
1

Right click your project and go to Properties -> Java Build Path. In Libraries add the AdMob SDK jar, and in Order and Export check the AdMob SDK library. Then clean and re-run your application.

Previously, putting 3rd party libraries in the libs/ folder was sufficient for the Android build to bundle these libraries when compiling your app. But since the release of v22 of the Android tools, you now have to explicitly add the library to your build path and export it.

Eric Leichtenschlag
  • 8,881
  • 1
  • 28
  • 28
  • Hi Eric, i can't do that.. I'm using the new Android Studio 0.1 and if I right click on the project I can't find properties and java build path. How did you do that? Thank you – Nerd May 25 '13 at 08:27
  • Ah, I'm not familiar with Android Studio. Maybe [this](http://stackoverflow.com/questions/16588389/android-libraries-in-android-studio) will help. It looks like the reason for this error is that the AdMob library is not getting included when building your .apk file. You'll need to figure out how to make that work in Android studio. – Eric Leichtenschlag May 28 '13 at 17:14
  • This is the solution for eclipse, not for Android Studio Preview. The correct solution can be seen in my post above. – Chris623 Jun 12 '13 at 18:49
1

I was the same and achieve fix, I have done just the same as you and I miss you just do the following:

open a terminal (windows or linux) and go to the root of your project, eg AndroidStudioProjects / MyProject

Run the command:

Linux(is the file gradlew): ./gradlew clean

Windows(is the file gradlew.bat): gradlew clean

Now goto: Build > Rebuild Project

That's it, enjoy!!!

Sorry for the bad English

picharras
  • 11
  • 3
0

I got same problem.I changed Android.jar to 4.2.2. Now it works fine in both emulator and phone

0

This may help you integrate admob with your application but google is not going to accept any updated or new apps which use this type of admob integration following 1 August 2014.

https://developers.google.com/mobile-ads-sdk/download#downloadandroid

Instead I recommend you to use Google Play Services version as suggested in above document.

gurkan
  • 3,457
  • 4
  • 25
  • 38
0

You are most likely using a AdView layout with package name com.google.ads.AdView. Change it to:

 <com.google.android.gms.ads.AdView
        xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/ad"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        />

I am also using Android Studio, with build.gradle dependency set like this:

dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:4.3.23'
}

Moving forward,we are not required to manually add libs that already have Maven coordinates to point to, so do NOT do this.

dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
}
Chinedu
  • 71
  • 3