Browse Source

Configure android resources after the agp is applied and ignore hidden files in resources

pull/4173/head
Konstantin 11 months ago committed by GitHub
parent
commit
3040ea85bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt
  2. 22
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt
  3. 11
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt
  4. 11
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/kotlin/App.kt

8
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<ResourceType, Map<String, List<ResourceItem>>> = 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<File> =
listFiles()?.filter { !it.isHidden }.orEmpty()
}
internal fun String.asUnderscoredIdentifier(): String =

22
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
)
}
}
}

11
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")
}
}
}

11
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))
}
}

Loading…
Cancel
Save