kotlingradlekotlin-multiplatform

Which Gradle task will rebuild / regenerate the Res class?


I'm using Android Studio to build a Kotlin Multiplatform (KMP) app using Compose Multiplatform, which I got from the Kotlin Multiplatform Wizard.

If I want to add a resource (such as an image or SVG) to my app I'm supposed to copy the resource's file into the right place and then reference the resource in my Kotlin code using something like Res.drawable.<name_of_file>.

The issue is that I'm getting a compile-time error about Res.drawable.<name_of_file> not being defined. This makes sense because I added the file but didn't re-generate the Res class.

I have two questions:

  1. Does anyone know the name of the Gradle task that will re-generate the Res class?

  2. Does anyone know how to find the Gradle task that re-generates Res? I've gone looking through the list of all the Gradle tasks (using the Gradle panel in Android Studio) but I haven't found anything with a name that suggests it rebuilds Res. The list is long and most of the tasks seem to be defined outside the project itself.


Solution

  • There are two ways get generated resource in Kotlin Multiplatform or Compose Multiplatform. First, you just need to add resource to commonMain and just rebuild it.

    Second, You need to run a command ./gradlew :composeApp:generateResourceAccessorsForCommonMain

    This is the same command that is run by gradle when we try to build app in Compose or Kotlin Multiplatform.