Error: cannot find symbol import com.google.android.gms.ads.InterstitialAd

Error: cannot find symbol import com.google.android.gms.ads.InterstitialAd

我已将 google AdMob 添加到项目中,但发现此错误: 错误:找不到符号 导入 com.google.android.gms.ads.InterstitialAd; ^ 符号:class InterstitialAd 位置:包 com.google.android.gms.ads

我找到了答案: 将此代码添加到 build.gradle(app) (在 android 标签中)


configurations.all {
  resolutionStrategy {
    force "com.google.android.gms:play-services-basement:17.0.0"
    force "com.google.android.gms:play-services-base:17.1.0"
    force "com.google.android.gms:play-services-stats:17.0.0"
    force "com.google.android.gms:play-services-gcm:17.0.0"
    force "com.google.android.gms:play-services-ads:19.7.0"
 }
}

如果您在一个库中使用过 admob,也将上面的代码添加到库 gradle

如果在将 com.google.android.gms:play-services-ads 依赖项更新到更新版本后出现此错误,请注意导入 com.google.android.gms.ads.InterstitialAd 自版本 19.7.0 以来已被弃用,并且已被完全删除。

新的导入应该是:

import com.google.android.gms.ads.interstitial.InterstitialAd;

要么恢复 com.google.android.gms:play-services-ads 到 19.7.0 之前的版本,要么按照此处的文档更新您的代码:https://developers.google.com/admob/android/interstitial.