Skip to content
Closed
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
18 changes: 18 additions & 0 deletions subprojects/project-models/src/main/proto/android.proto
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ message VariantDependencies {
// The key for the map entries is the keys found via [com.android.builder.model.v2.ide.GraphItem.key]
// and [Library.key]
map<string, Library> libraries = 3;

// The dependencies for the variant's unit test artifact, if it has one.
ArtifactDependencies unitTestArtifact = 4;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

NITPICK: The proto copies AGP's deprecated singular test shape rather than the hostTestArtifacts / deviceTestArtifacts maps.

Screenshot tests and AGP 9 test suites cannot be represented, so supporting them needs another schema change and another SYNC_META_VERSION bump.

Consider map<string, ArtifactDependencies> keyed by artifact name, or list it under Limitations.


// The dependencies for the variant's android test artifact, if it has one.
ArtifactDependencies androidTestArtifact = 5;
}

// A custom source directory.
Expand Down Expand Up @@ -222,6 +228,12 @@ message SourceSetContainer {

// The default source provider.
SourceProvider sourceProvider = 1;

// The unit test source provider (src/test), if the module has one.
SourceProvider unitTestSourceProvider = 2;

// The android test source provider (src/androidTest), if the module has one.
SourceProvider androidTestSourceProvider = 3;
}

// View binding options for Android projects.
Expand Down Expand Up @@ -287,6 +299,12 @@ message AndroidVariant {
// The source providers AGP layers over the main source set for this variant: its build
// type, each product flavor, the multi-flavor provider and the variant's own provider.
repeated SourceProvider variantSourceProviders = 3;

// The generated source folders of the variant's unit test artifact (kapt, KSP and the like).
repeated string unitTestGeneratedSourceFolderPaths = 4;

// The generated source folders of the variant's android test artifact.
repeated string androidTestGeneratedSourceFolderPaths = 5;
}

// The Android project model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ fun createAndroidProjectProtoModel(
variantDependencies = variantDependencies.asProtoModel(),
)

// The single-artifact test getters are deprecated but present from AGP 7.3 (our minimum) onwards;
// newer AGP implements them over the host/device test maps.
@Suppress("DEPRECATION")
fun VariantDependencies.asProtoModel() =
VariantDependencies(
name = this.name,
mainArtifact = this.mainArtifact.asProtoModel(),
librariesMap = this.libraries.mapValues { entry -> entry.value.asProtoModel() },
unitTestArtifact = this.unitTestArtifact?.asProtoModel(),

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

NITPICK: Each test artifact runs its own DependencyGraphBuilder, so each test graph re-emits nearly every key string from the main graph.

testImplementation extends implementation, and the sample has 54 and 53 nodes against main's 52, so persisted graph keys per module roughly triple.

If the size matters, share one key table across the three artifacts of a VariantDependencies.

androidTestArtifact = this.androidTestArtifact?.asProtoModel(),
)

fun Library.asProtoModel() =
Expand Down Expand Up @@ -206,7 +211,14 @@ fun UnresolvedDependency.asProtoModel() =
cause = this.cause,
)

fun SourceSetContainer.asProtoModel() = SourceSetContainer(sourceProvider = this.sourceProvider?.asProtoModel())
// See VariantDependencies.asProtoModel for why the deprecated test getters are used.
@Suppress("DEPRECATION")
fun SourceSetContainer.asProtoModel() =
SourceSetContainer(
sourceProvider = this.sourceProvider?.asProtoModel(),
unitTestSourceProvider = this.unitTestSourceProvider?.asProtoModel(),
androidTestSourceProvider = this.androidTestSourceProvider?.asProtoModel(),
)

fun SourceProvider.asProtoModel() =
SourceProvider(
Expand All @@ -226,6 +238,8 @@ fun ViewBindingOptions.asProtoModel() =
isEnabled = this.isEnabled,
)

// See VariantDependencies.asProtoModel for why the deprecated test getters are used.
@Suppress("DEPRECATION")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

NITPICK: @Suppress("DEPRECATION") covers whole functions (here, and at lines 84 and 215), not just the four deprecated test getters.

If AGP later deprecates another member these functions read (mainArtifact, name, libraries), the build stays silent and the removal surfaces as an UnsupportedMethodException during sync instead of a compile warning.

Scope the suppression to each test-getter expression or a small helper.

fun Variant.asProtoModel(
basicAndroidProject: BasicAndroidProject,
androidProject: AndroidProject,
Expand All @@ -244,6 +258,16 @@ fun Variant.asProtoModel(
),
variantSourceProvidersList =
basicAndroidProject.variantSourceProviders(this.name).map { provider -> provider.asProtoModel() },
unitTestGeneratedSourceFolderPathsList =

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

NITPICK: The ?.generatedSourceFolders?.map { it.absolutePath }.orEmpty() block is copied for each test artifact.

Each future per-artifact field will need another copy.

Extract an AbstractArtifact?.generatedSourcePaths() helper.

this.unitTestArtifact
?.generatedSourceFolders
?.map { it.absolutePath }
.orEmpty(),
androidTestGeneratedSourceFolderPathsList =
this.androidTestArtifact
?.generatedSourceFolders
?.map { it.absolutePath }
.orEmpty(),
)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.itsaky.androidide.tooling.impl.serial

import com.android.builder.model.v2.ide.AbstractArtifact
import com.android.builder.model.v2.ide.ArtifactDependencies
import com.android.builder.model.v2.ide.GraphItem
import com.android.builder.model.v2.ide.SourceProvider
import com.android.builder.model.v2.ide.SourceSetContainer
import com.android.builder.model.v2.ide.Variant
import com.android.builder.model.v2.models.VariantDependencies
import com.android.builder.model.v2.models.Versions
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.mockk
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.io.File

