Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rely on replacing GENERATED_RES to hook up the plugin #383

Merged
merged 5 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
uses: usefulness/agp-version-finder-action@v1

- id: build-agp-matrix
run: echo '::set-output name=agp-versions::["${{ steps.agp-version-finder.outputs.latest-stable }}", "${{ steps.agp-version-finder.outputs.latest-alpha }}"]'
run: echo '::set-output name=agp-versions::["${{ steps.agp-version-finder.outputs.latest-stable }}","${{ steps.agp-version-finder.outputs.latest-beta }}", "${{ steps.agp-version-finder.outputs.latest-alpha }}"]'

build-all-sample-apps:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
gradle-version: ${{ matrix.gradle }}
arguments: assemble lint -PuseMavenLocal -PagpVersion=${{ matrix.agp }}

build-sample-apps-with-confgiuration-cache:
build-sample-apps-with-configuration-cache:
runs-on: ${{ matrix.os }}
name: Configuration Cache Java-${{ matrix.javaVersion }} OS-${{ matrix.os }}
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.project.starter.easylauncher.plugin

import com.android.build.api.component.analytics.AnalyticsEnabledApplicationVariant
import com.android.build.api.dsl.AndroidSourceDirectorySet
import com.android.build.api.dsl.AndroidSourceFile
import com.android.build.api.variant.Variant
import com.android.build.gradle.internal.component.ComponentCreationConfig
import org.gradle.api.model.ObjectFactory
Expand Down Expand Up @@ -34,3 +35,7 @@ internal val Variant.isDebuggable: Boolean
@Suppress("DEPRECATION") // https://issuetracker.google.com/issues/170650362
internal val AndroidSourceDirectorySet.srcDirs
get() = (this as? com.android.build.gradle.api.AndroidSourceDirectorySet)?.srcDirs.orEmpty()

@Suppress("DEPRECATION") // https://issuetracker.google.com/issues/235266670
internal val AndroidSourceFile.srcFile
get() = (this as? com.android.build.gradle.api.AndroidSourceFile)?.srcFile
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.project.starter.easylauncher.plugin

import com.android.build.api.AndroidPluginVersion
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.Variant
import com.android.build.gradle.internal.api.DefaultAndroidSourceFile
import com.android.build.gradle.internal.scope.InternalArtifactType
import com.android.build.gradle.BaseExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.configurationcache.extensions.capitalized
import java.io.File

