【问题标题】:I have an gradle error everytime I run the app每次运行应用程序时都会出现 gradle 错误
【发布时间】:2018-11-07 15:53:08
【问题描述】:

我正在使用一个 android 项目,每次运行应用程序时都会出现这种类型的错误:

清单合并失败:属性 application@appComponentFactory 值=(android.support.v4.app.CoreComponentFactory)来自 [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 也出现在 [androidx.core:core:1.0.0-alpha1] AndroidManifest.xml:22:18-86 值=(androidx.core.app.CoreComponentFactory)。建议:加 'tools:replace="android:appComponentFactory"' 到元素 在 AndroidManifest.xml:10:5-44:19 处进行覆盖。

即使我尝试按照它告诉我的方式进行操作,也会出现另一个错误。

这是 build.gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.community.jboss.visitingcard"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
buildToolsVersion '28.0.3'
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
//the intro library
implementation 'com.github.msayan:tutorial-view:v1.0.6'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
implementation "com.android.support:preference-v7:28.0.0"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
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'
}

这是 Manifest 文件:

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

<!--
    TODO: Add location/GPS access permissions
    TODO: Change the Label of the Activities according to the Activity's 
context
    TODO: Change App Icon .i.e., ic_launcher
-->
<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=".LoginActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".VisitingCard.VisitingCardActivity" />
    <activity android:name=".IntroScreens.SliderActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <!-- TODO: Add Google Maps API key here -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".Maps.MapsActivity"
        android:label="@string/title_activity_maps"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
    <activity android:name=".VisitingCard.ViewVisitingCard" />
    <activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_settings" />
    <activity android:name=".IntroActivity"></activity>
</application>

</manifest>

【问题讨论】:

    标签: android gradle manifest


    【解决方案1】:

    在您的清单中添加以下行:

    tools:replace="android:appComponentFactory"
    

    像这样:

    <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"
    tools:replace="android:appComponentFactory>
    
    //Your code here
    
    </application>
    

    【讨论】:

    • 是的,当我尝试它时,它说 Manifest 合并失败并出现多个错误,请参阅日志
    • 您是否在清单中添加了这一行? tools:replace="android:appComponentFactory"
    【解决方案2】:

    您正在使用两个 appcompat 依赖项,一个来自 support 命名空间,另一个来自 androidx 命名空间。理想的解决方案是将您的项目完全移动到 androidx 并删除下面提到的依赖项,

     implementation 'com.android.support:appcompat-v7:28.0.0'
    

    你可以使用稳定版的 andoid x appcompat 库

    androidx.appcompat:appcompat:1.0.0 instead of androidx.appcompat:appcompat:1.0.0-alpha1'
    

    【讨论】:

    • 但我是否也应该删除实现 'com.android.support:design:28.0.0' 和
    • 以及实现“com.android.support:preference-v7:28.0.0”
    • 您已部分移至 androidx。用这个 androidx.preference:preference:1.0.0 替换 com.android.support:preference-v7:28.0.0
    • 将 com.android.support.design:28.0.0 替换为 com.google.android.material:material:1.0.0-rc01 更多信息请参考developer.android.com/jetpack/androidx/migrate#migrate
    • 我用这些 gradle 构建了应用程序,但现在我在所有其他文件上都有错误
    【解决方案3】:

    您似乎决定在您的项目中使用 androidX,但同时使用 appCompatandroidX 是不正确的。
    最好使用androidX的库,去掉appCompat的。
    如果你坚持使用androidX 删除build.gradle 文件中的implementation 'com.android.support:appcompat-v7:28.0.0' 或者删除一个androidX,否则。

    有关您的错误的更多信息: 您可以打开“清单”文件并转到合并选项卡。在文件的底部,您可以看到有什么问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 2020-08-13
      相关资源
      最近更新 更多