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

Add a version skipping mechanism for the AGP patch #111

Merged
merged 2 commits into from
Jan 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@
package app.cash.better.dynamic.features

import com.android.Version
import com.android.build.gradle.internal.crash.afterEvaluate
import org.gradle.api.Plugin
import org.gradle.api.Project

class AgpPatchPlugin : Plugin<Project> {
override fun apply(target: Project) {
val extension = target.extensions.create("betterDynamicFeaturesPatch", BetterDynamicFeaturesPatchExtension::class.java)

target.plugins.withId("com.android.application") {
// Do a strict version check to ensure that our monkey-patching will work correctly.
check(Version.ANDROID_GRADLE_PLUGIN_VERSION == TARGET_AGP_VERSION) {
"This version of the Android Gradle Plugin (${Version.ANDROID_GRADLE_PLUGIN_VERSION}) is not supported by the better-dynamic-features plugin. Only version $TARGET_AGP_VERSION is supported."
target.afterEvaluate {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance this might not be needed, given that this callback should be called only when modules apply the application plugin? i'm always reluctant to add more afterEvaluate blocks 😓

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe push it out of here into its own global check, if it'd be a good idea to ensure an Android plugin is always applied for a module applying this plugin:

https://github.com/cashapp/paparazzi/blob/6eb59e36b756476fe212e38b9dd3df507f56b5f4/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt#L56

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our use case, the agp-patch plugin is only applied to our main application module so that this version check runs.
The actual work of applying the "patch" is done by just including the plugin on the build classpath.

technically this block will also only run if the com.android.application plugin is applied in the first place! (there's a plugins.withId() block)

val agpVersion = Version.ANDROID_GRADLE_PLUGIN_VERSION
// Skip the version check if we ignored the current AGP version!
if (agpVersion in extension.ignoredVersions) {
target.logger.info("Version compatibility check for Android Gradle Plugin $agpVersion skipped.")
return@afterEvaluate
}

// Do a strict version check to ensure that our monkey-patching will work correctly.
check(agpVersion == TARGET_AGP_VERSION) {
"This version of the Android Gradle Plugin ($agpVersion) is not supported by the better-dynamic-features plugin. Only version $TARGET_AGP_VERSION is supported."
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.better.dynamic.features

open class BetterDynamicFeaturesPatchExtension {
internal val ignoredVersions = mutableSetOf<String>()

fun suppressVersionCheck(version: String) {
ignoredVersions += version
}
}