ClassNotFoundException: Didn't find class “com.google.android.gms.ads.AdView”

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I did a lot of research and this seems to be a common error for many users but for very different reasons. None of which I found worked for me.

I'm getting

java.lang.RuntimeException: Unable to start activity ComponentInfo{ [...]/[...].activities.StartActivity}: android.view.InflateException: Binary XML file line #173: Error inflating class [...].BannerAd     [...] Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.gms.ads.AdView     [...] Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView" on path: DexPathList[[zip file "/data/app/[...]-1.apk"],nativeLibraryDirectories=[/data/app-lib/[...]-1, /vendor/lib, /system/lib]]

I'm having the newest versions of ADT and SDK packages installed. I copied google-play-services_lib to my workspace and imported it as a Android project. I added it as a library to my app project. I checked everything under "Order and Export".

I'm having a banner_ad.xml:

And a BannerAd.java which I am using:

package [...];  import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.LinearLayout;  import [...].R; import [...].general.Settings; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView;   public class BannerAd extends LinearLayout {     public BannerAd(Context context, AttributeSet attrs) {         super(context, attrs);          if (!Settings.PRO) {             LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);             mInflater.inflate(R.layout.banner_ad, this, true);              AdView adView = (AdView) this.findViewById(R.id.adView);             AdRequest adRequest = new AdRequest.Builder().build();             adView.loadAd(adRequest);         }     } }

Could it have something to do with proguard? I have no idea, this is my proguard-project.txt file, however:

# To enable ProGuard in your project, edit project.properties # to define the proguard.config property as described in that file. # # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in ${sdk.dir}/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the ProGuard # include property in project.properties. # # For more details, see #   http://developer.android.com/guide/developing/tools/proguard.html  # Add any project specific keep options here:  # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { #   public *; #}  -keep class * extends java.util.ListResourceBundle {     protected Object[][] getContents(); }  -keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {     public static final *** NULL; }  -keepnames @com.google.android.gms.common.annotation.KeepName class * -keepclassmembernames class * {     @com.google.android.gms.common.annotation.KeepName *; }  -keepnames class * implements android.os.Parcelable {     public static final ** CREATOR; }

Any ideas what I could try to fix this?

Edit: Sometimes, I also get such output in the console (but not every time I compile, only sometimes):

[2014-07-24 12:49:05 - [...]] Dx  trouble processing: [2014-07-24 12:49:05 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000) ...while parsing com/google/android/gms/internal/mb.class ...while processing com/google/android/gms/internal/mb.class [2014-07-24 12:49:05 - [...]] Dx  trouble processing: [2014-07-24 12:49:05 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000) ...while parsing com/google/android/gms/internal/mc.class ...while processing com/google/android/gms/internal/mc.class [2014-07-24 12:49:05 - [...]] Dx  [...] [Lots of similar warnings here] [...] trouble processing: [2014-07-24 12:49:25 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000) ...while parsing com/google/ads/mediation/customevent/CustomEventAdapter$a.class ...while processing com/google/ads/mediation/customevent/CustomEventAdapter$a.class [2014-07-24 12:49:25 - [...]] Dx  trouble processing: [2014-07-24 12:49:25 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000) ...while parsing com/google/ads/mediation/customevent/CustomEventServerParameters.class ...while processing com/google/ads/mediation/customevent/CustomEventServerParameters.class [2014-07-24 12:49:25 - [...]] Dx 2786 warnings

回答1:

I did what @user1684030 said (upgraded Android SDK Build Tools; changed compiler to 1.7), but not worked for me. I've written the solution that I found in case it could help anyone with the same problem:

In the step 3 of Setting Up Google Play Services says:

In your app project, reference Google Play services library project. See Referencing a Library Project for Eclipse for more information on how to do this.

Note: You should be referencing a copy of the library that you copied to your development workspace―you should not reference the library directly from the Android SDK directory.

Solution: Don't do the copy manually. Instead just check the box in Eclipse that says "Copy projects into workspace"

I did it so and everything worked again.

Thanks to this StackOverFlow question: How to fix Google Play Services 2 Library install to Eclipse (Admob is part of Google Play Services)



回答2:

I finally solved my problems by

  • installing the newest version of Android SDK Build Tools (didn't realize that I did not have them yet)
  • switching from Compiler compliance level 1.6 to 1.7 (Window - Preferences - Java - Compiler - Compiler compliance level)

Thanks at everybody.



回答3:

This because ProGuard. To safely use ProGuard with Google Mobile Ads, add the following to your ProGuard config:

-keep public class com.google.android.gms.ads.** {    public *; }  -keep public class com.google.ads.** {    public *; }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!