找不到以下类: - com.google.android.gms.ads.AdView

时间:2016-11-16 04:27:08

标签: java android admob

我收到此错误:

  

找不到以下课程:
   - com.google.android.gms.ads.AdView

我已将Google Play服务更新至第37版,将Google存储库更新至第38版。

同样在MainActivity方法的OnCreate()课程中,我收到Cannot resolve symbol 'MobileAds'错误。我知道它是由缺少导入引起的,但我没有提供要导入的类。

MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.example.gateway.myapp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context="com.example.gateway.myapp.Class"
    android:orientation="vertical">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</LinearLayout>

build.gradle(模块):

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.gateway.myapp"
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.google.firebase:firebase-core:9.8.0'
}
apply plugin: 'com.google.gms.google-services'

build.gradle(项目):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'com.google.gms:google-services:3.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

2 个答案:

答案 0 :(得分:1)

在您的gradle文件中添加com.google.firebase:firebase-ads

dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])

        compile 'com.google.firebase:firebase-ads:9.8.0'
    }

Google Mobile Ads SDK

中更新(包括Firebase和SDK)移动广告

答案 1 :(得分:0)

Admobs现在是Google Play服务提供的 Firebase SDK和Google移动广告的一部分。它已不再可用于独立SDK。

Reference linkMigrate from the Google project.