PC パソコン

How to Fix “ClassNotFoundException” for a Nested PreviewParameterProvider

Jetpack Compose Preview can fail with ClassNotFoundException when the PreviewParameterProvider referenced by @PreviewParameter is nested inside another class. Google tracks this as Issue #364917767 and lists it as fixed in Android Studio Rabbit 1 Canary 5.

Representative error

java.lang.ClassNotFoundException:
com.example.preview.PreviewData$SampleProvider

The class name varies. The useful signature is that Compose Preview alone cannot load a nested provider whose binary name joins the outer and inner classes with $.

Cause

The affected Compose Preview implementation resolves the source name and binary name of a nested provider incorrectly, so its preview class loader cannot find the class. This is an IDE tooling defect rather than an exception thrown by the provider’s value logic.

Fixed version: Android Studio Rabbit 1 Canary 5. The public record does not establish one definitive first affected build, so it is not safe to label every earlier release as affected.

Conditions and versions

  • The preview uses @Preview and @PreviewParameter.
  • The provider is nested inside another class or object.
  • The app may build successfully while Design or Split preview fails.
  • Android Studio predates the verified fix.

Example

class PreviewData {
    class SampleProvider : PreviewParameterProvider<String> {
        override val values = sequenceOf("First", "Second")
    }
}

@Preview
@Composable
fun SamplePreview(
    @PreviewParameter(PreviewData.SampleProvider::class) value: String
) { Text(value) }

Recommended solution

  1. Confirm that the class named by the Preview error is the provider supplied to @PreviewParameter.
  2. Update to Android Studio Rabbit 1 Canary 5 or a later build containing the fix. Validate preview IDE compatibility alongside Stable before changing a shared environment.
  3. Run Gradle Sync and Build & Refresh. Restart the IDE if the old preview class loader remains cached.
./gradlew clean assembleDebug

Alternative

If you cannot update yet, move the provider to the top level.

class SampleProvider : PreviewParameterProvider<String> {
    override val values = sequenceOf("First", "Second")
}

This is a workaround for the older IDE’s name-resolution path; it does not mean the Compose API prohibits nested providers.

Verification

  1. Open the same composable in the updated IDE.
  2. Confirm that Preview renders every provider value.
  3. Verify that ClassNotFoundException is absent from Preview details.
  4. Run a debug build and test on a device or emulator.

Cautions

If a top-level provider still fails, check that it is not abstract or inner, can be created without constructor arguments, and does not throw while evaluating values. This fix does not apply to general dependency-related classpath failures.

Summary

A preview-only ClassNotFoundException involving a nested PreviewParameterProvider can match Google Issue #364917767. Update to Rabbit 1 Canary 5 or later; temporarily move the provider to the top level if an update is not possible.

Official references