如何避免 AdMob XML 中的 adUnitId 的 copy/paste?用于测试和发布

How to avoid copy/paste of the adUnitId in XML for AdMob? For both test and release

来自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.
中的 copy/paste 我应该为横幅创建布局吗?
但是,我仍然需要评论或手动替换 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