Android Admob:无法导入某些类 [英] Android Admob: Cannot import some classes
问题描述
我已经在几个项目中使用过 Admob,但这次遇到了一个奇怪的问题.
I have used Admob in several projects already, but this time I encounter a strange problem.
我遵循了官方纪录片(此处)中的所有说明,并且一切都基本正常,至少测试广告是这样.
I followed all instructions from the official documentary (here), and everything basically works, at least the test ads.
但我遇到了问题,就是有些类无法导入.
But I've encountered the problem, that some classes just can't be imported.
例如,当我从文档中获取此代码时:
For example, when I take this code from the documentation:
MobileAds.initialize(act, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
我的项目就是找不到OnInitializationCompleteListener
,也找不到initializationStatus
.在我以前的项目中从来没有这样.MobileAds
虽然可以完美地导入.(我所做的只是省略了不工作的部分:MobileAds.initialize(act);
)
My project just cannot find OnInitializationCompleteListener
, nor initializationStatus
. This was never like that in my older projects. MobileAds
though can be imported just perfectly. (What I did to make it work is just omit the not working part: MobileAds.initialize(act);
)
其他一些类也有同样的问题,例如LoadAdError
、AdError
或 ResponseInfo
.它们只是无法导入.
Same problem with some other classes as well, e.g. LoadAdError
, AdError
, or ResponseInfo
.
They just cannot be imported.
Android Studio 没有给具有该名称的类提供任何可以导入的提示,当我添加 import XXX
并从官方文档中获取 XXX 作为完整的类名(带包)时,它找不到那个.
Android Studio does not give any hints to classes with that name that could be imported and when I add import XXX
and take XXX from the official documentation as the full class name (with packages), it cannot find that.
这很奇怪,首先,这在我的项目中从未发生过,其次,因为只有一些导入找不到.
This is very odd, since first of all, this has never happened before in my projects, and secondly, because only some imports cannot be found.
我的应用程序构建梯度
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "..."
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
// proguard stuff
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'com.google.android.material:material:1.2.1'
// Firebase Stuff
...
// ads
implementation 'com.google.android.gms:play-services-ads:17.1.0'
推荐答案
实际上,您必须添加对 build.gradle 的依赖.我在下面写了依赖
actually, you have to add a dependency on build.gradle. I wrote the dependency below
implementation 'com.google.android.gms:play-services-ads:20.1.0'
这篇关于Android Admob:无法导入某些类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!