Skip to content

Commit

Permalink
Merge pull request #571 from usefulness/agp_8_4_0
Browse files Browse the repository at this point in the history
Use new AGP 8.4.0 apis to fetch variant sources
  • Loading branch information
mateuszkwiecinski authored Mar 10, 2024
2 parents e2ff394 + 33baa61 commit 0d4f53e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ jobs:

- name: Upload screenshot tests result
uses: actions/upload-artifact@v4
if: ${{ always() }}
with:
name: screenshots
path: "${{ github.workspace }}/**/build/*AndroidTest/"
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.canUseVariantManifestSources || !agpVersion.canAccessStaticVariantSources) {
@Suppress("UnstableApiUsage")
androidComponents.finalizeDsl { common ->
if (!agpVersion.canUseVariantManifestSources) {
manifestBySourceSet = common.sourceSets
.mapNotNull { sourceSet -> sourceSet.manifest.srcFile?.let { sourceSet.name to it } }
.toMap()
}

if (!agpVersion.canAccessStaticVariantSources) {
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.canAccessStaticVariantSources) {
variant.sources.res?.static?.map { outer -> outer.flatten().map { inner -> inner.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.canAccessStaticVariantSources get() = this >= AndroidPluginVersion(8, 4).alpha(13)
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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gradle-starter = "0.71.0"
gradle-gradlepublish = "1.2.1"
gradle-doctor = "0.9.2"
google-agp = "8.3.0"
google-agp = "8.4.0-alpha13"
gradle-jacocotestkit = "1.0.12"
mavencentral-junit = "5.10.2"
mavencentral-assertj = "3.25.3"
Expand Down

0 comments on commit 0d4f53e

Please sign in to comment.