From bea098860c6b9c33e6b8b605746ffbb3a6ff507d Mon Sep 17 00:00:00 2001 From: johnsonlee Date: Tue, 23 May 2023 20:41:43 +0900 Subject: [PATCH] Fix `Project.getJarTaskProviders(BaseVariant?)` with projects do not have `android` extension For example: ``` configurations.maybeCreate("default") artifacts.add("default", file(xxx.aar)) ``` --- .../main/kotlin/com/didiglobal/booster/gradle/Project.kt | 4 ++-- .../main/kotlin/com/didiglobal/booster/gradle/Version.kt | 2 ++ .../kotlin/com/didiglobal/booster/gradle/AGPInterface.kt | 7 +++++++ .../booster/task/analyser/AnalysisVariantProcessor.kt | 4 ++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Project.kt b/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Project.kt index 10995bf87..a00a79772 100644 --- a/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Project.kt +++ b/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Project.kt @@ -131,7 +131,7 @@ fun Project.getJars(variant: BaseVariant? = null): Set = getJarTaskProvide }.flatten().toSet() fun Project.getJarTaskProviders(variant: BaseVariant? = null): Collection> = when { - isAndroid -> when (getAndroid()) { + isAndroid -> when (getAndroidOrNull()) { is LibraryExtension -> filterByVariant(variant).mapNotNull(BaseVariant::createFullJarTaskProvider) is AppExtension -> filterByVariant(variant).mapNotNull(BaseVariant::bundleClassesTaskProvider) else -> emptyList() @@ -141,7 +141,7 @@ fun Project.getJarTaskProviders(variant: BaseVariant? = null): Collection { - val variants = when (val android = getAndroid()) { + val variants = when (val android = getAndroidOrNull()) { is AppExtension -> android.applicationVariants is LibraryExtension -> android.libraryVariants else -> emptyList() diff --git a/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Version.kt b/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Version.kt index 4c920fea4..eea35b813 100644 --- a/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Version.kt +++ b/booster-android-gradle-api/src/main/kotlin/com/didiglobal/booster/gradle/Version.kt @@ -13,3 +13,5 @@ val GTE_V4_2: Boolean by lazy { AGP.revision.major > 4 || (AGP.revision.major == val GTE_V4_1: Boolean by lazy { AGP.revision.major > 4 || (AGP.revision.major == 4 && AGP.revision.minor >= 1) } val GTE_V7_X: Boolean by lazy { AGP.revision.major >= 7 } +val GTE_V7_2: Boolean by lazy { AGP.revision.major > 7 || (AGP.revision.major == 7 && AGP.revision.minor >= 2) } +val GTE_V7_1: Boolean by lazy { AGP.revision.major > 7 || (AGP.revision.major == 7 && AGP.revision.minor >= 1) } diff --git a/booster-android-gradle-compat/src/main/kotlin/com/didiglobal/booster/gradle/AGPInterface.kt b/booster-android-gradle-compat/src/main/kotlin/com/didiglobal/booster/gradle/AGPInterface.kt index 3dc6864d0..35cd83b98 100644 --- a/booster-android-gradle-compat/src/main/kotlin/com/didiglobal/booster/gradle/AGPInterface.kt +++ b/booster-android-gradle-compat/src/main/kotlin/com/didiglobal/booster/gradle/AGPInterface.kt @@ -19,6 +19,7 @@ import com.android.repository.Revision import com.android.sdklib.BuildToolInfo import org.gradle.api.Project import org.gradle.api.Task +import org.gradle.api.UnknownDomainObjectException import org.gradle.api.artifacts.ArtifactCollection import org.gradle.api.artifacts.component.ComponentIdentifier import org.gradle.api.artifacts.result.ResolvedArtifactResult @@ -209,6 +210,12 @@ interface AGPInterface { inline fun Project.getAndroid(): T = extensions.getByName("android") as T +inline fun Project.getAndroidOrNull(): T? = try { + extensions.getByName("android") as? T +} catch (e: UnknownDomainObjectException) { + null +} + private val REVISION: Revision by lazy { Revision.parseRevision(Version.ANDROID_GRADLE_PLUGIN_VERSION) } diff --git a/booster-task-analyser/src/main/kotlin/com/didiglobal/booster/task/analyser/AnalysisVariantProcessor.kt b/booster-task-analyser/src/main/kotlin/com/didiglobal/booster/task/analyser/AnalysisVariantProcessor.kt index e7dba2a8d..9a191e596 100644 --- a/booster-task-analyser/src/main/kotlin/com/didiglobal/booster/task/analyser/AnalysisVariantProcessor.kt +++ b/booster-task-analyser/src/main/kotlin/com/didiglobal/booster/task/analyser/AnalysisVariantProcessor.kt @@ -6,7 +6,7 @@ import com.android.build.gradle.LibraryExtension import com.android.build.gradle.api.BaseVariant import com.android.build.gradle.internal.tasks.factory.dependsOn import com.didiglobal.booster.cha.asm.AsmClassSetCache -import com.didiglobal.booster.gradle.getAndroid +import com.didiglobal.booster.gradle.getAndroidOrNull import com.didiglobal.booster.gradle.getJarTaskProviders import com.didiglobal.booster.gradle.getTaskName import com.didiglobal.booster.gradle.getUpstreamProjects @@ -64,7 +64,7 @@ class AnalysisVariantProcessor : VariantProcessor { } private inline fun Project.setupAndroid() { - when (val android = getAndroid()) { + when (val android = getAndroidOrNull()) { is LibraryExtension -> android.libraryVariants is AppExtension -> android.applicationVariants else -> emptyList()