ClassNotFoundException:未找到类“com.google.android.gms.ads.AdView”

时间:2014-07-24 10:03:46

标签: java android admob google-play-services

我做了很多研究,这似乎是许多用户的常见错误,但原因各不相同。我发现这些都不适合我。

我正在

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

我安装了最新版本的ADT和SDK软件包。我将google-play-services_lib复制到我的工作区并将其作为Android项目导入。我将它作为库添加到我的应用程序项目中。我检查了" Order and Export"。

下的所有内容

我有一个banner_ad.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|top"
    android:orientation="vertical" >

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="[...]" />

</LinearLayout>

我正在使用的BannerAd.java:

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);
        }
    }
}

它与proguard有关吗?我不知道,这是我的proguard-project.txt文件,但是:

# 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;
}

我可以尝试解决这个问题吗?

编辑:有时,我也在控制台中获得这样的输出(但不是每次我编译时,有时候都是这样):

[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

3 个答案:

答案 0 :(得分:2)

我终于通过

解决了我的问题
  • 安装最新版本的Android SDK Build Tools(没有 意识到我还没有它们)
  • 从编译器合规性级别1.6切换到1.7(窗口 - 首选项 - Java - 编译器 - 编译器合规级别)

谢谢大家。

答案 1 :(得分:2)

我做了@ user1684030所说的(升级了Android SDK Build Tools;将编译器更改为1.7),但对我没有用。我已经编写了我找到的解决方案,以防它可以帮助任何有同样问题的人:

Setting Up Google Play Services的第3步说:

  

在您的应用项目中,参考Google Play服务库项目。   有关详细信息,请参阅参考Eclipse的库项目   怎么做。

     

注意:您应该引用您复制的库的副本   到您的开发工作区 - 您不应该引用该库   直接来自Android SDK目录

解决方案:请勿手动执行复制。相反,只需选中Eclipse中显示“将项目复制到工作区”

的框

我这样做了,一切都恢复了。

感谢StackOverFlow的这个问题:How to fix Google Play Services 2 Library install to Eclipse (Admob是Google Play服务的一部分)

答案 2 :(得分:2)

这是因为ProGuard。要将ProGuard安全地用于Google移动广告,请将以下内容添加到ProGuard配置中:

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

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