我正在尝试使用MVVM架构构建应用,为此,我在build.gradle
中实现了以下内容:
dependencies {
def lifecycle_version = "2.0.0"
def room_version = "2.1.0-alpha03"
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 "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
但是随后我遇到了Manifest Merger Failed
错误:
Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:5:5-19:19 to override.
堆栈建议我将tools:replace="android:appComponentFactory"
放在清单中的<application>
元素上,但这本身会导致更大的错误。
谢谢。
答案 0 :(得分:2)
如果您要使用AndroidX,则所有 支持依赖都必须是AndroidX。
其中包括appcompat-v7
和constraint-layout
。
AndroidX具有migration table,可为您提供所有等效的支持库。
这将需要一些手动迁移,因为某些导入(AppCompatActivity,ConstraintLayout等)现在将无效。如果您尝试构建,它将告诉您这些无效导入在构建错误日志中的位置。
还要确保找到XML中的AppCompat View的所有用法(例如ConstraintLayout),并用其对应的AndroidX替代(只需开始输入View类名,Android Studio会建议正确的用法)。
答案 1 :(得分:0)
流浪者是对的。我必须将所有内容更改为AndroidX,这就是我所做的。这是我的新依赖项:
dependencies {
def lifecycle_version = "2.0.0"
def room_version = "2.1.0-alpha03"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}