Tobias Reich
Tobias Reich

Reputation: 5160

Jetpack Compose - No virtual method setContent

I wanted to add Jetpack Compose to my Android App. The libs are already added (according to the tutorial and also the sample apps from Google). I use Kotlin 1.9.20 and the compile JVM version is Java 17. The Activity is an AppCompatActivity() so it should be compatible with compose.

The build.gradle file has the following lines added:

compileOptions {
    sourceCompatibility = 17
    targetCompatibility = 17
}

with(kotlinOptions) {
    jvmTarget = "17"
    languageVersion = 1.9
}

buildFeatures {
    compose = true
}
composeOptions {
    kotlinCompilerExtensionVersion = "1.5.4"
}

We have a buildSrc project along with the project and there we have the dependency declared like that:

dependencies {
    implementation("com.android.tools.build:gradle:8.1.2")
    ...
}

And it seems to be the cause of the problem. When I remove this dependency and specify it in the settings.gradle.kts it works.

The minimal version of the libraries look like that:

implementation(platform("androidx.compose:compose-bom:2023.10.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.material:material")
implementation("androidx.compose.ui:ui-tooling-preview")

So all looks fine. However when I add a composeView I get an exception as this:

NoSuchMethodError: No virtual method setContent(Lkotlin/jvm/functions/Function0;)V in class Landroidx/compose/ui/platform/ComposeView;

for a simplified code using XML layouts adding a ComposeView in the layout like that:

composeView = view.findViewById(R.id.compose_view)
composeView.setContent {
  ...
}

In the first line I can already see the composeView of my XML layout. And it IS a ComposeView. Just the next line fails.

I also tried making my whole fragment a container for Compose.

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
           setViewCompositionStrategy(
               ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent {
                // In Compose world
                MaterialTheme {
                    MyLayout()
                }
            }
        }
    }

Still I get the same error:

java.lang.NoSuchMethodError: No virtual method setContent(Lkotlin/jvm/functions/Function0;)V in class Landroidx/compose/ui/platform/ComposeView; or its super classes
     (declaration of 'androidx.compose.ui.platform.ComposeView' appears in /data/app/~~XE01rsSoScPaH1MnxHjNsA==/.../base.apk)
     at myFragment.onCreateView(MyFragment.kt:30)
     at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3114)

Upvotes: 0

Views: 1220

Answers (2)

psyill
psyill

Reputation: 911

I got the same error. In my case, I had forgot to put

buildFeatures {
    compose true
}

composeOptions {
    kotlinCompilerExtensionVersion my_compose_compiler_version
}

in my build.gradle. Inserting those lines made the problem go away.

Upvotes: 1

stan372
stan372

Reputation: 5

I believe you just need to add this dependency: implementation("androidx.activity:activity-compose")

Upvotes: 0

Related Questions