113

我已经成功配置了android-P SDK环境。当我尝试使用 android 设计支持库时,我遇到了项目构建错误。项目配置如下:

IDE:3.2 Canary 17 目标 API:28 编译 API:28

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.app.navigationpoc"
        minSdkVersion 21
        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'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'

    implementation 'com.android.support:design:28.0.0-alpha3'
    implementation 'com.android.support:cardview-v7:28.0.0-alpha3'
}

构建失败的错误是:

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

4

18 回答 18

97

您可以使用以前的 API 包版本的工件或新的 Androidx,不能同时使用两者。

如果您想使用以前的版本,请将您的依赖项替换为

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.1'

    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.android.support:design:28.0.0-alpha3'
    implementation 'com.android.support:cardview-v7:28.0.0-alpha3'
}

如果你想使用 Androidx:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'

    implementation 'com.google.android.material:material:1.0.0-alpha3'
    implementation 'androidx.cardview:cardview:1.0.0-alpha3'
}
于 2018-06-11T15:05:09.760 回答
60

重要更新

Android 之后不会更新支持库28.0.0

这将是 android.support 打包下的最后一个功能版本,鼓励开发人员迁移到 AndroidX 1.0.0

所以使用库AndroidX

  • 不要在项目中同时 使用SupportAndroidX
  • 您的库模块或依赖项仍然可以具有支持库。 Androidx Jetifier 将处理它。
  • 使用稳定版本androidx或任何库,因为 alpha、beta、rc 可能存在您不希望随应用一起发布的错误。

在你的情况下

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
}
于 2018-09-28T13:02:34.883 回答
44

添加这个:

tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"

到您的清单应用程序

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    tools:replace="android:appComponentFactory"
    android:appComponentFactory="whateverString">

希望能帮助到你

于 2018-06-21T09:24:47.640 回答
18

我使用了该选项:

使用 Android Studio 3.2 及更高版本,您可以通过从菜单栏中选择 Refactor > Migrate to AndroidX 快速迁移现有项目以使用 AndroidX。

https://developer.android.com/jetpack/androidx/migrate

于 2018-11-08T09:44:08.783 回答
12

Google 引入了新的AndroidX依赖项。您需要迁移到 AndroidX,这很简单。

我将所有依赖项替换为 AndroidX 依赖项

旧的设计依赖

implementation 'com.android.support:design:28.0.0'

新的 AndroidX 设计依赖项

implementation 'com.google.android.material:material:1.0.0-rc01'

你可以在这里找到 AndroidX 依赖项 https://developer.android.com/jetpack/androidx/migrate


自动 AndroidX 迁移选项(在 android studio 3.3+ 上支持)

通过从菜单栏中选择 Refactor > Migrate to AndroidX 来迁移现有项目以使用 AndroidX。

于 2019-02-16T15:25:16.473 回答
6

androidX 的设计支持库是implementation 'com.google.android.material:material:1.0.0'

于 2019-06-24T10:21:25.993 回答
6

1.将这些代码添加到您的 app/build.gradle:

configurations.all {
   resolutionStrategy.force 'com.android.support:support-v4:26.1.0' // the lib is old dependencies version;       
}

2.修改sdk和tools版本为28:

compileSdkVersion 28
buildToolsVersion '28.0.3'
targetSdkVersion  28

2.在您的 AndroidManifest.xml 文件中,您应该添加两行:

<application
    android:name=".YourApplication"
    android:appComponentFactory="anystrings be placeholder"
    tools:replace="android:appComponentFactory"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

感谢@Carlos Santiago 的回答: API 28 (P) 的 Android 设计支持库不起作用

于 2018-10-09T10:31:44.950 回答
5

首先,您应该查看gradle.properties并且这些值必须是true。如果你看不到它们,你必须写。

android.useAndroidX = true
android.enableJetifier = true

之后,您可以在build.gradle (Module: app)中使用 AndroidX 依赖项。此外,您必须检查compileSDKVersiontargetVersion。它们应该至少为 28。例如,我使用的是 29。