@Suppress("UnstableApiUsage")
Expand All @@ -25,7 +24,7 @@ class EasyLauncherPlugin : Plugin<Project> {

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

common.sourceSets
Expand Down Expand Up @@ -95,7 +94,8 @@ class EasyLauncherPlugin : Plugin<Project> {
}
.flatten()

val task = project.tasks.register("easylauncher${variant.name.capitalized()}", EasyLauncherTask::class.java) {
val capitalisedVariantName = variant.name.replaceFirstChar(Char::titlecase)
val task = project.tasks.register("easylauncher$capitalisedVariantName", EasyLauncherTask::class.java) {
it.manifestFiles.set(manifests)
it.manifestPlaceholders.set(variant.manifestPlaceholders)
it.resourceDirectories.set(resSourceDirectories)
Expand All @@ -104,11 +104,27 @@ class EasyLauncherPlugin : Plugin<Project> {
it.minSdkVersion.set(variant.minSdkVersion.apiLevel)
}

variant
.artifacts
.use(task)
.wiredWith(EasyLauncherTask::outputDir)
.toCreate(InternalArtifactType.GENERATED_RES)
val apgVersion = androidComponents.pluginVersion
if (apgVersion >= AndroidPluginVersion(7, 4).beta(2)) {
// proper solution, unavailable in 7.3. https://issuetracker.google.com/issues/237303854
variant.sources.res?.addGeneratedSourceDirectory(task, EasyLauncherTask::outputDir)
} else if (apgVersion >= AndroidPluginVersion(7, 3).alpha(1)) {
// has side-effects, but "works". @see: https://github.com/usefulness/easylauncher-gradle-plugin/issues/382
variant
.artifacts
.use(task)
.wiredWith(EasyLauncherTask::outputDir)
.toCreate(com.android.build.gradle.internal.scope.InternalArtifactType.GENERATED_RES)
} else {
// legacy way to hook up the plugin
val generatedResDir = buildDir.resolve("generated/easylauncher/res/${variant.name}")
task.configure { it.outputDir.set(generatedResDir) }
project.afterEvaluate {
val android = extensions.getByName("android") as BaseExtension
android.sourceSets.getByName(variant.name).res.srcDir(generatedResDir)
tasks.named("generate${capitalisedVariantName}Resources") { it.dependsOn(task) }
}
}
}
} else {
log.info { "disabled for ${variant.name}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal object log {

fun info(message: () -> String) {
if (logger.isInfoEnabled) {
logger.debug("[$TAG] $message")
logger.info("[$TAG] ${message()}")
}
}
}
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ subprojects {
// https://github.com/usefulness/easylauncher-gradle-plugin/issues/114
disable("IconLauncherShape")

if (agpVersion.contains("alpha")) {
if (agpVersion.contains("alpha") || agpVersion.contains("beta")) {
disable("MonochromeLauncherIcon")
}

Expand Down Expand Up @@ -174,7 +174,7 @@ subprojects {
// https://github.com/usefulness/easylauncher-gradle-plugin/issues/114
disable("IconLauncherShape")

if (agpVersion.contains("alpha")) {
if (agpVersion.contains("alpha") || agpVersion.contains("beta")) {
disable("MonochromeLauncherIcon")
}

Expand Down
36 changes: 36 additions & 0 deletions sample/example-vector/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import com.android.build.api.variant.ResValue
import com.android.build.api.AndroidPluginVersion
import kotlin.Pair

apply plugin: 'com.starter.application.android'
apply plugin: 'com.starter.easylauncher'

Expand Down Expand Up @@ -49,6 +53,35 @@ android {
register("allFilters") {
dimension "reportedBugs"
}
register("usingResValues") {
dimension "reportedBugs"
}
}
}


androidComponents {
if (pluginVersion >= new AndroidPluginVersion(7, 3).alpha(1) && pluginVersion < new AndroidPluginVersion(7, 4).beta(2)) {
tasks.register("agpHasBugs") {
def resourcesFile = file("src/usingResValues/res/values/generated_strings.xml")

doLast {
resourcesFile.parentFile.mkdirs()
resourcesFile.write("""
<resources>
<string name="generated_key">New AGP apis are broken</string>
</resources>
""")
}
}
tasks.withType(com.android.build.gradle.tasks.GenerateResValues).configureEach {
dependsOn("agpHasBugs")
}
} else {
onVariants(selector().withFlavor(new Pair("reportedBugs", "usingResValues"))) { variant ->
def value = new ResValue(variant.applicationId.get(), "Covers https://github.com/usefulness/easylauncher-gradle-plugin/issues/382")
variant.resValues.put(variant.makeResValueKey("string", "generated_key"), value)
}
}
}

Expand Down Expand Up @@ -103,5 +136,8 @@ easylauncher {
chromeLike(label: "aaa"),
overlayFilter(file("launcherOverlay/beta.png"))
}
usingResValues {
filters chromeLike(label: "res-values", ribbonColor: "#6600CC", labelColor: "#FFFFFF", position: "bottom")
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/example-vector/src/usingResValues/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated_strings.xml
8 changes: 8 additions & 0 deletions sample/example-vector/src/usingResValues/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:description="@string/generated_key"
/>
</manifest>
2 changes: 2 additions & 0 deletions sample/record_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ addToIndex
addToIndex
./../gradlew :example-vector:recordUsingAndroidVersionDebugAndroidTestScreenshotTest --no-configuration-cache
addToIndex
./../gradlew :example-vector:recordUsingResValuesDebugAndroidTestScreenshotTest --no-configuration-cache
addToIndex
echo "Completed"