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

时间:2015-03-30 12:57:42

标签: 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" />

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

<string name = "banner_ad_unit_id" >
    ca-app-pub-3940256099942544/6300978111
    </string>

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

1 个答案:

答案 0 :(得分:0)

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

在应用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*/")
         ...
      }              
   } 
}

在您的strings.xml中,您应该评论说,您的广告键是在应用build.gradle中定义的,否则,如果您忘记了它,则可能会迷失方向...

strings.xml:

<resources>
...
    <!--Note !! AdView keys are defined in app.gradle for debug and release buildTypes -->
...
</resources>

您现在可以参考在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" />

MainActivity.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)

      ...

   }
}

fragment.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())

RG