Skip to content

Commit

Permalink
Fix variant artifacts acquiring
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee authored and neighbWang committed May 19, 2022
1 parent ea824fc commit f42b864
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ val BaseVariant.bundleResourcesTaskProvider: TaskProvider<out Task>?
null
}

val BaseVariant.packageTaskProvider: TaskProvider<out Task>?
get() = try {
project.tasks.named(getTaskName("package"))
} catch (e: UnknownTaskException) {
null
}

val BaseVariant.packageBundleTaskProvider: TaskProvider<out Task>?
get() = try {
project.tasks.named(getTaskName("package", "Bundle"))
Expand Down Expand Up @@ -141,78 +148,78 @@ fun BaseVariant.getArtifactFileCollection(
getArtifactFileCollection(configType, scope, artifactType)
}

val BaseVariant.aar: Collection<File>
val BaseVariant.aar: FileCollection
get() = AGP.run {
aar
}

/**
* The output directory of APK files
*/
val BaseVariant.apk: Collection<File>
val BaseVariant.apk: FileCollection
get() = AGP.run {
apk
}

/**
* The output directory of merged [AndroidManifest.xml](https://developer.android.com/guide/topics/manifest/manifest-intro)
*/
val BaseVariant.mergedManifests: Collection<File>
val BaseVariant.mergedManifests: FileCollection
get() = AGP.run {
mergedManifests
}

/**
* The output directory of merged resources
*/
val BaseVariant.mergedRes: Collection<File>
val BaseVariant.mergedRes: FileCollection
get() = AGP.run {
mergedRes
}

/**
* The output directory of merged assets
*/
val BaseVariant.mergedAssets: Collection<File>
val BaseVariant.mergedAssets: FileCollection
get() = AGP.run {
mergedAssets
}

/**
* The output directory of merged native libs
*/
val BaseVariant.mergedNativeLibs: Collection<File>
val BaseVariant.mergedNativeLibs: FileCollection
get() = AGP.run {
mergedNativeLibs
}

/**
* The output directory of processed resources: *resources-**variant**.ap\_*
*/
val BaseVariant.processedRes: Collection<File>
val BaseVariant.processedRes: FileCollection
get() = AGP.run {
processedRes
}

/**
* All of classes
*/
val BaseVariant.allClasses: Collection<File>
val BaseVariant.allClasses: FileCollection
get() = AGP.run {
allClasses
}

val BaseVariant.symbolList: Collection<File>
val BaseVariant.symbolList: FileCollection
get() = AGP.run {
symbolList
}

val BaseVariant.symbolListWithPackageName: Collection<File>
val BaseVariant.symbolListWithPackageName: FileCollection
get() = AGP.run {
symbolListWithPackageName
}

val BaseVariant.allArtifacts: Map<String, Collection<File>>
val BaseVariant.allArtifacts: Map<String, FileCollection>
get() = AGP.run {
allArtifacts
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ interface AGPInterface {

val BaseVariant.hasDynamicFeature: Boolean

val BaseVariant.rawAndroidResources: Collection<File>
val BaseVariant.rawAndroidResources: FileCollection

val BaseVariant.javaCompilerTaskProvider: TaskProvider<out Task>

Expand Down Expand Up @@ -131,35 +131,35 @@ interface AGPInterface {
artifactType: AndroidArtifacts.ArtifactType
): FileCollection

val BaseVariant.allArtifacts: Map<String, Collection<File>>
val BaseVariant.allArtifacts: Map<String, FileCollection>

val BaseVariant.minSdkVersion: AndroidVersion

val BaseVariant.targetSdkVersion: ApiVersion

val BaseVariant.variantType: VariantType

val BaseVariant.aar: Collection<File>
val BaseVariant.aar: FileCollection

val BaseVariant.apk: Collection<File>
val BaseVariant.apk: FileCollection

val BaseVariant.mergedManifests: Collection<File>
val BaseVariant.mergedManifests: FileCollection

val BaseVariant.mergedRes: Collection<File>
val BaseVariant.mergedRes: FileCollection

val BaseVariant.mergedAssets: Collection<File>
val BaseVariant.mergedAssets: FileCollection

val BaseVariant.mergedNativeLibs: Collection<File>
val BaseVariant.mergedNativeLibs: FileCollection

val BaseVariant.processedRes: Collection<File>
val BaseVariant.processedRes: FileCollection

val BaseVariant.symbolList: Collection<File>
val BaseVariant.symbolList: FileCollection

val BaseVariant.symbolListWithPackageName: Collection<File>
val BaseVariant.symbolListWithPackageName: FileCollection

val BaseVariant.dataBindingDependencyArtifacts: Collection<File>
val BaseVariant.dataBindingDependencyArtifacts: FileCollection

val BaseVariant.allClasses: Collection<File>
val BaseVariant.allClasses: FileCollection

val BaseVariant.buildTools: BuildToolInfo

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.android.build.gradle.internal.publishing.AndroidArtifacts
import com.android.build.gradle.internal.scope.AnchorOutputType
import com.android.build.gradle.internal.scope.GlobalScope
import com.android.build.gradle.internal.scope.InternalArtifactType
import com.android.build.gradle.internal.scope.MissingTaskOutputException
import com.android.build.gradle.internal.scope.VariantScope
import com.android.build.gradle.internal.variant.BaseVariantData
import com.android.builder.core.VariantType
Expand All @@ -37,15 +36,17 @@ private val ARTIFACT_TYPES = arrayOf<Array<out ArtifactType>>(
BuildArtifactType.values(),
SourceArtifactType.values(),
InternalArtifactType.values()
).flatten().map {
it.name() to it
}.toMap()
).flatten().associateBy(ArtifactType::name)

internal object V33 : AGPInterface {

@Suppress("UnstableApiUsage")
private fun BaseVariant.getFinalArtifactFiles(type: ArtifactType): Collection<File> {
return variantScope.artifacts.getFinalArtifactFiles(type).files
private fun BaseVariant.getFinalArtifactFiles(type: ArtifactType): FileCollection {
return try {
variantScope.artifacts.getFinalArtifactFiles(type).get()
} catch (e: Throwable) {
project.files()
}
}

override val scopeFullWithFeatures: MutableSet<in QualifiedContent.Scope>
Expand Down Expand Up @@ -103,8 +104,8 @@ internal object V33 : AGPInterface {
override val BaseVariant.hasDynamicFeature: Boolean
get() = globalScope.hasDynamicFeatures()

override val BaseVariant.rawAndroidResources: Collection<File>
get() = variantData.allRawAndroidResources.files
override val BaseVariant.rawAndroidResources: FileCollection
get() = variantData.allRawAndroidResources

override fun BaseVariant.getArtifactCollection(
configType: AndroidArtifacts.ConsumedConfigType,
Expand All @@ -122,21 +123,13 @@ internal object V33 : AGPInterface {
return variantScope.getArtifactFileCollection(configType, scope, artifactType)
}

override val BaseVariant.allArtifacts: Map<String, Collection<File>>
get() = ARTIFACT_TYPES.entries.map {
val artifacts: Collection<File> by lazy {
try {
getFinalArtifactFiles(it.value)
} catch (e: RuntimeException) {
if (e.cause is MissingTaskOutputException) {
emptyList<File>()
} else {
throw e
}
}
override val BaseVariant.allArtifacts: Map<String, FileCollection>
get() = ARTIFACT_TYPES.entries.associateTo(TreeMap()) { (name, type) ->
val artifacts: FileCollection by lazy {
getFinalArtifactFiles(type)
}
it.key to artifacts
}.toMap(TreeMap())
name to artifacts
}

override val BaseVariant.minSdkVersion: AndroidVersion
get() = variantData.variantConfiguration.minSdkVersion
Expand All @@ -147,45 +140,45 @@ internal object V33 : AGPInterface {
override val BaseVariant.variantType: VariantType
get() = variantScope.type

override val BaseVariant.aar: Collection<File>
override val BaseVariant.aar: FileCollection
get() = getFinalArtifactFiles(InternalArtifactType.AAR)

override val BaseVariant.apk: Collection<File>
override val BaseVariant.apk: FileCollection
get() = getFinalArtifactFiles(InternalArtifactType.APK)

override val BaseVariant.mergedManifests: Collection<File>
override val BaseVariant.mergedManifests: FileCollection
get() = getFinalArtifactFiles(when (this) {
is ApplicationVariant -> InternalArtifactType.MERGED_MANIFESTS
is LibraryVariant -> InternalArtifactType.LIBRARY_MANIFEST
else -> TODO("Unsupported variant type: $variantType")
})

override val BaseVariant.mergedRes: Collection<File>
override val BaseVariant.mergedRes: FileCollection
get() = getFinalArtifactFiles(InternalArtifactType.MERGED_RES)

override val BaseVariant.mergedNativeLibs: Collection<File>
get() = setOf((File("${project.buildDir.path}${File.separatorChar}${AndroidProject.FD_INTERMEDIATES}${File.separatorChar}transforms${File.separatorChar}mergeJniLibs${File.separatorChar}$name")))
override val BaseVariant.mergedNativeLibs: FileCollection
get() = project.files("build${File.separatorChar}${AndroidProject.FD_INTERMEDIATES}${File.separatorChar}transforms${File.separatorChar}mergeJniLibs${File.separatorChar}$name")

override val BaseVariant.mergedAssets: Collection<File>
override val BaseVariant.mergedAssets: FileCollection
get() = getFinalArtifactFiles(when (this) {
is ApplicationVariant -> InternalArtifactType.MERGED_ASSETS
is LibraryVariant -> InternalArtifactType.LIBRARY_ASSETS
else -> TODO("Unsupported variant type: $variantType")
})

override val BaseVariant.processedRes: Collection<File>
override val BaseVariant.processedRes: FileCollection
get() = getFinalArtifactFiles(InternalArtifactType.PROCESSED_RES)

override val BaseVariant.symbolList: Collection<File>
override val BaseVariant.symbolList: FileCollection
get() = getFinalArtifactFiles(InternalArtifactType.SYMBOL_LIST)

override val BaseVariant.symbolListWithPackageName: Collection<File>
override val BaseVariant.symbolListWithPackageName: FileCollection
get() = getFinalArtifactFiles(InternalArtifactType.SYMBOL_LIST_WITH_PACKAGE_NAME)

override val BaseVariant.dataBindingDependencyArtifacts: Collection<File>
override val BaseVariant.dataBindingDependencyArtifacts: FileCollection
get() = getFinalArtifactFiles(InternalArtifactType.DATA_BINDING_DEPENDENCY_ARTIFACTS)

override val BaseVariant.allClasses: Collection<File>
override val BaseVariant.allClasses: FileCollection
get() = getFinalArtifactFiles(AnchorOutputType.ALL_CLASSES)

override val BaseVariant.buildTools: BuildToolInfo
Expand Down
Loading

0 comments on commit f42b864

Please sign in to comment.