1

In my application(main project) I am adding 3 existing projects marked as Library and all these 3 projects(library) requires GooglePlayServiceLibrary so that they can run AdMob and Interstitial ads.

I have successfully added these 3 library projects in my main project and they are working fine without AdMob and Interstitial ads (without adding GooglePlayServiceLibrary to these 3 projects), when I start the MainActivity of my library projects from my main projects Activity. But when I add GooglePlayServiceLibrary to these 3 projects and try to run them I getting errors.

XML code of library project:

<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="myadId" />

Java code of library project:

AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("").build();
adView.loadAd(adRequest);

Manifest file of library project:

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
<activity
    android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|smallestScreenSize|uiMode|screenLayout|screenSize"/>
<activity
    android:name="com.pkg.Interstitial"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.Transparent"  />

I am also attaching my LogCat:

07-15 04:12:41.630: E/AndroidRuntime(1301): FATAL EXCEPTION: main
07-15 04:12:41.630: E/AndroidRuntime(1301): Process: com.main.project, PID: 1301
07-15 04:12:41.630: E/AndroidRuntime(1301): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.project/com.library.project.MainActivity}: android.view.InflateException: Binary XML file line #187: Error inflating class com.google.android.gms.ads.AdView
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.os.Looper.loop(Looper.java:136)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at java.lang.reflect.Method.invoke(Method.java:515)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at dalvik.system.NativeStart.main(Native Method)
07-15 04:12:41.630: E/AndroidRuntime(1301): Caused by: android.view.InflateException: Binary XML file line #187: Error inflating class com.google.android.gms.ads.AdView
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.Activity.setContentView(Activity.java:1929)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at com.library.project.MainActivity.onCreate(MainActivity.java:75)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.Activity.performCreate(Activity.java:5231)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-15 04:12:41.630: E/AndroidRuntime(1301):     ... 11 more
07-15 04:12:41.630: E/AndroidRuntime(1301): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView" on path: DexPathList[[zip file "/data/app/com.main.project-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.main.project-1, /system/lib]]
07-15 04:12:41.630: E/AndroidRuntime(1301):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
07-15 04:12:41.630: E/AndroidRuntime(1301):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
07-15 04:12:41.630: E/AndroidRuntime(1301):     ... 21 more

Please let me know how can I use the these ads in my library projects.

Thanks in Advance

Abhishek Dhiman
  • 1,631
  • 6
  • 25
  • 38
  • `Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView"`. Do you have class with the name `AdView`? – Aniruddha Jul 15 '14 at 04:50
  • Refer http://stackoverflow.com/questions/10523703/error-inflating-class-com-google-ads-adview – Shailendra Madda Jul 15 '14 at 04:51
  • @Aniruddha AdView is inside the GooglePlayServiceLibrary, so it is already there and the code is working fine if I remove the is Library checkbox from the project. I am already getting this error when I use that project as a Library – Abhishek Dhiman Jul 15 '14 at 04:56
  • @shylendra please check my above comment – Abhishek Dhiman Jul 15 '14 at 04:58

3 Answers3

3

You need to create a libs/ folder in your Main Project and physically put the JAR file of GooglePlayServiceLibrary in the project, then add that Jar file to Java Build Path, you must Check Mark the SDK jar in Order and Export. This must be done in your main project and in manifest file add meta-data for GooglePlayServicesLibrary and also define activity name like this in our main projects manifest.

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

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

Also do not forget to add every permissions services receivers everything which is using in your library project, that all should be defined with their package name in the main project's manifest.

I was also facing same problem and this is how i resolve it ...cheers

sid
  • 1,116
  • 1
  • 10
  • 27
1

Since you're using Eclipse, you just need to right click on project that uses the library and select:

Properties -> Java Build Path -> Libraries -> Add External Jar

I would usually do it for all jars that are required by an application, but it looks like you want to add dependencies for each of three libs on GooglePlayServiceLibrary. If this is the case, check the link below. It can be done as well, moreover - duplicates of the same lib (GooglePlayServiceLibrary in your case) should be removed from the final project.

http://tools.android.com/recent/dealingwithdependenciesinandroidprojects

Because a project could depend on several libraries using the same jar files, the build system now looks at all the required jar files, detects duplicates coming from different libraries and removes them. This will prevent the dreaded “already added” error from dx.

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
0

Try to add this to your main project

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
<activity
    android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|smallestScreenSize|uiMode|screenLayout|screenSize"/>
<activity
    android:name="com.pkg.Interstitial"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.Transparent"  />

If any error, add GooglePlayServiceLibrary to your main project as well.

Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37