Jetpack Compose Typography配置错误解决

前些日子 Jetpack Compose 稳定版还没出的时候,我在使用过程中碰到了各种问题,其中就有在设置 Typography 后出现如下报错:

1
2
3
4
5
6
7
8
9
10
11
java.lang.NoSuchMethodError: No static method copy-H99Ercs$default(Landroidx/compose/ui/text/TextStyle;JJLandroidx/compose/ui/text/font/FontWeight;Landroidx/compose/ui/text/font/FontStyle;Landroidx/compose/ui/text/font/FontSynthesis;Landroidx/compose/ui/text/font/FontFamily;Ljava/lang/String;JLandroidx/compose/ui/text/style/BaselineShift;Landroidx/compose/ui/text/style/TextGeometricTransform;Landroidx/compose/ui/text/intl/LocaleList;JLandroidx/compose/ui/text/style/TextDecoration;Landroidx/compose/ui/graphics/Shadow;Landroidx/compose/ui/text/style/TextAlign;Landroidx/compose/ui/text/style/TextDirection;JLandroidx/compose/ui/text/style/TextIndent;ILjava/lang/Object;)Landroidx/compose/ui/text/TextStyle; in class Landroidx/compose/ui/text/TextStyle; or its super classes (declaration of 'androidx.compose.ui.text.TextStyle' appears in /data/app/~~dzGPwRcTH3NcPitRFMz-4g==/cn.phakel.fighting-5iN5DgNNOwwUjTIkZ-2A6Q==/base.apk)
at androidx.compose.material.TypographyKt.withDefaultFontFamily(Typography.kt:284)
at androidx.compose.material.TypographyKt.access$withDefaultFontFamily(Typography.kt:1)
at androidx.compose.material.Typography.<init>(Typography.kt:186)
at androidx.compose.material.Typography.<init>(Typography.kt:118)
at cn.phakel.fighting.ui.theme.TypeKt.<clinit>(Type.kt:10)
at cn.phakel.fighting.ui.theme.TypeKt.getTypography(Type.kt:10)
at cn.phakel.fighting.ui.theme.ThemeKt.FightingTheme(Theme.kt:36)
at cn.phakel.fighting.ComposableSingletons$MainActivityKt$lambda-3$1.invoke(MainActivity.kt:19)
at cn.phakel.fighting.ComposableSingletons$MainActivityKt$lambda-3$1.invoke(MainActivity.kt:18)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)

Type.kt如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
val Typography = Typography(
body1 = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
),
button = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.W500,
fontSize = 14.sp
),
caption = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 12.sp
)
)

自己鼓捣了半天也没也没好,最后上 StackOverFlow 问了下找到了解决方法。

我的 Gradle 设置中的 compose_version版本为1.0.0-beta08,而 Type.kt 中的写法在该版本中不存在。所以解决方案是把版本改成如下格式:

1
2
3
ext {
compose_version = '1.0.0-beta08'
}

Jetpack Compose Typography配置错误解决

https://www.phakel.cn/2021-09-13/f64b4108/

作者

EvanLuo42

发布于

2021-09-13

更新于

2021-09-13

许可协议

评论