新建Android Studio的Kotlin项目,运行报错:AAPT2 process unexpectedly exit. Error output:

一、创建新的Android Studio Kotlin项目,直接运行后报错:
AAPT2 process unexpectedly exit. Error output:
新建Android Studio的Kotlin项目,运行报错:AAPT2 process unexpectedly exit. Error output:
文章图片

详情如下:
新建Android Studio的Kotlin项目,运行报错:AAPT2 process unexpectedly exit. Error output:
文章图片

FAILURE: Build failed with an exception.* What went wrong:Execution failed for task ':app:processDebugResources'.> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.> Failed to transform artifact 'appcompat.aar (androidx.appcompat:appcompat:1.1.0)' to match attributes {artifactType=android-compiled-dependencies-resources}.> Execution failed for AarResourcesCompilerTransform: C:\Users\20012101\.gradle\caches\transforms-2\files-2.1\ac4199d79d4b0412da579f23fc82cb0f\appcompat-1.1.0.> AAPT2 aapt2-3.6.3-6040484-windows Daemon #0: Unexpected error during compile 'C:\Users\20012101\.gradle\caches\transforms-2\files-2.1\ac4199d79d4b0412da579f23fc82cb0f\appcompat-1.1.0\res\drawable-hdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png', attempting to stop daemon.This should not happen under normal circumstances, please file an issue if it does.


二、试了以下方法都不行:
1.更新windows运行时库(参考:https://stackoverflow.com/questions/59538305/unable-to-run-on-device-after-migrating-to-androidx-failed-for-task-appmerge)
2.更改gradle版本,更改Android Studio版本(直接无法编译),更改appcompat包的版本号,更换gradle和SDK的路径
三、发现原因:
最后把kotlin项目相对于java项目的gradle配置增量做逐行增加,验证出当增加androidx下的appcompat包,就会报出这个问题;
猜测:可能是androidx下的appcompat包没兼容Gradle3.6.3,具体原因没有找到
四、解决方案:
删除androidx下的appcompat包依赖,再将styles.xml和AndroidManifest的AppTheme创建和引用删掉(因为AppTheme用到了appcompat包),最后将AppCompatActivity换成Activity

【新建Android Studio的Kotlin项目,运行报错:AAPT2 process unexpectedly exit. Error output:】

    推荐阅读