因此,一个 androidx 依赖示例:

implementation 'androidx.cardview:cardview:1.0.0'

但是要小心,因为一切都不是从 androidx 开始的,比如cardview 依赖。例如,旧的设计 依赖是:

implementation 'com.android.support:design:27.1.1'

但是新的设计 依赖是:

implementation 'com.google.android.material:material:1.3.0'

RecyclerView 是:

implementation 'androidx.recyclerview:recyclerview:1.1.0'

因此,您必须仔细搜索和阅读。

于 2020-04-06T10:00:10.927 回答
4

打开文件 gradle.properties 并将这两行添加到其中:

android.useAndroidX = true
android.enableJetifier = true

清洁和建造

于 2018-11-07T17:50:05.040 回答
3

注意:您不应该在您的应用程序中同时使用 com.android.support 和 com.google.android.material 依赖项。

在 build.gradle(app) 文件中添加适用于 Android 的 Material 组件

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.0.0-beta01'
    // ...
  }

如果您的应用当前依赖于原始设计支持库,您可以使用 Android Studio 提供的 Refactor to AndroidX... 选项。这样做会更新您的应用程序的依赖项和代码,以使用新打包的 androidx 和 com.google.android.material 库。

如果您还不想切换到新的 androidx 和 com.google.android.material 包,可以通过 com.android.support:design:28.0.0-alpha3 依赖项使用 Material 组件。

于 2018-09-11T07:29:27.860 回答
3

我通过将所有替换androidx.*appropiate package name.

改变你的线路

implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'

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

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

著名的

  • tools:replace="android:appComponentFactory"从 AndroidManifest中删除
于 2018-06-11T04:55:10.360 回答
0

Android文档对此很清楚。转到下面的页面。在下面,有两列名称为“OLD BUILD ARTIFACT”“AndroidX build artifact”

https://developer.android.com/jetpack/androidx/migrate

现在您在 gradle 中有许多依赖项。只需将它们与 Androidx 构建工件匹配并在 gradle 中替换它们。

那还不够。

转到您的 MainActivity(对所有活动重复此操作)并删除语句“公共类 MainActivity 扩展 AppCompatActivity”中的单词 AppCompact Activity 并再次写入相同的单词。但是这次 androidx 库被导入。直到现在 appcompact 支持文件被导入并使用(另外,删除该 appcompact 导入语句)。

另外,转到您的布局文件。假设你有一个约束布局,那么你可以注意到xml文件中的第一行约束布局有一些与appcompact相关的东西。所以只需将其删除并再次编写约束布局。但是现在添加了androidx相关的约束布局。

对尽可能多的活动和尽可能多的 xml 布局文件重复此操作..

但不要担心:Android Studio 在编译时会显示所有此类可能的错误。

于 2019-07-05T17:50:26.357 回答
0

有类似的问题。添加在 build.gradle 中,它对我有用。

 compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
于 2019-07-25T06:10:20.853 回答
0

尝试这个:

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
于 2018-10-08T06:52:30.420 回答
0

如果你想在不迁移到 AndroidX 的情况下解决这个问题(我不推荐)

此清单合并问题与您使用 androidX 的依赖项之一有关。

您需要降低此依赖项的发布版本。对于我的情况:

我使用的是 google 或 firebase

api 'com.google.android.gms:play-services-base:17.1.0'

我必须将它降低到 15.0.1 才能在支持库中使用。

于 2020-02-14T12:37:56.623 回答
0

下面的代码非常适合我:

dependencies {
    api 'com.android.support:design:28.0.0-alpha3'

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'
}
于 2018-06-11T21:04:58.377 回答
0
  1. 转到 gradle.properties

android.useAndroidX=true

android.enableJetifier=true

  1. 改变你的依赖

实施 'com.androidx.support:design:28.0.0'

实施 'com.androidx.support:design:28.0.0'

于 2021-12-05T10:02:44.947 回答
0

为我添加androidTestImplementation 'androidx.test:runner:1.2.0-alpha05'作品。

于 2020-07-01T10:05:40.677 回答