Why does my android app crashes when I implement android.gms:play-services-ads:19.3.0

huangapple go评论66阅读模式
英文:

Why does my android app crashes when I implement android.gms:play-services-ads:19.3.0

问题

简而言之,我开发了一个简单的 Android Java 应用程序,希望使用 AdSense 服务实现一些广告。应用程序本身可以运行并完成其目的,但一旦我在 Gradle 移动模块中添加了实现 'com.google.android.gms:play-services-ads:19.3.0',应用程序就会崩溃。

我还尝试完成了所有可能必要的步骤,包括在 Android 清单中的 Android 名称和值元数据部分,以及在主活动中使用 MobileAds.initialize() 函数来设置库和广告。

我按照这里的步骤进行操作:

https://developers.google.com/admob/android/quick-start?hl=import_the_mobile_ads_sdk

但当我试图隔离问题时,我发现仅在 Gradle 移动脚本中包括实现 'com.google.android.gms:play-services-ads:19.3.0'(连同存储库中的 google() 规范)会导致应用程序从一开始就崩溃。它甚至无法进入应用。

我该怎么办?为什么会发生这种情况?如果能得到一些帮助,我会很高兴。

英文:

Long story short, I develop a simple android java app to which I wish to implement some ads using the adsense service. The app itself works and does its purpose, but once I add the implementation 'com.google.android.gms:play-services-ads:19.3.0' in the gradle mobile module, the app crashes.

I also tried to complete all the possible necessary steps, including the android name & value metadata part in the Android manifest, and also set up the ads with libraries and the function of MobileAds.initialize() in the Main Activity.

I followed the steps in here:

https://developers.google.com/admob/android/quick-start?hl=import_the_mobile_ads_sdk

but as i tried to isolate the problem, I saw that by only including the implementation 'com.google.android.gms:play-services-ads:19.3.0' in the gradle mobile script (together with the google() spec in the repository) the app crashes from the start. It doesn't even enter.

What can I do? Why does this happen? I'll be glad if I receive some help.

答案1

得分: 1

这份文档包含了以下部分:更新你的 AndroidManifest.xml

> 重要提示: 从 Google 移动广告 SDK 版本 17.0.0 开始,需要完成这一步骤。如果不添加这个 <meta-data> 标签,将导致崩溃并显示错误消息:Google 移动广告 SDK 初始化不正确。

你说过你只是在 build.gradle 文件中添加了依赖,所以你很可能还没有执行这一步,而这可能就是你遇到的问题。你需要去找到你的应用 ID,然后按照文档中的描述将其添加到 AndroidManifest.xml 文件中。

英文:

The documentation you've linked to includes this section: Update your AndroidManifest.xml

> Important: This step is required as of Google Mobile Ads SDK version 17.0.0. Failure to add this <meta-data> tag results in a crash with the message: The Google Mobile Ads SDK was initialized incorrectly.

You've said that all you've done is add the dependency to your build.gradle file, so you've probably not done this step and that's your issue. You will need to find your App ID and add it to AndroidManifest.xml, as described in the documentation.

答案2

得分: 0

使用元数据标签在清单文件中添加类似下面示例代码中的内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.google.android.gms.permission.AD_ID"/>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.WatchAndEarn"
        tools:targetApi="31"
        android:usesCleartextTraffic="true">

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3304471310252738~5701061203"/>

        <activity
            android:name=".Home"
            android:exported="false">
            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>
</manifest>

MainActivity 中使用如下所示的初始化方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });
}
英文:

Use the meta data tag in menifest like the given example code

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
&lt;uses-permission android:name=&quot;com.google.android.gms.permission.AD_ID&quot;/&gt;

&lt;application
    android:allowBackup=&quot;true&quot;
    android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
    android:fullBackupContent=&quot;@xml/backup_rules&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/Theme.WatchAndEarn&quot;
    tools:targetApi=&quot;31&quot;
    android:usesCleartextTraffic=&quot;true&quot;&gt;

    &lt;meta-data
        android:name=&quot;com.google.android.gms.ads.APPLICATION_ID&quot;
        android:value=&quot;ca-app-pub-3304471310252738~5701061203&quot;/&gt;

    &lt;activity
        android:name=&quot;.Home&quot;
        android:exported=&quot;false&quot;&gt;
        &lt;meta-data
            android:name=&quot;android.app.lib_name&quot;
            android:value=&quot;&quot; /&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;.MainActivity&quot;
        android:exported=&quot;true&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;

        &lt;meta-data
            android:name=&quot;android.app.lib_name&quot;
            android:value=&quot;&quot; /&gt;
    &lt;/activity&gt;
&lt;/application&gt;
&lt;/manifest&gt;

And in MainActivity use initialization like the example given below

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });
}

huangapple
  • 本文由 发表于 2020年9月6日 23:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63765620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定