Standard guidance shows that this should be placed where the AdMob banner should be displayed in you MainActivity.xml:
<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" />
In my strings.xml I have defined the following:
<string name = "banner_ad_unit_id" >
ca-app-pub-3940256099942544/6300978111
</string>
How do I get this value automatically replaced with my personal AdMob ID when creating a release build/signed apk file?
This is my solution for this problem, and I know it is not flawless, but it works quite well.
In app build.gradle
you can define string resources upon buildTypes
('...' are other definitions not related):
build.gradle (app):
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*/")
...
}
}
}
In your strings.xml
you should comment that your Ad keys are defined in app build.gradle
or you may loose track if you don't remember it...
strings.xml:
<resources>
...
<!--Note !! AdView keys are defined in app.gradle for debug and release buildTypes -->
...
</resources>
You can now reference to the string definitions you have entered in the app build.gradle
file as they were written in the strings.xml
, and the correct value is selected upon debug release when debugging code, and release release when releasing code for the app store:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With