Skip to content

Commit

Permalink
Use new AGP 8.4.0 apis to fetch variant sources
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Mar 8, 2024
1 parent e2ff394 commit 6174712
Showing 1 changed file with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,24 @@ class EasyLauncherPlugin : Plugin<Project> {
}
}

val manifestBySourceSet = mutableMapOf<String, File>()
val resSourceDirectoriesBySourceSet = mutableMapOf<String, Set<File>>()

@Suppress("UnstableApiUsage")
androidComponents.finalizeDsl { common ->
common.sourceSets
.mapNotNull { sourceSet -> sourceSet.manifest.srcFile?.let { sourceSet.name to it } }
.forEach { manifestBySourceSet[it.first] = it.second }

common.sourceSets
.map { sourceSet -> sourceSet.name to sourceSet.res.srcDirs }
.forEach { resSourceDirectoriesBySourceSet[it.first] = it.second }
lateinit var manifestBySourceSet: Map<String, File>
lateinit var resSourceDirectoriesBySourceSet: Map<String, Set<File>>

if (!agpVersion.canUseNewResources || !agpVersion.canUseNewSources) {
@Suppress("UnstableApiUsage")
androidComponents.finalizeDsl { common ->
if (!agpVersion.canUseNewResources) {
manifestBySourceSet = common.sourceSets
.mapNotNull { sourceSet -> sourceSet.manifest.srcFile?.let { sourceSet.name to it } }
.toMap()
}

if (!agpVersion.canUseNewSources) {
resSourceDirectoriesBySourceSet = common.sourceSets
.map { sourceSet -> sourceSet.name to sourceSet.res.srcDirs }
.toMap()
}
}
}

androidComponents.onVariants { variant ->
Expand Down Expand Up @@ -107,15 +113,22 @@ class EasyLauncherPlugin : Plugin<Project> {
}
}

val resSourceDirectories = resSourceDirectoriesBySourceSet
.mapNotNull { (name, files) ->
if (relevantSourcesSets.contains(name)) {
files
} else {
null
}
val resSourceDirectories = if (agpVersion.canUseNewSources) {
variant.sources.res?.static?.map { outer -> outer.flatten().map { innter -> innter.asFile } }
?: project.provider { emptyList() }
} else {
project.provider {
resSourceDirectoriesBySourceSet
.mapNotNull { (name, files) ->
if (relevantSourcesSets.contains(name)) {
files
} else {
null
}
}
.flatten()
}
.flatten()
}

val capitalisedVariantName = variant.name.replaceFirstChar(Char::titlecase)
val task = project.tasks.register("easylauncher$capitalisedVariantName", EasyLauncherTask::class.java) {
Expand All @@ -124,7 +137,8 @@ class EasyLauncherPlugin : Plugin<Project> {
it.resourceDirectories.set(resSourceDirectories)
it.filters.set(filters)
it.customIconNames.set(customIconNames)
it.minSdkVersion.set(variant.minSdkCompat(agpVersion))
@Suppress("DEPRECATION")
it.minSdkVersion.set(if (agpVersion.canUseNewMinSdk) variant.minSdk.apiLevel else variant.minSdkVersion.apiLevel)
}

if (agpVersion.canUseNewResources) {
Expand Down Expand Up @@ -164,9 +178,7 @@ class EasyLauncherPlugin : Plugin<Project> {
private val AndroidPluginVersion.canUseVariantManifestSources get() = this >= AndroidPluginVersion(8, 3, 0)
private val AndroidPluginVersion.hasDebuggableProperty get() = this >= AndroidPluginVersion(8, 3, 0)
private val AndroidPluginVersion.canUseNewResources get() = this >= AndroidPluginVersion(7, 4).beta(2)
private val AndroidPluginVersion.canUseNewSources get() = this >= AndroidPluginVersion(8, 4).alpha(12)
private val AndroidPluginVersion.hasBrokenResourcesMerging get() = this >= AndroidPluginVersion(7, 3).alpha(1)
private val AndroidPluginVersion.canUseNewMinSdk get() = this >= AndroidPluginVersion(8, 1, 0)
}

@Suppress("DEPRECATION")
private fun Variant.minSdkCompat(agpVersion: AndroidPluginVersion) =
if (agpVersion >= AndroidPluginVersion(8, 1)) minSdk.apiLevel else minSdkVersion.apiLevel

0 comments on commit 6174712

Please sign in to comment.