在 Android Studio 中,如何配置 gradle 在创建签名发布 APK 时自动替换 AdMob 的 AdUnitId

Nfe*_*ger 7 android gradle admob

标准指南表明,这应该放置在您的 MainActivity.xml 中应显示 AdMob 横幅的位置:

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:background="@color/default_background"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />
Run Code Online (Sandbox Code Playgroud)

在我的 strings.xml 中,我定义了以下内容:

<string name = "banner_ad_unit_id" >
    ca-app-pub-3940256099942544/6300978111
    </string>
Run Code Online (Sandbox Code Playgroud)

在创建发布版本/签名的 apk 文件时,如何使用我的个人 AdMob ID 自动替换此值?

Roa*_*nmo 11

这是我针对此问题的解决方案,我知道它并非完美无缺,但效果很好。

在应用程序中,build.gradle您可以定义字符串资源buildTypes('...' 是其他不相关的定义):

build.gradle(应用程序)

android{
   ...
   buildTypes{

      debug {  //Debug build type
         ...
         //In here only TestAd keys (theese are public test keys free to use)
         resValue("string","mobileads_app_id","ca-app-pub-3940256099942544~3347511713")
         resValue("string","banner_ad_unit_id_no_1","ca-app-pub-3940256099942544/6300978111")
         resValue("string","banner_ad_unit_id_no_2","ca-app-pub-3940256099942544/6300978111")
         resValue("string","interstitial_ad_unit_no_1","ca-app-pub-3940256099942544/1033173712")
         ...
      }

      release {  //Release build type
         ...
         //In here only your own production Ad keys
         resValue("string","mobileads_app_id","/*YOUR REAL MOBILEADS KEY*/")
         resValue("string","banner_ad_unit_id_no_1","/*YOUR REAL AD UNIT ID KEY*/")
         resValue("string","banner_ad_unit_id_no_2","/*YOUR REAL AD UNIT ID KEY*/")
         resValue("string","interstitial_ad_unit_no_1","/*YOUR REAL INTERSTITIAL AD KEY*/")
         ...
      }              
   } 
}
Run Code Online (Sandbox Code Playgroud)

在您的strings.xml评论中,您应该评论您的广告键是在应用程序中定义的,build.gradle否则如果您不记得它,您可能会忘记...

字符串.xml:

<resources>
...
    <!--Note !! AdView keys are defined in app.gradle for debug and release buildTypes -->
...
</resources>
Run Code Online (Sandbox Code Playgroud)

现在,您可以参照您在应用程序中输入的字符串的定义build.gradle,因为他们写在文件中strings.xml,并正确的值取决于所选的调试调试代码时释放,释放释放的应用程序商店代码时发布:

fragment_layout.xml:

    <com.google.android.gms.ads.AdView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/banner_ad_unit_id_no_1"  
            android:id="@+id/fragment_no_1_adView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintTop_toTopOf="parent" />
Run Code Online (Sandbox Code Playgroud)

主活动.kt:

class MainActivity :AppCompatActivity(), FragmentOne.OnFragmentInteractionListener, ...
{
   ...
   override fun onCreate(savedInstanceState: Bundle?)
   {
      ...
      setContentView(R.layout.activity_main)

      //You can refer to the AdView code in string definition in a common way
      MobileAds.initialize(this,getString(R.string.mobileads_app_id))

      ... 

      interstitialAd = InterstitialAd(this)
      interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_no_1)

      ...

   }
}
Run Code Online (Sandbox Code Playgroud)

片段.kt:

class MyFragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?): 
    View? {

    adRequest = AdRequest
        .Builder()
        .build()

    stationListAdView.loadAd(adRequest)

    (activity as MainActivity).stationListToPriceInterstitialAd.loadAd(AdRequest.Builder().build())
Run Code Online (Sandbox Code Playgroud)

RG