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 一直在提示,除非这两个 adSize
和 adUnitId
在 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/