Skip to content

Commit

Permalink
chore: use AGP-blessed API for getting compiled class files instead o…
Browse files Browse the repository at this point in the history
…f bundleTask.

Also refactor and remove dead code.
  • Loading branch information
autonomousapps committed Feb 13, 2024
1 parent 36f114b commit 0850e05
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import com.autonomousapps.internal.OutputPaths
import com.autonomousapps.internal.android.AndroidGradlePluginFactory
import com.autonomousapps.internal.artifactsFor
import com.autonomousapps.internal.utils.capitalizeSafely
import com.autonomousapps.internal.utils.namedOrNull
import com.autonomousapps.model.declaration.SourceSetKind
import com.autonomousapps.services.InMemoryCache
import com.autonomousapps.tasks.*
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.get
Expand Down Expand Up @@ -56,9 +53,6 @@ internal abstract class AndroidAnalyzer(

final override val outputPaths = OutputPaths(project, "$variantName${kind.taskNameSuffix}")

final override val testJavaCompileName: String = "compile${variantNameCapitalized}UnitTestJavaWithJavac"
final override val testKotlinCompileName: String = "compile${variantNameCapitalized}UnitTestKotlin"

final override fun registerByteCodeSourceExploderTask(): TaskProvider<ClassListExploderTask> {
return project.tasks.register<ClassListExploderTask>("explodeByteCodeSource$taskNameSuffix") {
classes.setFrom(project.files())
Expand Down Expand Up @@ -174,19 +168,24 @@ internal class AndroidLibAnalyzer(
variant: AndroidVariant,
agpVersion: String,
androidSources: AndroidSources,
/** Tests and Android Tests don't have ABIs. */
private val hasAbi: Boolean,
) : AndroidAnalyzer(
project = project,
variant = variant,
androidSources = androidSources,
agpVersion = agpVersion
) {

override fun registerAbiAnalysisTask(abiExclusions: Provider<String>): TaskProvider<AbiAnalysisTask> {
override fun registerAbiAnalysisTask(abiExclusions: Provider<String>): TaskProvider<AbiAnalysisTask>? {
if (!hasAbi) return null

return project.tasks.register<AbiAnalysisTask>("abiAnalysis$taskNameSuffix") {
jar.set(getBundleTaskOutput())
exclusions.set(abiExclusions)
output.set(outputPaths.abiAnalysisPath)
abiDump.set(outputPaths.abiDumpPath)
}.also { provider ->
androidSources.wireWithClassFiles(provider)
}
}

Expand All @@ -202,7 +201,4 @@ internal class AndroidLibAnalyzer(
output.set(outputPaths.androidScorePath)
}
}

// TODO stop using bundleTask directly. Fragile.
private fun getBundleTaskOutput(): Provider<RegularFile> = agp.getBundleTaskOutput(variantNameCapitalized)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.android.build.api.artifact.SingleArtifact
import com.android.build.api.variant.ScopedArtifacts
import com.android.build.api.variant.Sources
import com.autonomousapps.model.declaration.Variant
import com.autonomousapps.tasks.ClassListExploderTask
import com.autonomousapps.tasks.AndroidClassesTask
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
Expand All @@ -20,7 +20,6 @@ import java.io.File
*/
internal interface AndroidSources {
val variant: Variant
val agpArtifacts: Artifacts

/** E.g., `debugCompileClasspath` or `debugUnitTestCompileClasspath` */
val compileClasspathConfigurationName: String
Expand All @@ -34,16 +33,27 @@ internal interface AndroidSources {
fun getAndroidRes(): Provider<Iterable<File>>
fun getManifestFiles(): Provider<Iterable<File>>
fun getLayoutFiles(): Provider<Iterable<File>>
fun wireWithClassFiles(task: TaskProvider<ClassListExploderTask>)
fun wireWithClassFiles(task: TaskProvider<out AndroidClassesTask>)
}

@Suppress("UnstableApiUsage")
internal class DefaultAndroidSources(
private val project: Project,
private val agpVariant: com.android.build.api.variant.Variant,
/**
* "Primary" as opposed to UnitTest or AndroidTest sub-variants.
*
* @see [com.android.build.api.variant.Variant.unitTest]
* @see [com.android.build.api.variant.HasAndroidTest.androidTest]
*/
private val primaryAgpVariant: com.android.build.api.variant.Variant,

/**
* The artifacts accessor for the specific sub-variant that this `AndroidSources` instance defines. May be the
* production artifacts (main/debug/release/etc), or the test or androidTest sources.
*/
private val agpArtifacts: Artifacts,
private val sources: Sources,
override val variant: Variant,
override val agpArtifacts: Artifacts,
override val compileClasspathConfigurationName: String,
override val runtimeClasspathConfigurationName: String,
) : AndroidSources {
Expand Down Expand Up @@ -98,20 +108,20 @@ internal class DefaultAndroidSources(

override fun getManifestFiles(): Provider<Iterable<File>> {
// For this one, we want to use the main variant's artifacts
return agpVariant.artifacts.get(SingleArtifact.MERGED_MANIFEST).map {
return primaryAgpVariant.artifacts.get(SingleArtifact.MERGED_MANIFEST).map {
listOf(it.asFile)
}
}

override fun wireWithClassFiles(task: TaskProvider<ClassListExploderTask>) {
override fun wireWithClassFiles(task: TaskProvider<out AndroidClassesTask>) {
// For this one, we want to use the main/test/androidTest variant's artifacts, depending on the source set under
// analysis.
agpArtifacts.forScope(ScopedArtifacts.Scope.PROJECT)
.use(task)
.toGet(
type = ScopedArtifact.CLASSES,
inputJars = ClassListExploderTask::jars,
inputDirectories = ClassListExploderTask::dirs,
inputJars = AndroidClassesTask::jars,
inputDirectories = AndroidClassesTask::dirs,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ internal interface DependencyAnalyzer {
val isDataBindingEnabled: Provider<Boolean>
val isViewBindingEnabled: Provider<Boolean>

val testJavaCompileName: String
val testKotlinCompileName: String

val outputPaths: OutputPaths

fun registerByteCodeSourceExploderTask(): TaskProvider<ClassListExploderTask>
Expand Down Expand Up @@ -104,24 +101,6 @@ internal abstract class AbstractDependencyAnalyzer(
// Always null for JVM projects. May be null for Android projects.
override val testInstrumentationRunner: Provider<String?> = project.provider { null }

protected val testJavaCompile by lazy {
try {
project.tasks.named<JavaCompile>(testJavaCompileName)
} catch (e: UnknownTaskException) {
null
}
}

protected val testKotlinCompile by lazy {
try {
project.tasks.named<KotlinCompile>(testKotlinCompileName)
} catch (e: UnknownTaskException) {
null
} catch (e: NoClassDefFoundError) {
null
}
}

protected fun kaptConf(): Configuration? = try {
project.configurations[kaptConfigurationName]
} catch (_: UnknownDomainObjectException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.autonomousapps.tasks.AbiAnalysisTask
import com.autonomousapps.tasks.ClassListExploderTask
import com.autonomousapps.tasks.FindDeclaredProcsTask
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
Expand Down Expand Up @@ -50,9 +49,6 @@ internal abstract class JvmAnalyzer(

override val outputPaths = OutputPaths(project, variantName)

final override val testJavaCompileName: String = "compileTestJava"
final override val testKotlinCompileName: String = "compileTestKotlin"

final override fun registerByteCodeSourceExploderTask(): TaskProvider<ClassListExploderTask> {
return project.tasks.register<ClassListExploderTask>("explodeByteCodeSource$variantNameCapitalized") {
classes.setFrom(sourceSet.classesDirs)
Expand All @@ -65,10 +61,6 @@ internal abstract class JvmAnalyzer(

return project.tasks.register<AbiAnalysisTask>("abiAnalysis$variantNameCapitalized") {
classes.setFrom(sourceSet.classesDirs)
// These two are only used for Android projects (for now)
javaClasses.setFrom(project.files())
kotlinClasses.setFrom(project.files())

exclusions.set(abiExclusions)
output.set(outputPaths.abiAnalysisPath)
abiDump.set(outputPaths.abiDumpPath)
Expand All @@ -92,9 +84,11 @@ internal abstract class JvmAnalyzer(
private fun getGroovySources(): Provider<Iterable<File>> {
return project.provider { getSourceDirectories().matching(Language.filterOf(Language.GROOVY)) }
}

private fun getJavaSources(): Provider<Iterable<File>> {
return project.provider { getSourceDirectories().matching(Language.filterOf(Language.JAVA)) }
}

private fun getKotlinSources(): Provider<Iterable<File>> {
return project.provider { getSourceDirectories().matching(Language.filterOf(Language.KOTLIN)) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
package com.autonomousapps.internal.android

import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider

internal interface AndroidGradlePlugin {
fun getBundleTaskOutput(variantName: String): Provider<RegularFile>
fun isViewBindingEnabled(): Provider<Boolean>
fun isDataBindingEnabled(): Provider<Boolean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,9 @@ package com.autonomousapps.internal.android

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Provider

internal class AndroidGradlePlugin4_2(
project: Project,
agpVersion: String,
) : BaseAndroidGradlePlugin(project, agpVersion) {

override val bundleTaskType: String = "com.android.build.gradle.internal.tasks.BundleLibraryClassesJar"
override val bundleTaskOutputMethodName: String = "getOutput"

override fun getBundleTaskOutput(variantName: String): Provider<RegularFile> {
val bundleTaskName = "bundleLibCompileToJar$variantName"
val type = getBundleTaskType()
val task = project.tasks.named(bundleTaskName, type)
val outputMethod = getOutputMethod(type)

return task.flatMap {
outputMethod.invoke(it) as RegularFileProperty
}
}
internal class AndroidGradlePlugin4_2(private val project: Project) : AndroidGradlePlugin {

override fun isViewBindingEnabled(): Provider<Boolean> {
return project.provider { project.extensions.getByType(CommonExtension::class.java).viewBinding.enable }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ package com.autonomousapps.internal.android
import org.gradle.api.Project

internal class AndroidGradlePluginFactory(
private val project: Project, private val agpVersion: String
private val project: Project,
agpVersion: String,
) {

private val agp = AgpVersion.version(agpVersion)

fun newAdapter(): AndroidGradlePlugin = when {
agp >= AgpVersion.version("4.2") -> AndroidGradlePlugin4_2(project, agpVersion)
agp >= AgpVersion.version("4.2") -> AndroidGradlePlugin4_2(project)
// Assume latest
else -> AndroidGradlePlugin4_2(project, agpVersion)
else -> AndroidGradlePlugin4_2(project)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ internal fun computeAbi(
abiDumpFile: File? = null
): Set<ExplodingAbi> = getBinaryAPI(classFiles).explodedAbi(exclusions, abiDumpFile)

internal fun computeAbi(
jarFile: File,
exclusions: AbiExclusions,
abiDumpFile: File? = null
): Set<ExplodingAbi> = getBinaryAPI(JarFile(jarFile)).explodedAbi(exclusions, abiDumpFile)

private fun List<ClassBinarySignature>.explodedAbi(
exclusions: AbiExclusions,
abiDumpFile: File? = null
Expand Down
Loading

0 comments on commit 0850e05

Please sign in to comment.