android - 在 xml 中设置变量,就像在 android 中为 adMob 设置 list 占位符一样

标签 android gradle android-gradle-plugin admob build.gradle

build.gradle 中的 list 占位符文件允许您指定可以在 manifest 中使用的常量。像这样的文件。

  manifestPlaceholders = [admob_app_id: "ca-app-pub-3940256099942544~3347511713", banner_id: "ca-app-pub-3940256099942544/6300978111"]

然后在 list 文件中

 <meta-data
   android:name="com.google.android.gms.ads.APPLICATION_ID"
   android:value="${admob_app_id}" />

这在布局 xml 文件的情况下似乎不起作用

    <com.google.android.gms.ads.AdView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/publisherAdView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    ads:adSize="BANNER"
    ads:adUnitId="${banner_id}" />

有没有办法可以动态更改 banner id基于构建配置。我尝试过以编程方式执行此操作,但 admob throws 一直在提示,除非这两个 adSizeadUnitId在 xml 中声明。

最佳答案

您可以使用the resValue() method使用您的横幅 id 创建一个字符串资源,然后在您的布局中引用它:

android {
  ...
  buildTypes {
    release {
      resValue("string", "banner_id", "foo")
      ...
    }
    debug {
      resValue("string", "banner_id", "bar")
      ...
    }
  }
}
ads:adUnitId="@string/banner_id"

关于android - 在 xml 中设置变量,就像在 android 中为 adMob 设置 list 占位符一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677187/

相关文章:

java - 安卓工作室错误 : "Invoke-customs are only supported starting with Android O"

android - Gradle Build for lib:androidJavadocs失败

android - GSON 动态类绑定(bind)

java - Application 子类中的 onCreate 方法不会在每次应用程序打开时调用

java - 用于查看网页的 Intent 过滤器

intellij-idea - 运行 integTests 时的 intellij idea 14 和 gradle 问题

gradle - 扩展现有的 Gradle 任务并覆盖配置

android - 签名 apk 中的 ClassNotFoundException

android - 为什么 gradle 说它无法解析 "com.android.support:support-annotations:23.1.1"?

java - 如何获得比特率,采样率和没有。 android中音频文件的 channel 数