diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt index 7eb8465444..1c2d7d581c 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt @@ -34,7 +34,7 @@ abstract class GenerateResClassTask : DefaultTask() { logger.info("Generate resources for $rootResDir") //get first level dirs - val dirs = rootResDir.listFiles().orEmpty() + val dirs = rootResDir.listNotHiddenFiles() dirs.forEach { f -> if (!f.isDirectory) { @@ -45,8 +45,7 @@ abstract class GenerateResClassTask : DefaultTask() { //type -> id -> resource item val resources: Map>> = dirs .flatMap { dir -> - dir.listFiles() - .orEmpty() + dir.listNotHiddenFiles() .mapNotNull { it.fileToResourceItems(rootResDir.toPath()) } .flatten() } @@ -105,6 +104,9 @@ abstract class GenerateResClassTask : DefaultTask() { .map { it.attributes.getNamedItem("name").nodeValue } return ids.toSet() } + + private fun File.listNotHiddenFiles(): List = + listFiles()?.filter { !it.isHidden }.orEmpty() } internal fun String.asUnderscoredIdentifier(): String = diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt index 3244537b07..abeb73ad94 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt @@ -11,9 +11,13 @@ import java.io.File internal const val COMPOSE_RESOURCES_DIR = "composeResources" private const val RES_GEN_DIR = "generated/compose/resourceGenerator" +private val androidPluginIds = listOf( + "com.android.application", + "com.android.library" +) internal fun Project.configureComposeResources() { - pluginManager.withPlugin(KOTLIN_MPP_PLUGIN_ID) { + plugins.withId(KOTLIN_MPP_PLUGIN_ID) { val kotlinExtension = project.extensions.getByType(KotlinMultiplatformExtension::class.java) kotlinExtension.sourceSets.all { sourceSet -> val sourceSetName = sourceSet.name @@ -76,12 +80,14 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File, } } - val androidExtension = project.extensions.findByName("android") - if (androidExtension != null) { - configureAndroidResources( - commonComposeResources, - buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile }, - shouldGenerateResourceAccessors - ) + //when applied AGP then configure android resources + androidPluginIds.forEach { pluginId -> + plugins.withId(pluginId) { + configureAndroidResources( + commonComposeResources, + buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile }, + shouldGenerateResourceAccessors + ) + } } } diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt index 284ff4d58a..da3cbc3bd2 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt @@ -9,7 +9,7 @@ import kotlin.io.path.Path class ResourcesTest : GradlePluginTestBase() { @Test - fun testGeneratedAccessorsAndCopiedFonts() = with(testProject("misc/commonResources")) { + fun testGeneratedAccessorsAndCopiedFonts(): Unit = with(testProject("misc/commonResources")) { //check generated resource's accessors gradle("generateComposeResClass").checks { assertEqualTextFiles( @@ -108,11 +108,12 @@ class ResourcesTest : GradlePluginTestBase() { file("src/commonMain/composeResources/drawable/vector_3.xml").renameTo( file("src/commonMain/composeResources/drawable/vector_2.xml") ) + } - //TODO: check a real build after a release a new version of the resources library - //because generated accessors depend on classes from the new version - gradle("assembleDebug", "--dry-run").checks { - check.taskSkipped("copyFontsToAndroidAssets") + @Test + fun testCopyFontsInAndroidApp(): Unit = with(testProject("misc/commonResources")) { + gradle("assembleDebug").checks { + check.taskSuccessful(":copyFontsToAndroidAssets") } } } \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/kotlin/App.kt b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/kotlin/App.kt index f83ac5962e..376fe108ba 100644 --- a/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/kotlin/App.kt +++ b/gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/kotlin/App.kt @@ -1,8 +1,11 @@ import androidx.compose.foundation.layout.* +import androidx.compose.foundation.Image import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.ui.text.font.FontFamily -import generated.resources.Res +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import app.group.resources_test.generated.resources.Res import org.jetbrains.compose.resources.* @OptIn(ExperimentalResourceApi::class) @@ -11,10 +14,10 @@ fun App() { Column { Image( modifier = Modifier.size(100.dp), - painter = painterResource(Res.images.vector), + painter = painterResource(Res.drawable.vector), contentDescription = null ) - Text(getString(Res.strings.app_name)) - val font = FontFamily(Font(Res.fonts.emptyfont)) + Text(stringResource(Res.string.app_name)) + val font = FontFamily(Font(Res.font.emptyfont)) } }