Skip to content

Commit

Permalink
Fix Project.getJarTaskProviders(BaseVariant?) with projects do not …
Browse files Browse the repository at this point in the history
…have `android` extension

For example:

```
configurations.maybeCreate("default")
artifacts.add("default", file(xxx.aar))
```
  • Loading branch information
johnsonlee committed May 23, 2023
1 parent a80f7ae commit bea0988
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fun Project.getJars(variant: BaseVariant? = null): Set<File> = getJarTaskProvide
}.flatten().toSet()

fun Project.getJarTaskProviders(variant: BaseVariant? = null): Collection<TaskProvider<out Task>> = when {
isAndroid -> when (getAndroid<BaseExtension>()) {
isAndroid -> when (getAndroidOrNull<BaseExtension>()) {
is LibraryExtension -> filterByVariant(variant).mapNotNull(BaseVariant::createFullJarTaskProvider)
is AppExtension -> filterByVariant(variant).mapNotNull(BaseVariant::bundleClassesTaskProvider)
else -> emptyList()
Expand All @@ -141,7 +141,7 @@ fun Project.getJarTaskProviders(variant: BaseVariant? = null): Collection<TaskPr
}

private fun Project.filterByVariant(variant: BaseVariant? = null): Collection<BaseVariant> {
val variants = when (val android = getAndroid<BaseExtension>()) {
val variants = when (val android = getAndroidOrNull<BaseExtension>()) {
is AppExtension -> android.applicationVariants
is LibraryExtension -> android.libraryVariants
else -> emptyList<BaseVariant>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -209,6 +210,12 @@ interface AGPInterface {

inline fun <reified T : BaseExtension> Project.getAndroid(): T = extensions.getByName("android") as T

inline fun <reified T : BaseExtension> 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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,7 +64,7 @@ class AnalysisVariantProcessor : VariantProcessor {
}

private inline fun <reified T : AnalysisTask> Project.setupAndroid() {
when (val android = getAndroid<BaseExtension>()) {
when (val android = getAndroidOrNull<BaseExtension>()) {
is LibraryExtension -> android.libraryVariants
is AppExtension -> android.applicationVariants
else -> emptyList<BaseVariant>()
Expand Down

0 comments on commit bea0988

Please sign in to comment.