我的主题是从Theme.MaterialComponents 扩展而来的吗?哦,是的,确实,自从我开始使用任何新的 Material UI 东西以来就一直如此。
如果这里的所有这些答案对您和对我一样无济于事,请做好准备:This component requires that you specify a valid android:textAppearance attribute 错误可能与 指定android:theme 与您使用的主题同名的外部库有关,并且Android 根据你的 build.gradle 随机决定使用哪一个。就我而言,罪魁祸首在Mobile FFmpeg 内部。
我在工作一周后开始遇到此错误,而构建变体被设置为不同的产品风格,然后切换回原来的风格。同时发生了什么变化?经过彻底调查后,我发现在我将implementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS' 分成两部分后构建中断,对于我实际使用它的每种产品风味,如下所示:
videoImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'
mainImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'
这足以触发This component requires that you specify a valid android:textAppearance attribute 用于风味main,同时可以正常用于风味video。每个main 构建都崩溃了,因为我的应用程序的主题名为AppTheme,并且Mobile FFmpeg 清单还指定了android:theme="@style/AppTheme"(这会影响到4.2.2 的所有版本)。所以我重命名了我的主题,但这导致了一个非常类似于这里的构建错误:
https://github.com/tanersener/mobile-ffmpeg/issues/206
Attribute application@theme value=(@style/ScryptedTheme) from AndroidManifest.xml:37:9-45
is also present at [com.arthenica:mobile-ffmpeg-https:4.2.LTS] AndroidManifest.xml:17:9-40 value=(@style/AppTheme).
Suggestion: add 'tools:replace="android:theme"' to <application> element at AndroidManifest.xml:31:5-95:19 to override.
添加说tools:replace="android:theme"后,一切又恢复了,原来的MaterialComponents错误消失了。
但是为什么一种口味可以,而另一种口味不行呢?完全不知道。这要归功于 Google 倾向于将最疯狂的错误添加到“稳定”版本中。至少可以通过一些重构很容易地解决。
TL;DR
这是问题所在:https://github.com/tanersener/mobile-ffmpeg/issues/206
再加上当两个合并的 manifest 指定同名的不同主题时,Android 会根据你的 build.gradle 的内容随机选择一个。
解决方案:将 tools:replace="android:theme" 添加到清单的 <application> 标记并更改主题名称。