Has anybody had success using custom fonts with @P...
# compose-android
d
Has anybody had success using custom fonts with @Preview composables? Android Studio is failing to render with
Copy code
java.lang.IllegalStateException: Could not load font
  at androidx.compose.ui.text.font.TypefaceRequestCache.runCached(FontFamilyResolver.kt:207)
  at androidx.compose.ui.text.font.FontFamilyResolverImpl.resolve(FontFamilyResolver.kt:92)
The fonts are ttf files in
/app/src/main/res/font
and are being used like
Text("Test", fontFamily = robotoFontFamily)
The app works fine on real devices, it just seems to be something related to loading fonts for the preview pane in Android Studio. I'm using Koala Feature Drop 2024.1.1 and compose BOM 2024.09.02.
d
We must be in sync, I'm sitting at my work desk staring at exactly the same issue.
Maybe we can figure this out together; one thing I'm not doing is using the latest AGP, which may help... I'm on
8.2.2
but there's an
8.6.1
... will try.
...well what do you know? That fixed it for me 🙂 Try going to AGP
8.6.1
👌
One more tip: In case you flip between Android Studio and IntelliJ as I do; the latest IJ 2024.2.3 doesn't support AGP
8.6.1
, but the current Toolbox EAP of
2024.3
does ✔️
d
There must be something else in play because I was already on
8.6.1
Copy code
[versions]
agp = "8.6.1"
kotlin = "2.0.20"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.6"
activityCompose = "1.9.2"
composeBom = "2024.09.02"
ksp = "2.0.20-1.0.24"
hilt = "2.51.1"
navigationCompose = "2.8.1"
hiltNavigationCompose = "1.2.0"
runtimeLivedata = "1.7.2"
I saw some comments in various tickets about the
kotlinCompilerExtensionVersion
possibly being the problem. I have it defined in my build.gradle as
Copy code
composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
However, uprevving it doesn't change the version being used when I look at the output of the dependencies task. I tried 1.5.1, 1.5.15, and 1.6.1
Copy code
kotlin-extension - Configuration for Compose related kotlin compiler extension
\--- androidx.compose.compiler:compiler:1.5.1 -> org.jetbrains.kotlin:kotlin-compose-compiler-plugin-embeddable:2.0.20
-------------
\--- androidx.compose.compiler:compiler:1.5.15 -> org.jetbrains.kotlin:kotlin-compose-compiler-plugin-embeddable:2.0.20
-------------
\--- androidx.compose.compiler:compiler:1.6.1 -> org.jetbrains.kotlin:kotlin-compose-compiler-plugin-embeddable:2.0.20
Then after some research I found that it appears that might not be used any more and the compose compiler is covering that now. That was driven by my kotlin version in the toml, so I tried downrevving that to
2.0.10
which modified my compiler version to
Copy code
\--- androidx.compose.compiler:compiler:1.5.1 -> org.jetbrains.kotlin:kotlin-compose-compiler-plugin-embeddable:2.0.10
This allowed my preview to work as expected. What version of Kotlin/Compile plugin were you using? Maybe you hadn't uprevved that yet
What's interesting now is that when I uprevved back to
2.0.20
the preview worked fine. I restarted Android Studio and the preview is back to working again. I can only guess that something was cached and by playing with the versions forced gradle to clean things up.
d
From Kotlin 2.0 you should be adding the 'compose compiler plugin' (in addition to the 'compose plugin'), then removing that
kotlinCompilerExtensionVersion
setting - it's handled for you by the plugin.
286 Views