/**
* The unit test and android test source providers and compile graphs reach the proto model, so
* the IDE can give test sources their own scopes (ADFA-7115).
*/
@Suppress("DEPRECATION")
@RunWith(JUnit4::class)
class TestArtifactsSerializationTest {
@Test
fun `serializes unit test and android test source providers`() {
val container: SourceSetContainer =
mockk {
every { sourceProvider } returns provider("/p/src/main/java")
every { unitTestSourceProvider } returns provider("/p/src/test/java")
every { androidTestSourceProvider } returns provider("/p/src/androidTest/java")
}

val proto = container.asProtoModel()

assertThat(proto.sourceProvider.javaDirsList).containsExactly("/p/src/main/java")
assertThat(proto.unitTestSourceProvider.javaDirsList).containsExactly("/p/src/test/java")
assertThat(proto.androidTestSourceProvider.javaDirsList).containsExactly("/p/src/androidTest/java")
}

@Test
fun `a module without test source sets leaves the test providers unset`() {
val container: SourceSetContainer =
mockk {
every { sourceProvider } returns provider("/p/src/main/java")
every { unitTestSourceProvider } returns null
every { androidTestSourceProvider } returns null
}

val proto = container.asProtoModel()

assertThat(proto.hasUnitTestSourceProvider()).isFalse()
assertThat(proto.hasAndroidTestSourceProvider()).isFalse()
}

@Test
fun `serializes unit test and android test compile graphs`() {
val dependencies: VariantDependencies =
mockk {
every { name } returns "debug"
every { mainArtifact } returns artifact("main-lib")
every { libraries } returns emptyMap()
every { unitTestArtifact } returns artifact("junit")
every { androidTestArtifact } returns artifact("espresso")
}

val proto = dependencies.asProtoModel()

assertThat(proto.mainArtifact.compileGraph.keyList).containsExactly("main-lib")
assertThat(proto.unitTestArtifact.compileGraph.keyList).containsExactly("junit")
assertThat(proto.androidTestArtifact.compileGraph.keyList).containsExactly("espresso")
}

@Test
fun `a variant without test artifacts leaves the test graphs unset`() {
val dependencies: VariantDependencies =
mockk {
every { name } returns "debug"
every { mainArtifact } returns artifact("main-lib")
every { libraries } returns emptyMap()
every { unitTestArtifact } returns null
every { androidTestArtifact } returns null
}

val proto = dependencies.asProtoModel()

assertThat(proto.hasUnitTestArtifact()).isFalse()
assertThat(proto.hasAndroidTestArtifact()).isFalse()
}

@Test
fun `serializes each test artifact's generated source folders`() {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

MINOR: No test covers a Variant whose unitTestArtifact or androidTestArtifact is null.

That is the path a release-variant sync takes, since androidTest exists only for testBuildType. If a refactor turns ?. into !! or drops .orEmpty(), all five tests still pass and the first release-variant sync throws inside Variant.asProtoModel.

Add a case with both artifacts null that asserts both generated-folder lists are empty.

val variant: Variant =
mockk(relaxed = true) {
every { name } returns "debug"
every { unitTestArtifact } returns generating("/p/build/generated/ksp/debugUnitTest")
every { androidTestArtifact } returns generating("/p/build/generated/ksp/debugAndroidTest")
}
val versions: Versions = mockk { every { agp } returns "8.8.2" }

val proto = variant.asProtoModel(mockk(relaxed = true), mockk(relaxed = true), mockk(relaxed = true), versions)

assertThat(proto.unitTestGeneratedSourceFolderPathsList).containsExactly("/p/build/generated/ksp/debugUnitTest")
assertThat(proto.androidTestGeneratedSourceFolderPathsList).containsExactly("/p/build/generated/ksp/debugAndroidTest")
}

/** A test artifact whose only generated source folder is [dir]. */
private inline fun <reified T : AbstractArtifact> generating(dir: String): T =
mockk(relaxed = true) { every { generatedSourceFolders } returns listOf(File(dir)) }

private fun provider(javaDir: String): SourceProvider =
mockk {
every { javaDirectories } returns listOf(File(javaDir))
every { kotlinDirectories } returns emptyList()
every { resDirectories } returns emptyList()
}

private fun artifact(key: String): ArtifactDependencies {
val item: GraphItem =
mockk {
every { this@mockk.key } returns key
every { requestedCoordinates } returns null
every { dependencies } returns emptyList()
}
return mockk {
every { compileDependencies } returns listOf(item)
every { unresolvedDependencies } returns emptyList()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object ProjectSyncHelper {
* differs from this one discards the sync files and forces a full sync. The models are
* produced and consumed only by the IDE and hold no user data, so discarding them is safe.
*/
const val SYNC_META_VERSION = "2"
const val SYNC_META_VERSION = "3"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

MINOR: The SYNC_META_VERSION KDoc (line 49) says to bump only when a schema changes incompatibly, but this bump is for a purely additive change.

The next developer who adds optional fields follows the KDoc and skips the bump. Cached projects then read the new fields as unset until a manual re-sync, which is the stale-cache bug this bump exists to prevent.

Reword it: bump whenever a persisted proto schema changes, including added fields that consumers rely on.


private val logger = LoggerFactory.getLogger(ProjectSyncHelper::class.java)

Expand Down