好吧,我已经用头撞桌子两天试图解决这个问题了,但我却不知所措。希望这是简单的事情,但不确定。
我有一个使用 Google AdMob 的 Android 项目。我已遵循 AdMob 开发者网站上的所有说明。构建我的项目时失败并出现以下错误:
任务:app:processDebugResources 失败 AGPBI: {"kind":"error","text":"Android 资源链接失败","sources":[{"file":"/Users/user12345/Developer/Android Studio Projects/MyApp/app/src/ main/res/layout/activity_main.xml","position":{"startLine":213}}],"original":"错误: /Users/user12345/Developer/Android Studio Projects/MyApp//app/src/ main/res/layout/activity_main.xml:214:AAPT:错误:找不到属性 adUnitID(又名 com.someorg.MyApp.debug:adUnitID)。 ","工具":"AAPT"}
任务“:app:processDebugResources”执行失败。
执行 com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask$TaskAction 时发生故障 Android资源链接失败 错误:/Users/user12345/Developer/Android Studio Projects/MyApp/app/src/main/res/layout/activity_main.xml:214:AAPT:错误:属性 adUnitID (又名 com.someorg.myapp.debug:adUnitID)不是找到了。
显然它指向 XML 文件。在我的 XML 中,这就是我所拥有的:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity">
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="0dp"
android:layout_height="75dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#000000"
ads:adSize="BANNER"
ads:adUnitID="@string/banner_ad_unit_id"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
</com.google.android.gms.ads.AdView>
</androidx.constraintlayout.widget.ConstraintLayout>
由于某种原因,资源链接器找不到 adUnitID。这是我的应用程序级 build.gradle 文件:
android {
namespace 'com.someorg.myapp'
compileSdk 34
defaultConfig {
applicationId "com.someorg.myapp"
minSdkVersion 24
targetSdkVersion 34
versionCode 7
versionName '1.0.7'
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
buildConfig true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
signingConfig signingConfigs.debug
debuggable false
}
debug {
applicationIdSuffix ".debug"
debuggable true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.11.0"))
implementation 'com.google.android.gms:play-services-maps:18.2.0'
implementation 'androidx.activity:activity:1.9.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.squareup.retrofit2:converter-jackson:2.7.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.3'
implementation 'com.fasterxml.jackson.core:jackson-core:2.10.3'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.10.3'
implementation 'com.android.support:multidex:1.0.3'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19'
implementation 'com.google.android.gms:play-services-ads:23.3.0'
implementation 'com.google.android.gms:play-services-ads-base:23.3.0'
}
}
这是 Manifest.XML 文件中包含以下内容的部分:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-################~##########"/>
(我已经编辑了上面的app-id)
我现在不知道该怎么办。我可能会尝试构建一个新的示例项目,看看是否可以让 adview 正常工作。
Per Mike M. - 问题出在这行 XML 中的大写 D:
ads:adUnitID="@string/banner_ad_unit_id"
我改为:
ads:adUnitId="@string/banner_ad_unit_id"
这解决了问题。