使用 androidX 在项目中配置 ViewPager2

ViewPager2 config in project using androidX

我的项目 运行 很好,没有在我的应用程序中实现 ViewPager2 gradle。但是当我在我的项目中使用它的 gradle implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01' 时,它给了我这个 Log-cat 错误...

Android resource linking failed
Output:  D:\sam\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-v28\values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
D:\sam\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-v28\values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
D:\sam\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:557: error: resource android:attr/fontVariationSettings not found.
D:\sam\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:557: error: resource android:attr/ttcIndex not found.
D:\sam\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:834: error: resource android:attr/textFontWeight not found.
error: failed linking references.

Command: C:\Users\anonymous\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.1-4818971-windows.jar\e2e18b400b8fa36a8523d44c560bb82c\aapt2-3.2.1-4818971-windows\aapt2.exe link -I\
        C:\Users\anonymous\AppData\Local\Android\Sdk\platforms\android-27\android.jar\
        --manifest\
        D:\sam\app\build\intermediates\merged_manifests\debug\processDebugManifest\merged\AndroidManifest.xml\
        -o\
        D:\sam\app\build\intermediates\processed_res\debug\processDebugResources\out\resources-debug.ap_\
        -R\
        @D:\sam\app\build\intermediates\incremental\processDebugResources\resources-list-for-resources-debug.ap_.txt\
        --auto-add-overlay\
        --java\
        D:\sam\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\
        --custom-package\
        com.sam.test.sushil\
        -0\
        apk\
        --output-text-symbols\
        D:\sam\app\build\intermediates\symbols\debug\R.txt\
        --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.1-4818971-windows Daemon #0

同样的错误 implementation group: 'androidx.viewpager2', name:'viewpager2', version: '1.0.0-alpha01'

然后我创建了另一个空项目并实现了implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01'

它给了我这个错误.....

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.1.0-alpha04] 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.

以上错误特定 android.support.v4.app.CoreComponentFactory 之间存在冲突,即 implementation 'com.android.support:appcompat-v7:28.0.0'implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01'。所以我从我的项目中删除了 support:appcompat 库。

现在可以使用了。

我的问题是

如果我删除 support:appcompat,我将无法使用 AppCompatActivity 及其用途。它存在于我现有的应用程序中。

如何解决?

如何同时使用 ViewPager2support:appcompat

编辑

@ManoharReddy 在清单的 <application> 标签内添加 tools:replace="android:appComponentFactory" 之后。它给了我这个错误

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:103)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:73)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
    at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:91)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:59)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.run(DefaultTaskGraphExecuter.java:256)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:249)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:238)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access0(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:663)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:597)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs
    at com.android.builder.core.AndroidBuilder.mergeManifestsForApplication(AndroidBuilder.java:540)
    at com.android.build.gradle.tasks.MergeManifests.doFullTaskAction(MergeManifests.java:173)
    at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.run(ExecuteActionsTaskExecuter.java:124)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:113)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:95)
    ... 33 more

我找到了解决方案。

正如@Kishore 和@Shweta 所说,迁移项目中的 AndroidX 是解决方案。

手动添加 android.useAndroidX=trueandroid.enableJetifier=true 让我很难过。因为它抛出一些错误或 Suggestion: add 'tools:replace="android:appComponentFactory"' to <application>

要在项目中启用 Jet-fire,android Studio 中有一个选项

Select 你的项目 ---> 右击

app----> 重构----> 迁移到 AndroidX

如下图所示:-

点击迁移到 AndroidX 后。

它将要求确认并备份您的项目。

最后一步它会要求您进行重构。

重构后检查你的 gradle.properties 有 android.useAndroidX=trueandroid.enableJetifier=true。如果不是,则将这两行添加到您的 gradle.properties 文件中:

android.useAndroidX=true
android.enableJetifier=true

感谢所有用户的帮助。