Jetpack Compose: NoSuchMethodError on setContent

Apr 29, 2022

Recently, I faced several hours of frustration when trying to add Jetpack Compose to an existing Android project. Eventually, the fix was a oneliner. If you found this page, I guess you’re struggling as well. I hope this helps!

The project is a multi-module Android project, has buildSrc, Kotlin 1.6.20, and no Jetpack Compose so far. I tried to introduce the latest Jetpack Compose version 1.2.0-alpha08.

When adding a simple test-Compose:

class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Text(text = "Hello World!")
        }
    }
}

…the app started to crash immediately on startup.

The exception message varied depending on whether setContent was called in an Activity:

java.lang.NoSuchMethodError: No static method setContent$default(Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/CompositionContext;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V in class Landroidx/activity/compose/ComponentActivityKt; or its super classes (declaration of 'androidx.activity.compose.ComponentActivityKt' appears in /data/app/…/base.apk)
    at at.xa1.testapp.TestActivity.onCreate(TestActivity.kt:42)
    at android.app.Activity.performCreate(Activity.java:8054)
    at android.app.Activity.performCreate(Activity.java:8034)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7842)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

…or in a Fragment using a ComposeView:

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/…/base.apk)
    at at.xa1.testapp.TestComposeFragment.onCreateView(TestComposeFragment.kt:42)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1758)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
    at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:488)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7842)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

When I tried to use Column I even got a more weird error, already at compile-time:

org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: …/ComposeExample.kt
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:238)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:234)

Solution

As it turned out, the Compiler was just missing a dependency in all the cases. After adding this line to the buildSrc/build.gradle it worked like a charm:

dependencies {
     
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20"
}

That’s it!

That was all that was missing for me.

But for the sake of completeness, all of these things are important:

buildSrc/build.gradle:

buildscript {
    
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20"
    }
}

dependencies {
    
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20"
}

module-with-compose/build.gradle:

android {
    buildFeatures {
        compose true
    }
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    composeOptions {
        kotlinCompilerExtensionVersion "1.1.1"
    }
}

dependencies {
    implementation "androidx.compose.foundation:foundation:1.2.0-alpha08"
    implementation "androidx.compose.ui:ui:1.2.0-alpha08"
    implementation "androidx.activity:activity-compose:1.4.0"
    implementation "androidx.compose.animation:animation:1.2.0-alpha08"
    implementation "androidx.compose.foundation:foundation:1.2.0-alpha08"
    implementation "androidx.compose.material:material:1.2.0-alpha08"
    implementation "androidx.compose.material:material-icons-core:1.2.0-alpha08"
    implementation "androidx.compose.material:material-icons-extended:1.2.0-alpha08"
    implementation "androidx.compose.runtime:runtime:1.2.0-alpha08"
    implementation "androidx.compose.ui:ui-tooling:1.2.0-alpha08"
    implementation "androidx.compose.ui:ui:1.2.0-alpha08"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1"

    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.2.0-alpha08"
}

Conclusion

In my opinion, Jetpack Compose is not mature yet. Errors like this are very obscure and hard to debug. If you still want to use Jetpack Compose, follow the documentation and especially the setup instructions as closely as possible. Also, keep in mind that each Jetpack Compose version is only compatible with specific Kotlin versions. If something doesn’t work, consider updating everything from Android Gradle Plugin to Kotlin and experimenting with beta versions of Jetpack Compose. Often, only the very latest versions are supported.

I hope this post could relieve some frustration. Enjoy!

You might also like