Android 设计支持库在 API 28(P)上无法使用。

118

我已经成功配置了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'
}

构建失败并出现以下错误:

Manifest 合并失败: 属性 application@appComponentFactory 的值 (androidx.core.app.CoreComponentFactory) 在 [androidx.core:core:1.0.0-alpha3] AndroidManifest.xml:22:18-86 中也存在于 [com.android.support:support-compat:28.0.0-alpha3] AndroidManifest.xml:22:18-91 中,值为(android.support.v4.app.CoreComponentFactory)。建议:在 AndroidManifest.xml 的元素中添加 'tools:replace="android:appComponentFactory"',以覆盖该属性。


你有检查过这个答案吗:https://dev59.com/RFQK5IYBdhLWcg3wQt5t#54825603 - Pratik Butani
查找应该使用哪个新的AndroidX库:https://developer.android.com/jetpack/androidx/migrate/artifact-mappings - Blundell
18个回答

102

你可以使用之前的API软件包版本或者新的Androidx,但不能同时使用。

如果你想使用之前的版本,请用以下代码替换你的依赖:


```html ```
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'
}

2
这个答案应该被接受。更多信息请访问https://medium.com/mindorks/whats-new-in-android-support-library-db0ec7a8ad1 - Rawa
为什么在“实现”中没有替换“material”中的“com”? - RoCkDevstack

62

重要更新

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'
}

很奇怪,我的项目已经使用P版本很长时间了,但今天在构建时开始出现支持库错误。我是否漏掉了什么? - Talha
这是我们现在经常遇到的一个普遍错误,在运行项目中我每隔几天就会遇到。只需清除缓存并重新启动即可解决。 - Khemraj Sharma
@Talha 请尝试从菜单中选择“Build > Clear Project”,然后再选择“Build > Make Project”进行编译,有时这已经足够了。如果还不行,请尝试执行“Invalidate cache and restart”,但是请注意,这需要很长时间,并会清除所有项目的缓存。 - John

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">

希望它有所帮助


添加了2行后出现错误:
  • 出错原因: 执行任务':app:generateDebugBuildConfig'失败。
    org.xml.sax.SAXParseException;systemId:file:/C:/D/code/js/emps_app/android/app/src/main/AndroidManifest.xml;lineNumber:14;columnNumber:52;属性“工具:替换”与元素类型“应用程序”相关联的前缀“工具”未绑定。
- user938363
2
你是否在AndroidManifest.xml头部添加了xmlns:tools="http://schemas.android.com/tools"? - Carlos Santiago
我在AndroidManifest.xml(位于android/app下)中添加了xmlns:tools =“schemas.android.com/tools”,但出现了新错误: > Task:app:processDebugManifest FAILED C:\ D \ code \ js \ emps_app \ android \ app \ src \ debug \ AndroidManifest.xml:2:11-50错误: 来自AndroidManifest.xml:2:11-50的属性manifest@tools value =(http://schemas.android.com/tools) 也存在于AndroidManifest.xml:2:11-50 value =(schemas.android.com/tools)中。 <manifest>元素的属性不会合并。 - user938363
2
android/src/debug/目录下的清单文件也有类似的头部声明:<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> - user938363
你应该也要添加 "xmlns:tools="http://schemas.android.com/tools""。 - Ali Ziaee
显示剩余2条评论

19

13

谷歌推出了新的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+)

通过从菜单栏中选择“重构”>“迁移到AndroidX”来将现有项目迁移到使用AndroidX。


1
实现 'com.google.android.material:material:1.1.0-alpha05',这是新版本。 - Hitesh Tarbundiya
在 'import android.support.design.widget.TabLayout;' 的位置上应该放什么? - temirbek

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和工具版本修改为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 的回答: Android 设计支持库在 API 28(P)上无法工作


这个解决方案不符合未来的要求,因为现在应该使用AndroidX。已发布的旧应用程序现在应该升级。如果需要大量重做工作,仍然应进行这些更改。 - Abhinav Saxena
@Abhinav Saxena,你说得对,我们应该将项目更新到28版本,而不是用旧的SDK版本来解决这个问题。 :) - Codios
这是被接受的答案。经过许多建议,没有什么能帮助我,但是这个配置很有用。你救了我的一天,谢谢。 - Noor Hossain

6

首先,您应该查看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开头,例如旧的design dependencycardview dependency
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'

所以,你需要仔细搜索和阅读。


6

AndroidX的设计支持库为implementation 'com.google.android.material:material:1.0.0'


5

打开 gradle.properties 文件并添加以下两行内容:

android.useAndroidX = true
android.enableJetifier = true

清理并构建


1
useAndroidX告诉编译器使用androidx文件而不是旧的android support包。 - Lena Bru
这将指示 Studio 3.2 及以上版本迁移到 AndroidX。已迁移到 AndroidX 的所有库将在其依赖项中更正。 - Abhinav Saxena

3
我通过将所有的androidx.*替换为适当的包名称来解决这个问题。
请更改您的行。
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'

注意

  • 从AndroidManifest中删除tools:replace="android:appComponentFactory"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接