You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio集成AdMob报错:adUnitID属性未找到

解决AdMob集成中adUnitID属性未找到的AAPT错误

以下是针对该问题的具体解决方案:

  • 移除重复的XML命名空间
    你的布局文件中同时声明了xmlns:app和xmlns:ads两个指向http://schemas.android.com/apk/res-auto的命名空间,这会导致AAPT解析属性时出现冲突。保留一个即可,推荐统一使用app命名空间。

  • 修正属性名大小写
    AdMob AdView的XML属性正确名称是adUnitId(最后一个字母d为小写),而非你使用的adUnitID(大写D)。AAPT对XML属性名的大小写严格敏感,这是触发错误的核心原因之一。

  • 修正后的AdView布局示例
    调整后的布局代码如下:

    <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"
        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"
            app:adSize="BANNER"
            app: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>
    
  • 验证依赖与项目同步

    1. 执行File > Sync Project with Gradle Files确保依赖完全同步
    2. 执行Build > Clean Project后再Build > Rebuild Project,清除旧的编译缓存
    3. 确认app级build.gradle中的依赖无拼写错误:
      dependencies {
          implementation 'com.google.android.gms:play-services-ads:23.3.0'
      }
      
  • 检查字符串资源与Manifest配置

    1. 确认res/values/strings.xml中已正确定义banner_ad_unit_id和AdMob应用ID:
      <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
      <string name="admob_app_id">ca-app-pub-3940256099942544~3347511713</string>
      
    2. 确保AndroidManifest.xml的<application>标签内已添加应用ID元数据:
      <meta-data
          android:name="com.google.android.gms.ads.APPLICATION_ID"
          android:value="@string/admob_app_id"/>
      

内容的提问来源于stack exchange,提问作者Will Buffington

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 16:57:36