首页 > 解决方案 > 尝试安装 admob 时总是出现明显的合并错误

问题描述

我尝试在我的项目中添加 admob,并且我还尝试tools:replace="android:appComponentFactory"在清单中添加应用程序,但此时它给出了另一个错误:

tools:replace="android:appComponentFactory"代替<application>

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

    <uses-permission     android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


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


        </application>
    </manifest>

我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.admobessay"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-        optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'com.google.android.gms:play-services-ads:18.0.0'
}

清单合并失败:来自 [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 的属性 application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) 也存在于[androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory)。建议:将 'tools:replace="android:appComponentFactory"' 添加到 AndroidManifest.xml:9:5-30:19 的元素以覆盖。

标签: androidadmob

解决方案


问题是由于您尝试使用支持库以及 Android X 依赖项,这些依赖项在 Google Play 服务的最后(第 18 个)版本中实现。要解决此问题,您可以通过对您的 进行以下更改来迁移到 Android X AndroidManifest.xml

替换以下行:

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

用这些:

 implementation 'androidx.appcompat:appcompat:1.0.0'
 implementation 'androidx.constraintlayout:constraintlayout:1.1.2' 

此外,将这些标志设置到gradle.properties项目根目录中的文件可能会有所帮助:

android.useAndroidX=true
android.enableJetifier=true

推荐阅读