Skip to content

Compose Preview fails with "unresolved reference: R" #1699

Description

@Ben-ADFA

Ref. 5203

The Jetpack Compose Layout Preview panel reports a compilation error for R on code that compiles and runs fine when built as a real APK.

User specs:
POCO F5 5G (8GB RAM + 256GB storage), Android 16

The user also provided the following code sample:
package com.learn
import com.learn.R
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.Alignment
import androidx.compose.foundation.layout.Box
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.Image
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.layout.ContentScale
import androidx.compose.foundation.layout.fillMaxWidth

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
MyImage()
}
}
}
}
}
}

@Preview
@composable
fun MyImage() {
Image(
painter = painterResource(id = R.drawable.img),
contentDescription = "My image",
modifier = Modifier.fillMaxWidth(),
contentScale = ContentScale.Crop
)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions