android - 无法在 Jetpack Compose 中设置排版

标签 android android-jetpack-compose

我使用 Jetpack Compose 1.0.0-beta08 中的默认排版。但是logcat给了我这样的错误信息:

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:
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
    )
)
主题.kt:
@Composable
fun FightingTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) {
val colors = if (darkTheme) {
    DarkColorPalette
} else {
    LightColorPalette
}

MaterialTheme(
        colors = colors,
        typography = Typography,
        shapes = Shapes,
        content = content
)
}
MainActivity.kt:
class MainActivity : ComponentActivity() {
@ExperimentalPagerApi
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        FightingTheme {
            Surface(color = MaterialTheme.colors.background) {
                val navController = rememberNavController()
                Home(navController = navController)
            }
        }
    }
}
}
项目的 Gradle 设置:
buildscript {
ext {
    compose_version = '1.0.0-beta08'
}
repositories {
    google()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:7.0.0-beta04'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}
模块的 Gradle 设置:
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 30
    buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "cn.phakel.fighting"
    minSdk 21
    targetSdk 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables {
        useSupportLibrary true
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
    useIR = true
}
buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion compose_version
    kotlinCompilerVersion '1.4.32'
}
}

dependencies {
    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.0-beta01'
    testImplementation 'junit:junit:4.+'
    implementation 'com.google.code.gson:gson:2.8.7'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    implementation 'com.google.accompanist:accompanist-pager:0.11.1'
    implementation 'com.google.accompanist:accompanist-pager-indicators:0.9.1'
    implementation 'com.google.accompanist:accompanist-coil:0.11.0'
    implementation 'io.github.openfeign:feign-okhttp:10.11'
    implementation 'io.github.openfeign:feign-gson:10.11'
    implementation 'io.github.openfeign:feign-slf4j:10.11'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
    implementation 'androidx.navigation:navigation-compose:2.4.0-alpha03'
}
gradle.properties
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
android.enableJetifier=true
kotlin.code.style=official
设置.gradle:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
    google()
    mavenCentral()
    jcenter() // Warning: this repository is going to shut down soon
}
}
rootProject.name = "Fighting"
include ':app'
我认为这可能是由错误的版本引起的。但我不知道如何解决这个问题。如果您知道,请告诉我如何解决此问题。

最佳答案

我刚刚遇到了同样的问题,我认为他们只是解决了它。

ext {
    compose_version = '1.0.0-beta09'
}

关于android - 无法在 Jetpack Compose 中设置排版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68033451/

相关文章:

android - 调试 WatchFace 时永远为 "Waiting for application to start debug server"

android - 在没有 .class 的情况下开始 Activity

java - 滚动 GridView 导致 GC_FOR_ALLOC 释放问题

android - 如何仅选择列表中的一项(LazyColumn)?

android - Jetpack Compose 1.0.0-alpha11如何手动导入扩展函数?

android - 在 ViewModel 中使用 hilt 注入(inject)时,无法在可组合函数中使用 ViewModel

android - 具有不同列数和自动列宽的表格

java - RxJava onErrorResumeNext 调度器

android - Jetpack Compose 现有多模块应用程序中的依赖项

Android Compose 显示和隐藏键盘