Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set Typography in Jetpack Compose

I use the default Typography in Jetpack Compose 1.0.0-beta08. But logcat gave me the error info like this:

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)

This is my 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
    )
)

Theme.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 setting for Project:

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 setting for module:

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

settings.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'

I think that it may be caused by the wrong version. But I don't know how to solve this. Please tell me how to fix this if you know.

like image 454
Evan Luo Avatar asked Jun 18 '21 10:06

Evan Luo


People also ask

How do I change font size in compose?

To change font size of Text composable in Android Jetpack Compose, pass a required font size value for the optional fontSize parameter of Text composable. Make sure to import sp , as shown in the above code snippet, when you are assign fontSize property with scale-independent pixels.

How do I change the Text color in compose?

To change color of Text composable in Android Jetpack Compose, pass a required Color value for the optional color parameter of Text composable.

How to change style of text to italic in Android jetpack compose?

How to change style of Text to Italic in Android Jetpack Compose? To change font style of Text composable to Italic, in Android Jetpack Compose, pass FontStyle.Italic for the optional fontStyle parameter of Text composable. The following is a sample code snippet to set the font style to italic.

What is textstyle in jetpack compose text?

5. TextStyle in JetPack Compose Text plays important role in mobile/web applications. We can present the details to the user using Text. Let's assume If we use the same font, same color, same size for this entire blog it doesn't look good and users can't able to understand.

What is a material theme in jetpack?

A Material Theme contains color, typography and shape attributes. When you customize these attributes, your changes are automatically reflected in the components you use to build your app. Jetpack Compose implements these concepts with the MaterialTheme composable: shapes = …

How to pass typography to a m3 materialtheme?

Once you have defined your Typography you can pass it to a M3 MaterialTheme: You can retrieve the Typography provided to the M3 MaterialTheme composable by using MaterialTheme.typography: Material 3 represents elevation mainly using tonal color overlays.


Video Answer


2 Answers

I just ran into the same problem and I think they just fixed it.

ext {
    compose_version = '1.0.0-beta09'
}
like image 121
Jasper Vergeer Avatar answered Oct 23 '22 04:10

Jasper Vergeer


In your Gradle file, app level, inside Compose options block, remove the Kotlin compiler line. Also, update to compose beta09 by changing the compose version in the project level Gradle file

like image 1
MARSK Avatar answered Oct 23 '22 05:10

MARSK