From 4e9074f20560e72d9ea9afb6526fc2e840e6960a Mon Sep 17 00:00:00 2001 From: arunkumar9t2 Date: Mon, 11 Nov 2024 18:03:12 +0800 Subject: [PATCH] Implement support for resource_sets API --- .bazel/.default.bazelrc | 75 +++++++++++------- .bazelversion | 2 +- build.gradle | 2 +- flavors/sample-android-flavor/BUILD.bazel | 46 +++++++---- .../grab/grazel/bazel/rules/AndroidRules.kt | 8 +- .../grazel/migrate/android/AndroidData.kt | 19 ++--- .../migrate/android/AndroidExtractor.kt | 32 ++------ .../migrate/android/AndroidLibraryData.kt | 23 ++++-- .../grazel/migrate/android/AndroidTarget.kt | 10 +-- .../grab/grazel/migrate/android/SourceSet.kt | 65 ++++++++++++++-- .../target/AndroidBinaryTargetBuilder.kt | 4 +- .../target/AndroidLibraryTargetBuilder.kt | 4 +- .../DefaultAndroidLibraryDataExtractorTest.kt | 33 ++++++++ keystore/BUILD.bazel | 15 ++-- sample-android/BUILD.bazel | 76 ++++++++++--------- sample-android/lint_baseline.xml | 30 ++++---- 16 files changed, 284 insertions(+), 160 deletions(-) diff --git a/.bazel/.default.bazelrc b/.bazel/.default.bazelrc index ace5e06f..dab769af 100644 --- a/.bazel/.default.bazelrc +++ b/.bazel/.default.bazelrc @@ -1,12 +1,15 @@ # BzlMod common --enable_bzlmod=false -# Cache +# Cache setup common --disk_cache=bazel-cache +common --remote_cache_compression=true +common --remote_upload_local_results=false # Don't upload results by default startup --digest_function=blake3 -common --experimental_action_cache_store_output_metadata +common --experimental_remote_cache_eviction_retries=10 common --modify_execution_info=JavaDeployJar=+no-remote-cache # Don't cache Java deploy jar which is huge in size common --remote_cache_compression=true +common --experimental_disk_cache_gc_max_size=4G # Error config common --verbose_failures @@ -16,16 +19,12 @@ common --incompatible_strict_action_env common --reuse_sandbox_directories common --repo_env=RJE_VERBOSE=true -# Resources +# JVM External rules +common --@rules_jvm_external//settings:stamp_manifest=False + # Resources common --local_resources=cpu=HOST_CPUS*.75 # Android actions start their own threads which can overwhelm the system -# Workers -common --worker_verbose -common --experimental_worker_multiplex -common --experimental_shrink_worker_pool -common --experimental_worker_for_repo_fetching=platform - # JAVA - START common --experimental_strict_java_deps=off # Turn off strict java deps common --java_runtime_version=remotejdk_17 # Use inbuilt Java 17 for hermeticity @@ -39,6 +38,7 @@ common --experimental_java_header_input_pruning # JAVA - END # Android +common --fat_apk_cpu=arm64-v8a,x86 common --experimental_google_legacy_api common --noincompatible_enable_android_toolchain_resolution common --noincompatible_enable_cc_toolchain_resolution @@ -54,30 +54,53 @@ common --output_library_merged_assets=false # Turn off asset merging artifact # common --experimental_use_package_aware_rtxt=true # Use package aware R.txt files (required for databinding) # common --define=nontransitive_r_class=1 # Adapt bazel common rules for non transitive R class +# Workers +common --worker_verbose +common --worker_multiplex +common --experimental_shrink_worker_pool +common --experimental_worker_for_repo_fetching=platform +common --experimental_collect_worker_data_in_profiler + +## Android Resource Workers common --experimental_persistent_aar_extractor -common --persistent_multiplex_android_tools common --persistent_android_dex_desugar +common --persistent_android_resource_processor +common --persistent_android_dex_desugar +common --persistent_multiplex_android_dex_desugar +common --persistent_multiplex_android_resource_processor +common --persistent_multiplex_android_tools + +# Action Strategies +common --strategy=AARGenerator=worker common --strategy=DatabindingStubs=worker -common --worker_max_instances=Javac=1 -common --worker_max_instances=KotlinCompile=1 -common --worker_max_instances=KotlinKapt=1 +common --modify_execution_info=GenerateDataBindingBaseClasses=+supports-multiplex-workers=1 +## Java Workers +common --strategy=KotlinCompile=worker +common --strategy=Javac=worker +# common --strategy=Turbine=worker +## Worker configuration to avoid CPU thrashing https://github.com/bazelbuild/bazel/issues/8586#issuecomment-500070549 +common --worker_max_instances=Aapt2Optimize=1 common --worker_max_instances=AaptPackage=1 -common --worker_max_instances=AndroidResourceParser=1 +common --worker_max_instances=AndroidAapt2=1 +common --worker_max_instances=AndroidAssetMerger=1 +common --worker_max_instances=AndroidCompiledResourceMerger=1 common --worker_max_instances=AndroidResourceCompiler=1 +common --worker_max_instances=AndroidResourceMerger=1 +common --worker_max_instances=AndroidResourceParser=1 common --worker_max_instances=AndroidResourceValidator=1 common --worker_max_instances=AndroidLintAnalyze=1 common --worker_max_instances=AndroidLint=1 -common --worker_max_instances=RClassGenerator=1 -common --worker_max_instances=AndroidAapt2=1 -common --worker_max_instances=AndroidAssetMerger=1 -common --worker_max_instances=AndroidResourceMerger=1 -common --worker_max_instances=AndroidCompiledResourceMerger=1 -common --worker_max_instances=Aapt2Optimize=1 +common --worker_max_instances=BuildConfigGenerationWorker=1 common --worker_max_instances=DatabindingStubs=1 -common --worker_max_instances=GenerateDataBindingBaseClasses=1 -common --worker_max_instances=DexBuilder=1 +common --worker_max_instances=DatabindingWorker=1 common --worker_max_instances=Desugar=1 - -# JVM -common --strategy=KotlinCompile=worker -common --strategy=Javac=worker \ No newline at end of file +common --worker_max_instances=DexBuilder=1 +common --worker_max_instances=GenerateDataBindingBaseClasses=1 +common --worker_max_instances=Javac=1 +common --worker_max_instances=JdepsMerge=1 +common --worker_max_instances=KotlinCompile=1 +common --worker_max_instances=KotlinKapt=1 +common --worker_max_instances=MergeSourceSets=1 +common --worker_max_instances=ProcessDatabinding=1 +common --worker_max_instances=RClassGenerator=1 +common --worker_max_instances=Turbine=1 diff --git a/.bazelversion b/.bazelversion index 0ee843cc..ba7f754d 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -7.2.0 +7.4.0 diff --git a/build.gradle b/build.gradle index 45330e34..0c7982b6 100644 --- a/build.gradle +++ b/build.gradle @@ -155,7 +155,7 @@ idea { module { excludeDirs += [ file("bazel-bin"), - file("bazel-grazel"), + file("bazel-" + projectDir.name), file("bazel-cache"), file("bazel-out"), file("bazel-testlogs"), diff --git a/flavors/sample-android-flavor/BUILD.bazel b/flavors/sample-android-flavor/BUILD.bazel index 99b1027a..69edb8d6 100644 --- a/flavors/sample-android-flavor/BUILD.bazel +++ b/flavors/sample-android-flavor/BUILD.bazel @@ -19,12 +19,17 @@ android_library( "generated_value": "generated", }, }, - resources = { - "src/demo/res": { + resource_sets = { + "demo": { + "res": "src/demo/res", }, - "src/free/res": { + "free": { + "res": "src/free/res", }, - "src/main/res": { + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ @@ -60,10 +65,14 @@ android_library( "generated_value": "generated", }, }, - resources = { - "src/demo/res": { + resource_sets = { + "demo": { + "res": "src/demo/res", }, - "src/main/res": { + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ @@ -99,12 +108,17 @@ android_library( "generated_value": "generated", }, }, - resources = { - "src/full/res": { + resource_sets = { + "full": { + "res": "src/full/res", }, - "src/free/res": { + "free": { + "res": "src/free/res", }, - "src/main/res": { + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ @@ -140,10 +154,14 @@ android_library( "generated_value": "generated", }, }, - resources = { - "src/full/res": { + resource_sets = { + "full": { + "res": "src/full/res", }, - "src/main/res": { + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/rules/AndroidRules.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/rules/AndroidRules.kt index 0c3e843a..5e9cb2ec 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/rules/AndroidRules.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/bazel/rules/AndroidRules.kt @@ -133,7 +133,7 @@ internal fun StatementsBuilder.androidBinary( enableCompose: Boolean = false, visibility: Visibility = Visibility.Public, resourceFiles: List = emptyList(), - resources: Assignee? = null, + resourceSets: Assignee? = null, resValuesData: ResValuesData, deps: List, assetsGlob: List = emptyList(), @@ -172,7 +172,7 @@ internal fun StatementsBuilder.androidBinary( resConfigFilters.notEmpty { "resource_configuration_filters" `=` resConfigFilters.quote } - resources?.let { "resources" `=` resources } + resourceSets?.let { "resource_sets" `=` resourceSets } deps.notEmpty { "deps" `=` array(deps.map(BazelDependency::toString).quote) } @@ -199,7 +199,7 @@ internal fun StatementsBuilder.androidLibrary( manifest: String? = null, srcsGlob: List = emptyList(), visibility: Visibility = Visibility.Public, - resources: Assignee? = null, + resorceSets: Assignee? = null, resourceFiles: List = emptyList(), enableDataBinding: Boolean = false, enableCompose: Boolean = false, @@ -226,7 +226,7 @@ internal fun StatementsBuilder.androidLibrary( transform = Assignee::asString ) } - resources?.let { "resources" `=` resources } + resorceSets?.let { "resource_sets" `=` resorceSets } deps.notEmpty { "deps" `=` array(deps.map(BazelDependency::toString).map(String::quote)) } diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidData.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidData.kt index 15648e3f..b918c97c 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidData.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidData.kt @@ -19,13 +19,18 @@ package com.grab.grazel.migrate.android import com.grab.grazel.bazel.rules.Multidex import com.grab.grazel.bazel.starlark.BazelDependency +internal data class BazelSourceSet( + val name: String, + val res: String?, + val assets: String?, + val manifest: String?, +) + internal interface AndroidData { val name: String val srcs: List - val res: List + val resourceSets: Set val resValuesData: ResValuesData - val assets: List - val assetsDir: String? val manifestFile: String? // Custom package used for detecting Java/Kotlin sources root @@ -45,10 +50,8 @@ internal interface AndroidData { internal data class AndroidLibraryData( override val name: String, override val srcs: List = emptyList(), - override val res: List = emptyList(), + override val resourceSets: Set = emptySet(), override val resValuesData: ResValuesData = ResValuesData(), - override val assets: List = emptyList(), - override val assetsDir: String? = null, override val manifestFile: String? = null, override val customPackage: String, override val packageName: String, @@ -64,10 +67,8 @@ internal data class AndroidLibraryData( internal data class AndroidBinaryData( override val name: String, override val srcs: List = emptyList(), - override val res: List = emptyList(), + override val resourceSets: Set = emptySet(), override val resValuesData: ResValuesData = ResValuesData(), - override val assets: List = emptyList(), - override val assetsDir: String? = null, override val manifestFile: String? = null, override val customPackage: String, override val packageName: String, diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidExtractor.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidExtractor.kt index 33bc4d1f..4e854efc 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidExtractor.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidExtractor.kt @@ -34,10 +34,7 @@ import com.grab.grazel.gradle.variant.AndroidVariantDataSource import com.grab.grazel.gradle.variant.MatchedVariant import com.grab.grazel.gradle.variant.getMigratableBuildVariants import com.grab.grazel.gradle.variant.nameSuffix -import com.grab.grazel.migrate.android.PathResolveMode.DIRECTORY -import com.grab.grazel.migrate.android.SourceSetType.ASSETS import com.grab.grazel.migrate.android.SourceSetType.JAVA_KOTLIN -import com.grab.grazel.migrate.android.SourceSetType.RESOURCES import com.grab.grazel.migrate.dependencies.calculateDirectDependencyTags import com.grab.grazel.migrate.kotlin.kotlinParcelizeDeps import dagger.Lazy @@ -103,7 +100,6 @@ constructor( matchedVariant: MatchedVariant, extension: BaseExtension, deps: List, - sourceSetType: SourceSetType = JAVA_KOTLIN ): AndroidLibraryData { // Only consider source sets from migratable variants val migratableSourceSets = matchedVariant.variant.sourceSets @@ -113,13 +109,14 @@ constructor( extension, migratableSourceSets ) ?: "" - val srcs = androidSources(migratableSourceSets, sourceSetType).toList() - val res = androidSources(migratableSourceSets, RESOURCES, DIRECTORY).toList().reversed() + val srcs = androidSources(migratableSourceSets, JAVA_KOTLIN).toList() - val assets = androidSources(migratableSourceSets, ASSETS).toList() - val assetsDir = assetsDirectory(migratableSourceSets, assets) + val resourceSets = migratableSourceSets.flatMap { it.toResourceSet(project) } + .reversed() + .toSet() - val manifestFile = androidManifestParser.androidManifestFile(migratableSourceSets) + val manifestFile = androidManifestParser + .androidManifestFile(migratableSourceSets) ?.let(::relativePath) val tags = if (grazelExtension.rules.kotlin.enabledTransitiveReduction) { @@ -138,9 +135,7 @@ constructor( return AndroidLibraryData( name = name + matchedVariant.nameSuffix, srcs = srcs, - res = res, - assets = assets, - assetsDir = assetsDir, + resourceSets = resourceSets, manifestFile = manifestFile, customPackage = packageName, packageName = packageName, @@ -153,19 +148,6 @@ constructor( lintConfigData = lintConfigs ) } - - private fun Project.assetsDirectory( - sourceSets: List, - assets: List - ): String? { - return if (assets.isNotEmpty()) { - val assetItem = assets.first() - sourceSets - .flatMap { it.assets.srcDirs } - .map { relativePath(it) } - .first { assetItem.contains(it) } - } else null - } } internal interface AndroidBinaryDataExtractor : AndroidExtractor diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryData.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryData.kt index ff0f4a61..a973e222 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryData.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidLibraryData.kt @@ -38,14 +38,25 @@ internal fun StatementsBuilder.buildResFiles( } internal fun buildResources( - resDirs: List, -) = if (resDirs.isEmpty()) null else + resourceSets: Set, +) = if (resourceSets.isEmpty()) null else Assignee { add( - statement = resDirs - .groupBy { it } - .mapValues { emptyMap() } - .toObject(quoteKeys = true, quoteValues = true, allowEmpty = true) + statement = resourceSets.groupBy( + keySelector = { it.name }, + valueTransform = { + mapOf( + "res" to it.res, + "assets" to it.assets, + "manifest" to it.manifest + ) + }) + .mapValues { it.value.first() } + .toObject( + quoteKeys = true, + quoteValues = true, + allowEmpty = false + ) ) } diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidTarget.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidTarget.kt index 0a520a2b..cb326df7 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidTarget.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/AndroidTarget.kt @@ -28,7 +28,7 @@ internal interface AndroidTarget : BazelBuildTarget { val enableDataBinding: Boolean val enableCompose: Boolean val projectName: String - val resDirs: List + val resourceSets: Set val resValuesData: ResValuesData val buildConfigData: BuildConfigData val packageName: String @@ -47,7 +47,7 @@ internal data class AndroidLibraryTarget( override val enableDataBinding: Boolean = false, override val enableCompose: Boolean = false, override val projectName: String = name, - override val resDirs: List, + override val resourceSets: Set = emptySet(), override val resValuesData: ResValuesData = ResValuesData(), override val buildConfigData: BuildConfigData = BuildConfigData(), override val packageName: String, @@ -65,7 +65,7 @@ internal data class AndroidLibraryTarget( enableDataBinding = enableDataBinding, enableCompose = enableCompose, srcsGlob = srcs, - resources = buildResources(resDirs), + resorceSets = buildResources(resourceSets), visibility = visibility, deps = deps, tags = tags, @@ -87,7 +87,7 @@ internal data class AndroidBinaryTarget( override val enableDataBinding: Boolean = false, override val enableCompose: Boolean = false, override val projectName: String = name, - override val resDirs: List, + override val resourceSets: Set = emptySet(), override val resValuesData: ResValuesData = ResValuesData(), override val buildConfigData: BuildConfigData = BuildConfigData(), override val packageName: String, @@ -122,7 +122,7 @@ internal data class AndroidBinaryTarget( manifest = manifest, manifestValues = manifestValues, resConfigFilters = resConfigFilters, - resources = buildResources(resDirs), + resourceSets = buildResources(resourceSets), resValuesData = resValuesData, deps = deps, assetsGlob = assetsGlob, diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/SourceSet.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/SourceSet.kt index 2887919e..3a64faee 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/SourceSet.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/android/SourceSet.kt @@ -17,7 +17,14 @@ package com.grab.grazel.migrate.android import com.android.build.gradle.api.AndroidSourceSet -import com.grab.grazel.migrate.android.PathResolveMode.* +import com.android.build.gradle.internal.utils.toImmutableSet +import com.grab.grazel.migrate.android.PathResolveMode.DIRECTORY +import com.grab.grazel.migrate.android.PathResolveMode.FILES +import com.grab.grazel.migrate.android.SourceSetType.ASSETS +import com.grab.grazel.migrate.android.SourceSetType.JAVA +import com.grab.grazel.migrate.android.SourceSetType.JAVA_KOTLIN +import com.grab.grazel.migrate.android.SourceSetType.KOTLIN +import com.grab.grazel.migrate.android.SourceSetType.RESOURCES import com.grab.grazel.util.commonPath import org.gradle.api.Project import java.io.File @@ -51,6 +58,52 @@ enum class PathResolveMode { FILES } +internal fun AndroidSourceSet.toResourceSet( + project: Project +): Set { + val manifestPath = manifest.srcFile.takeIf { it.exists() }?.let(project::relativePath) + + fun File.isValid() = exists() && walk().drop(1).any() + val resources = res.srcDirs.filter(File::isValid) + val assets = assets.srcDirs.filter(File::isValid) + + return if (resources.size == 1 && assets.size == 1) { + // Happy path, most modules would be like this with one single res and assets dir. + setOf( + BazelSourceSet( + name = name, + res = project.relativePath(resources.first()), + assets = project.relativePath(assets.first()), + manifest = manifestPath + ) + ) + } else { + // Special case: res and assets have custom dirs, hence manually map each of them as a source + // set dir for Bazel. + return LinkedHashSet().apply { + resources.mapIndexedTo(this) { index, resDir -> + val sourceSetManifest = if (index == 0) manifestPath else null + BazelSourceSet( + name = name, + res = project.relativePath(resDir), + assets = null, + manifest = sourceSetManifest, + ) + } + assets.mapIndexedTo(this) { index, assets -> + val sourceSetManifest = if (index == 0) manifestPath else null + BazelSourceSet( + name = name, + res = null, + assets = project.relativePath(assets), + manifest = sourceSetManifest, + ) + } + }.toImmutableSet() + } +} + + /** * Given a list of directories specified by `dirs` and list of file patterns specified by `patterns` * will return list of `dir/pattern` where `dir`s has at least one file matching the pattern. @@ -96,17 +149,15 @@ internal fun Project.androidSources( ): Sequence { val sourceSetChoosers: AndroidSourceSet.() -> Sequence = when (sourceSetType) { - SourceSetType.JAVA, SourceSetType.JAVA_KOTLIN, SourceSetType.KOTLIN -> { + JAVA, JAVA_KOTLIN, KOTLIN -> { { java.srcDirs.asSequence() } } - SourceSetType.RESOURCES -> { - { - res.srcDirs.asSequence() - } + RESOURCES -> { + { res.srcDirs.asSequence() } } - SourceSetType.ASSETS -> { + ASSETS -> { { assets.srcDirs .asSequence() diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidBinaryTargetBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidBinaryTargetBuilder.kt index 3396632a..b89e120c 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidBinaryTargetBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidBinaryTargetBuilder.kt @@ -120,10 +120,8 @@ constructor( manifest = androidLibraryData.manifestFile, manifestValues = androidBinaryData.manifestValues, resConfigFilters = androidBinaryData.resConfigs, - resDirs = androidLibraryData.res, + resourceSets = androidLibraryData.resourceSets, resValuesData = androidLibraryData.resValuesData, - assetsGlob = androidLibraryData.assets, - assetsDir = androidLibraryData.assetsDir, buildConfigData = androidLibraryData.buildConfigData, lintConfigData = androidLibraryData.lintConfigData, ) diff --git a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidLibraryTargetBuilder.kt b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidLibraryTargetBuilder.kt index abf37b91..1dd25f51 100644 --- a/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidLibraryTargetBuilder.kt +++ b/grazel-gradle-plugin/src/main/kotlin/com/grab/grazel/migrate/target/AndroidLibraryTargetBuilder.kt @@ -90,7 +90,7 @@ constructor( private fun AndroidLibraryData.toAndroidLibTarget() = AndroidLibraryTarget( name = name, srcs = srcs, - resDirs = res, + resourceSets = resourceSets, deps = deps, enableDataBinding = databinding, enableCompose = compose, @@ -98,8 +98,6 @@ private fun AndroidLibraryData.toAndroidLibTarget() = AndroidLibraryTarget( buildConfigData = buildConfigData, packageName = packageName, manifest = manifestFile, - assetsGlob = assets, - assetsDir = assetsDir, tags = tags, lintConfigData = lintConfigData ) diff --git a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidLibraryDataExtractorTest.kt b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidLibraryDataExtractorTest.kt index 018f254b..b51341c9 100644 --- a/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidLibraryDataExtractorTest.kt +++ b/grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidLibraryDataExtractorTest.kt @@ -14,12 +14,14 @@ import com.grab.grazel.gradle.variant.MatchedVariant import com.grab.grazel.util.addGrazelExtension import com.grab.grazel.util.createGrazelComponent import com.grab.grazel.util.doEvaluate +import com.grab.grazel.util.truth import org.gradle.api.Project import org.gradle.api.provider.Provider import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.the import org.junit.Test +import java.nio.file.Files import kotlin.test.assertEquals class DefaultAndroidLibraryDataExtractorTest { @@ -37,6 +39,7 @@ class DefaultAndroidLibraryDataExtractorTest { it.addGrazelExtension() } appProject = buildProject("android", rootProject) + createSources() libraryProject = buildProject("lib", rootProject) with(appProject) { with(plugins) { @@ -89,6 +92,16 @@ class DefaultAndroidLibraryDataExtractorTest { )) } + private fun createSources() { + val resMain = appProject.file("src/main/res") + resMain.toPath().let { + Files.createDirectories(it) + val values = it.resolve("values") + Files.createDirectories(values) + values.resolve("values.xml").toFile().writeText("") + } + } + private fun debugVariant(): MatchedVariant { val variant = appProject.the() .applicationVariants @@ -127,4 +140,24 @@ class DefaultAndroidLibraryDataExtractorTest { androidLibraryDataExtractor.extract(appProject, debugVariant()).databinding ) } + + @Test + fun `assert resource sets are calulcated for android binary only when file exists`() { + configure() + val resourceSets = androidLibraryDataExtractor + .extract(appProject, debugVariant()) + .resourceSets + resourceSets.truth { + containsExactly( + BazelSourceSet( + name = "main", + res = "src/main/res", + assets = null, + manifest = null + ) + ) + containsNoDuplicates() + hasSize(1) + } + } } \ No newline at end of file diff --git a/keystore/BUILD.bazel b/keystore/BUILD.bazel index bb11ee34..a44148b1 100644 --- a/keystore/BUILD.bazel +++ b/keystore/BUILD.bazel @@ -1,9 +1,10 @@ filegroup( - name = "debug-keystore", - srcs = [ - "debug.keystore", - ], - visibility = [ - "//visibility:public", - ], + name = "debug-keystore", + srcs = [ + "debug.keystore", + ], + visibility = [ + "//visibility:public", + ] ) + diff --git a/sample-android/BUILD.bazel b/sample-android/BUILD.bazel index 417730d9..9ee5a8e3 100644 --- a/sample-android/BUILD.bazel +++ b/sample-android/BUILD.bazel @@ -7,10 +7,6 @@ android_binary( srcs = glob([ "src/main/java/com/grab/grazel/android/sample/**/*.kt", ]), - assets = glob([ - "src/main/assets/assert-file.png", - ]), - assets_dir = "src/main/assets", build_config = { "strings": { "SOME_STRING": "Something", @@ -74,12 +70,18 @@ android_binary( "vi", "zh", ], - resources = { - "src/main/res-debug": { + resource_sets = { + "debug": { + "res": "src/main/res-debug", + "manifest": "src/debug/AndroidManifest.xml", }, - "src/free/res": { + "free": { + "res": "src/free/res", }, - "src/main/res": { + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ @@ -117,10 +119,6 @@ android_binary( srcs = glob([ "src/main/java/com/grab/grazel/android/sample/**/*.kt", ]), - assets = glob([ - "src/main/assets/assert-file.png", - ]), - assets_dir = "src/main/assets", build_config = { "strings": { "SOME_STRING": "Something", @@ -184,12 +182,18 @@ android_binary( "vi", "zh", ], - resources = { - "src/main/res-debug": { + resource_sets = { + "debug": { + "res": "src/main/res-debug", + "manifest": "src/debug/AndroidManifest.xml", }, - "src/paid/res": { + "paid": { + "res": "src/paid/res", }, - "src/main/res": { + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ @@ -227,10 +231,6 @@ android_binary( srcs = glob([ "src/main/java/com/grab/grazel/android/sample/**/*.kt", ]), - assets = glob([ - "src/main/assets/assert-file.png", - ]), - assets_dir = "src/main/assets", build_config = { "strings": { "SOME_STRING": "Something", @@ -294,12 +294,18 @@ android_binary( "vi", "zh", ], - resources = { - "src/main/res-debug": { + resource_sets = { + "debug": { + "res": "src/main/res-debug", + "manifest": "src/debug/AndroidManifest.xml", }, - "src/free/res": { + "free": { + "res": "src/free/res", }, - "src/main/res": { + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ @@ -337,10 +343,6 @@ android_binary( srcs = glob([ "src/main/java/com/grab/grazel/android/sample/**/*.kt", ]), - assets = glob([ - "src/main/assets/assert-file.png", - ]), - assets_dir = "src/main/assets", build_config = { "strings": { "SOME_STRING": "Something", @@ -404,12 +406,18 @@ android_binary( "vi", "zh", ], - resources = { - "src/main/res-debug": { - }, - "src/paid/res": { - }, - "src/main/res": { + resource_sets = { + "debug": { + "res": "src/main/res-debug", + "manifest": "src/debug/AndroidManifest.xml", + }, + "paid": { + "res": "src/paid/res", + }, + "main": { + "res": "src/main/res", + "assets": "src/main/assets", + "manifest": "src/main/AndroidManifest.xml", }, }, visibility = [ diff --git a/sample-android/lint_baseline.xml b/sample-android/lint_baseline.xml index d30630c5..2b617eb5 100755 --- a/sample-android/lint_baseline.xml +++ b/sample-android/lint_baseline.xml @@ -5,7 +5,7 @@ id="LocaleFolder" message="The locale folder "`id`" should be called "`in`" instead; see the `java.util.Locale` documentation"> + file="../../../../armeabi-v7a-fastbuild-android-ST-5b74a929aefd/bin/sample-android/_sample-android-demo-free-debug_res/out/res/values-id"/> @@ -22,10 +22,10 @@ @@ -36,7 +36,7 @@ errorLine1="<manifest xmlns:android="http://schemas.android.com/apk/res/android"" errorLine2=" ~~~~~~~~"> @@ -47,7 +47,7 @@ errorLine1="<manifest xmlns:android="http://schemas.android.com/apk/res/android"" errorLine2=" ~~~~~~~~"> @@ -56,7 +56,7 @@ id="Typos" message="Did you mean "flavor!" instead of "flavor1"?"> @@ -67,7 +67,7 @@ errorLine1="<string name="overridable_resource1">Overridable resource - From free</string>" errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -122,7 +122,7 @@ errorLine1="<string name="overridable_resource1">Overridable resource - From free</string>" errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -133,7 +133,7 @@ errorLine1="<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">" errorLine2="^"> @@ -144,7 +144,7 @@ errorLine1="<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">" errorLine2="^"> @@ -155,7 +155,7 @@ errorLine1=" <EditText" errorLine2=" ~~~~~~~~"> @@ -166,7 +166,7 @@ errorLine1=" <EditText" errorLine2=" ~~~~~~~~"> @@ -177,7 +177,7 @@ errorLine1=" <EditText" errorLine2=" ~~~~~~~~"> @@ -206,7 +206,7 @@ errorLine1=" android:text="Hello World from Grazel!"" errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">