Notes on the Jetpack Compose alpha11 to alpha12 Upgrade Process
Upgrading an app from Compose alpha11 to alpha12 was more troublesome than normal. I took some notes along the way and wanted to pass them along.
Note: all of the Gradle samples show the classic Groovy approach — you will
need to tweak those for build.gradle.kts
if you are going that route.
The obvious change is moving your Compose Gradle plugin and all of the
Compose runtime dependencies to 1.0.0-alpha12
, along with your kotlinCompilerExtensionVersion
in composeOptions
. If you have been working with Compose for a while, this is
a standard change, and hopefully you have consolidated all of those version
references into a single constant.
Compose alpha12 also requires Kotlin 1.4.30, for its Gradle plugin and for its runtime dependencies. With luck, you have a single constant for that as well:
buildscript {
ext {
kotlinVersion = "1.4.30"
composeVersion = '1.0.0-alpha12'
}
...
}
The release notes
also show having this Gradle snippet outside of your android {}
closure in your modules:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += ["-Xallow-jvm-ir-dependencies"]
}
}
After doing those things, I then ran into a bunch of IDE errors akin to the following:
‘padding(Dp): Modifier’ is only available since Kotlin 1.4.30 and cannot be used in Kotlin 1.4
At first, I tried upgrading to “Android Studio Arctic Fox | 2020.3.1 Canary 6” (hereafter referred to as “Canary 6” for simplicity). That did not clear up the problem.
The solution is to upgrade the Kotlin plugin in the IDE to support 1.4.30. Unfortunately, this is not yet stable. So, you need to:
- Open the Settings dialog in Canary 6
- Go into “Languages & Frameworks” > Kotlin
- Switch your “Update channel” to “Early Access Preview 1.4.x”
- Choose to upgrade your Kotlin plugin to 203-1.4.30-RC-AS6682.9 (or newer, depending on when you are reading this)
- Restart Android Studio afterwards
This appears to be undocumented, unless you count Kotlinlang Slack messages from Google developers as documentation.
You should also check all your third-party dependencies and make sure that you have
their latest versions, as otherwise you may fail when you build the module.
In my case, I needed to upgrade dev.chrisbanes.accompanist:accompanist-coil
to 0.5.1
.
That got me to the point where things would run, but you are likely to encounter a bunch of deprecations.
The one that will affect most of you is that
setContent()
on Activity
will show up as deprecated. That is because it moved
out of a mainline Compose dependency. You will need to add:
implementation "androidx.activity:activity-compose:1.3.0-alpha02"
to your module’s list of dependencies. Note the alpha02
— while alpha01
was released this past week, alpha02
shipped hours later to fix a significant bug.
Once you have that dependency in place, you can replace:
import androidx.compose.ui.platform.setContent
with:
import androidx.activity.compose.setContent
If you happen to be using the Jetpack ViewModel
in your Compose UI code, you may run
into a similar deprecation notice on viewModel()
. That is another case where the
extension function moved from a mainline Compose dependency to another one.
You will need to add:
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha01"
to your module’s dependencies. Then, you can change:
import androidx.compose.ui.viewinterop.viewModel
to:
import androidx.lifecycle.viewmodel.compose.viewModel
There is a “long tail” of other deprecations that you might encounter. In my
samples, one was that imageResource()
is deprecated. The replacements,
though, will vary by circumstance:
-
If you need a
Bitmap
, you will wind up changingimageResource(R.drawable.whatever)
toimageFromResource(LocalContext.current.resources, R.drawable.whatever)
-
Otherwise,
painterResource(R.drawable.whatever)
will probably suffice
This upgrade process was more involved than usual, exacerbated by gaps in documentation. I will be honest: this worries me, with a possible move to “beta” versions coming in the next couple of weeks.