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
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