gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:02:57 26 4
gpt4 key购买 nike

标准指南显示,应将其放置在 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?

最佳答案

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

在应用程序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 中编写的,正确的值为在调试代码时选择debugrelease,在为应用商店发布代码时选择releaserelease:

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

关于android - 在Android Studio中,如何配置gradle在创建签名发布APK时自动替换AdMob的AdUnitId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29346871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com