24

I upgraded to Jetpack Compose 1.0.0-alpha12 and started to run into issues.

Firstly, the setContent method I was using showed as deprecated.

From the Alpha 12 release notes, I noticed that it said:

ComponentActivity.setContent has moved to androidx.activity.compose.setContent in the androidx.activity:activity-compose module. (Icf416)

So I removed my import androidx.compose.ui.platform.setContent and switched it to import androidx.activity.compose.setContent, which removed the deprecation.

However, I then got an error that says:

w: Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies
w: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+NonParenthesizedAnnotationsOnFunctionalTypes
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
e: Classes compiled by an unstable version of the Kotlin compiler were found in dependencies.
Remove them from the classpath or use '-Xallow-unstable-dependencies' to suppress errors
e: /[my path]/MainActivity.kt: (39, 9): Class 'androidx.activity.compose.ComponentActivityKt' is
compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler

And again, I was able to work around that by changing my build.gradle file to have:

kotlinOptions {
    jvmTarget = '1.8'
    useIR = true
    // I added this line
    freeCompilerArgs += "-Xallow-unstable-dependencies"
}

While that let me compile my app, I now get the following exception at runtime:

java.lang.NoSuchMethodError: No static method setContent(
Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/Com
positionContext;Lkotlin/jvm/functions/Function2;)V in class 
Landroidx/activity/compose/ComponentActivityKt; or its super classes
(declaration of 'androidx.activity.compose.ComponentActivityKt' appears in [my apk]

How can I fix this and upgrade my app to Jetpack Compose 1.0.0-alpha12?

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443

2 Answers2

36

As per this issue, this issue is related to the new androidx.activity:activity-compose:1.3.0-alpha01 artifact.

From that issue:

Activity 1.3.0-alpha02 has been released and fixes this issue.

Apps using Compose alpha12 and specifically artifacts like androidx.compose.ui:ui-test-junit4:1.0.0-alpha12 that internally use setContent should add the activity-compose:1.3.0-alpha02 dependency to their dependencies block to ensure that the 1.3.0-alpha01 artifact is not used

So to fix your app, you should:

  1. Remove the freeCompilerArgs += "-Xallow-unstable-dependencies" line from the build.gradle file (as it is no longer needed)

  2. Add a specific dependency on Activity Compose 1.3.0-alpha02:

implementation 'androidx.activity:activity-compose:1.3.0-alpha02'

By adding that dependency, any direct usages of setContent as well as internal usages by androidx.compose.ui:ui-tooling:1.0.0-alpha12 or androidx.compose.ui:ui-test-junit4:1.0.0-alpha12 will use the fixed Activity Compose 1.3.0-alpha02 release.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
-1

With Activity 1.3.0-alpha02, setContent is working, but got another error.

Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
   > 2 files found with path 'META-INF/AL2.0' from inputs:

Had to use the workaround to make it built

    packagingOptions {
        exclude 'META-INF/AL2.0'
        exclude 'META-INF/LGPL2.1'
    }

And still have the warning Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies

joyl1216
  • 303
  • 1
  • 3
  • 10
  • If it isn't about `setContent`, then it really isn't an answer to this question, is it? It sounds like you're running into totally separate issues related to the move to Kotlin 1.4.3.0, namely [this Kotlin issue](https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-debug#debug-agent-and-android) (unrelated to Compose) and the use of a no longer needed flag you've left behind. – ianhanniballake Feb 23 '21 at 04:53
  • You are right. Created another new question about this. https://stackoverflow.com/questions/66343654/upgrading-to-jetpack-compose-1-0-0-alpha12-with-kotlin-1-4-30-error – joyl1216 Feb 24 '21 at 02:06