android - 如何避免在 AdMob 的 XML 中复制/粘贴 adUnitId?用于测试和发布

标签 android admob

来自official example :

<com.google.android.gms.ads.AdView
  ...
  ads:adUnitId="ca-app-pub-3940256099942544/6300978111">

他们指定必须使用测试广告单元 ID。

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

如何将某处的测试和发布 ID 设置为常量?避免在每个 Activity 中复制/粘贴。
我应该为横幅创建布局吗?
但是,我仍然需要评论或手动替换 ID 以从测试切换到发布。

最佳答案

您可以使用字符串。创建两个分开的目录:

app/src/debug/res/values/strings.xml
app/src/release/res/values/strings.xml

将您的 banner_ad_unit_id 放在那里(用于调试和发布版本),然后在布局中使用它:

<com.google.android.gms.ads.AdView
  ...
  ads:adUnitId="@string/banner_ad_unit_id">

更多详情可以查看here

关于android - 如何避免在 AdMob 的 XML 中复制/粘贴 adUnitId?用于测试和发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207261/

相关文章:

android - 将 maven-release-plugin 添加到 Android Studio

android - Android 中的方向更改和 onCreate 方法

android - 使按钮在 Android SDK 中保持按下状态

Swift - 从 GADBannerView 扩展后出现无法识别的选择器错误

iphone - 将 Flurry AppCircle 广告与 AdMob 中介集成

android - AVG 杀毒软件删除了 adb.exe

android - 使用 google_sign_in 插件进行 flutter 时 Gradle 构建失败

java - Admob 没有填满所有宽度

java - 类未找到异常 : Didn't find class "com.google.android.gms.ads.AdView"

android - 测试时不要在模拟器上看到横幅广告。错误: Ad failed to load : 3