android - 在 xml 中设置变量,就像在 android 中为 adMob 设置清单占位符一样
问题描述
文件中的清单占位符build.gradle
允许您指定可以在manifest
文件中使用的常量,就像这样。
manifestPlaceholders = [admob_app_id: "ca-app-pub-3940256099942544~3347511713", banner_id: "ca-app-pub-3940256099942544/6300978111"]
然后在清单文件中
<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 一直在抱怨,除非两者adSize
都adUnitId
在 xml 中声明。
解决方案
您可以使用该resValue()
方法使用您的横幅 id 创建一个字符串资源,然后在您的布局中引用它:
android {
...
buildTypes {
release {
resValue("string", "banner_id", "foo")
...
}
debug {
resValue("string", "banner_id", "bar")
...
}
}
}
ads:adUnitId="@string/banner_id"
推荐阅读
- windows - 什么可以禁用终端 ANSI 颜色代码?
- symfony - 无法显示动态图像-twig + symfony
- r - 如何在 r 中的同一个箱线图中绘制数据框的多列(使用 ggplot2)?
- python - Python Posix 路径不能连接 str
- git - .png 上的 Git 提交似乎混淆了文件
- bash - 在 git bash 上安装/运行 JQ
- c++ - MacOS 软件作为 Midi 设备
- c# - Visual Studio 的 ODAC 安装错误
- r - 如何根据 R 中的重叠连接字符串?
- python - 无法访问保存在 Django 模板上的 Quill 表单字段中的文本