Skip to content

Commit 722391f

Browse files
authored
Fix task dependency for Android Studio previews (#5478)
Fixes [CMP-7170](https://youtrack.jetbrains.com/issue/CMP-7170) Resources / Preview: NullPointerException with Android previews when accessing compose resources from another module ## Testing Integration test ## Release Notes ### Fixes - Resources - Fix resource gradle tasks invocation on AGP < 9.0.0 for Android Studio previews
1 parent e4e44e2 commit 722391f

File tree

11 files changed

+149
-1
lines changed

11 files changed

+149
-1
lines changed

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidResources.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private fun Project.configureGeneratedAndroidComponentAssets(
189189
)
190190
tasks.configureEach { task ->
191191
//fix agp task dependencies for AndroidStudio preview
192-
if (task.name == "compile${camelComponentName}Sources") {
192+
if (task.name == "package${camelComponentName}Resources") {
193193
task.dependsOn(copyComponentAssets)
194194
}
195195
//fix linter task dependencies for `build` task

gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.jetbrains.compose.test.tests.integration
22

3+
import org.gradle.util.GradleVersion
34
import org.jetbrains.compose.desktop.application.internal.ComposeProperties
5+
import org.jetbrains.compose.internal.Version
46
import org.jetbrains.compose.internal.utils.Arch
57
import org.jetbrains.compose.internal.utils.OS
68
import org.jetbrains.compose.internal.utils.currentArch
@@ -342,6 +344,28 @@ class ResourcesTest : GradlePluginTestBase() {
342344
}
343345
}
344346

347+
@Test
348+
fun testAndroidPreviewCallsResourcesPackaging() {
349+
// Valid for AGP < 9.0.0 only
350+
// https://youtrack.jetbrains.com/issue/CMP-7170
351+
Assumptions.assumeTrue { Version.fromString(defaultTestEnvironment.agpVersion).major < 9 }
352+
with(testProject("misc/oldAndroidTargetAppWithResources", defaultTestEnvironment)) {
353+
//AndroidStudio previews call `compileDebugSources` task
354+
gradle(":appModule:compileDebugSources").checks {
355+
check.taskSuccessful(":appModule:packageDebugResources")
356+
check.taskSuccessful(":featureModule:packageDebugResources")
357+
check.taskSuccessful(":featureModule:copyDebugComposeResourcesToAndroidAssets")
358+
359+
val resourceFile = "composeResources/oldagpresources.featuremodule.generated.resources/values/strings.commonMain.cvr"
360+
assertTrue {
361+
file(
362+
"featureModule/build/generated/assets/copyDebugComposeResourcesToAndroidAssets/$resourceFile"
363+
).exists()
364+
}
365+
}
366+
}
367+
}
368+
345369
@Test
346370
fun testDisableMultimoduleResources() {
347371
with(testProject("misc/commonResources")) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id("org.jetbrains.compose")
3+
kotlin("plugin.compose")
4+
id("com.android.application")
5+
}
6+
7+
android {
8+
namespace = "me.sample.app"
9+
compileSdk = 35
10+
defaultConfig {
11+
applicationId = "org.example.project"
12+
minSdk = 23
13+
targetSdk = 35
14+
versionCode = 1
15+
versionName = "1.0"
16+
}
17+
}
18+
19+
dependencies {
20+
implementation(project(":featureModule"))
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest>
3+
<application/>
4+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.sample.app
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.material3.Text
5+
import androidx.compose.runtime.Composable
6+
import androidx.compose.ui.Modifier
7+
import oldagpresources.featuremodule.generated.resources.*
8+
import org.jetbrains.compose.resources.stringResource
9+
10+
@Composable
11+
fun App() {
12+
Column {
13+
val txt = "text: "
14+
Text(txt + stringResource(Res.string.str_1))
15+
MyFeatureText(txt = txt)
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
plugins {
2+
id("org.jetbrains.compose").apply(false)
3+
kotlin("multiplatform").apply(false)
4+
kotlin("plugin.compose").apply(false)
5+
id("com.android.library").apply(false)
6+
id("com.android.application").apply(false)
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id("org.jetbrains.compose")
3+
kotlin("multiplatform")
4+
kotlin("plugin.compose")
5+
id("com.android.library")
6+
}
7+
8+
kotlin {
9+
jvm()
10+
11+
androidTarget()
12+
13+
sourceSets {
14+
commonMain.dependencies {
15+
api(compose.runtime)
16+
api(compose.material3)
17+
api(compose.components.resources)
18+
}
19+
}
20+
}
21+
android {
22+
namespace = "me.sample.feature"
23+
compileSdk = 35
24+
}
25+
26+
compose.resources {
27+
publicResClass = true
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="str_1">Feature text str_1</string>
3+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.sample.app
2+
3+
import androidx.compose.material3.Text
4+
import androidx.compose.runtime.Composable
5+
import androidx.compose.ui.Modifier
6+
import org.jetbrains.compose.resources.stringResource
7+
import oldagpresources.featuremodule.generated.resources.*
8+
9+
@Composable
10+
fun MyFeatureText(modifier: Modifier = Modifier, txt: String) {
11+
Text(txt + stringResource(Res.string.str_1), modifier)
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
2+
kotlin.code.style=official
3+
android.useAndroidX=true

0 commit comments

Comments
 (0)