Caleb
Caleb

Reputation: 35

"Unresolved reference: R" - Kotlin Compose

I am making a simple note taking app in Kotlin Compose and I am trying to add a font from a font file (.otf) but its saying R doesn't exist. Ive tried to import from my package, dev.bluemethyst.apps.notix.R and it doesn't exist there either.

I have tried invalidating gradle caches, rebuilding gradle, cleaning gradle, recreating the folder and redownloading the fonts. Nothing seems to work, please help me :P

package dev.bluemethyst.apps.notix

import dev.bluemethyst.apps.notix.R
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight

val letteraMonoLLFont = FontFamily(
    Font(R.font.lettera_mono_ll_regular, FontWeight.Normal),
    Font(R.font.lettera_mono_ll_condlight_regular, FontWeight.Bold)
)

ide screenshot

ide app structure

Upvotes: 1

Views: 264

Answers (2)

Hillol Talukdar
Hillol Talukdar

Reputation: 26

You cannot access R because you are in the commonMain. To access it, you must be in the androidMain, as commonMain does not support platform-specific dependencies.

To resolve this, create an interface or an expect class in commonMain and implement it in both androidMain and iosMain. In the androidMain implementation, you can then access R.

Upvotes: 1

R is imported with the following structure:

import {namespace}.R

{namespace} is the namespace defined in your module level gradle file.

Upvotes: 1

Related Questions