Skip to content

Commit d08d3d4

Browse files
juangardi21Copilot
andauthored
Update AGP and dependencies (#19)
* Update AGP and dependencies Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Remove deprecated common library dependency * Address jeprubio's review comments Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix applicationId to use androidTest variant for correct device path Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Move adb result check outside try/catch to avoid double-wrapped exceptions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f59a64e commit d08d3d4

10 files changed

Lines changed: 98 additions & 72 deletions

File tree

‎android-snaptesting/build.gradle.kts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
plugins {
22
alias(libs.plugins.android.library)
3-
alias(libs.plugins.kotlin)
43
}
54

65
android {
@@ -31,13 +30,11 @@ android {
3130
sourceCompatibility = JavaVersion.VERSION_17
3231
targetCompatibility = JavaVersion.VERSION_17
3332
}
34-
kotlinOptions {
35-
jvmTarget = "17"
36-
}
3733
}
3834

3935
kotlin {
4036
explicitApi()
37+
jvmToolchain(17)
4138
}
4239

4340
dependencies {

‎app/build.gradle.kts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
plugins {
22
alias(libs.plugins.android.application)
3-
alias(libs.plugins.kotlin)
43
id("com.telefonica.androidsnaptesting-plugin")
54
}
65

@@ -31,9 +30,10 @@ android {
3130
sourceCompatibility = JavaVersion.VERSION_17
3231
targetCompatibility = JavaVersion.VERSION_17
3332
}
34-
kotlinOptions {
35-
jvmTarget = "17"
36-
}
33+
}
34+
35+
kotlin {
36+
jvmToolchain(17)
3737
}
3838

3939
dependencies {

‎gradle.properties‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,3 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
1717
android.useAndroidX=true
1818
# Kotlin code style for this project: "official" or "obsolete":
1919
kotlin.code.style=official
20-
# Enables namespacing of each library's R class so that its R class includes only the
21-
# resources declared in the library itself and none from the library's dependencies,
22-
# thereby reducing the size of the R class for that library
23-
android.nonTransitiveRClass=true

‎gradle/libs.versions.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[versions]
2-
agp = "8.13.1"
2+
agp = "9.1.0"
33
constraintlayout = "2.2.1"
44
min-sdk = "23"
55
target-sdk = "36"
66
compile-sdk = "36"
77
material = "1.12.0"
8-
kotlin = "2.1.21"
8+
kotlin = "2.3.0"
99
appcompat = "1.7.0"
1010
androidx-junit = "1.2.1"
1111
androidx-monitor = "1.7.2"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Fri Mar 22 10:54:28 CET 2024
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.0-bin.zip
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

‎include-build/gradle-plugin/build.gradle.kts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ dependencies {
1515
implementation(libs.android.gradle)
1616
implementation(libs.android.builder.test.api)
1717
implementation(libs.android.ddmlib)
18-
implementation(libs.android.common)
1918
}
2019

2120
gradlePlugin {

‎include-build/gradle-plugin/src/main/java/com/telefonica/androidsnaptesting/AndroidSnaptestingPlugin.kt‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
package com.telefonica.androidsnaptesting
22

3-
import com.android.build.gradle.TestedExtension
3+
import com.android.build.api.variant.AndroidComponentsExtension
4+
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
45
import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
56
import org.gradle.api.Plugin
67
import org.gradle.api.Project
78
import org.gradle.api.Task
89
import org.gradle.api.file.Directory
10+
import org.gradle.api.provider.Provider
911
import org.gradle.api.provider.ProviderFactory
1012
import java.io.File
1113

1214
class AndroidSnaptestingPlugin : Plugin<Project> {
1315

1416
override fun apply(project: Project) {
17+
val applicationIds = mutableMapOf<String, Provider<String>>()
18+
19+
project.extensions.findByType(ApplicationAndroidComponentsExtension::class.java)
20+
?.onVariants { variant ->
21+
variant.androidTest?.let { androidTest ->
22+
applicationIds["${variant.name}AndroidTest"] = androidTest.applicationId
23+
}
24+
}
25+
1526
project.afterEvaluate {
1627

1728
val deviceProviderInstrumentTestTasks = project.tasks
@@ -21,8 +32,8 @@ class AndroidSnaptestingPlugin : Plugin<Project> {
2132
throw AndroidSnaptestingNoDeviceProviderInstrumentTestTasksException()
2233
}
2334

24-
val extension = project.extensions.findByType(TestedExtension::class.java)
25-
?: throw RuntimeException("TestedExtension not found")
35+
val androidComponents = project.extensions.findByType(AndroidComponentsExtension::class.java)
36+
?: throw RuntimeException("AndroidComponentsExtension not found")
2637

2738
val isRecordMode = project.properties["android.testInstrumentationRunnerArguments.record"] == "true"
2839
val providerFactory: ProviderFactory = project.providers
@@ -32,27 +43,33 @@ class AndroidSnaptestingPlugin : Plugin<Project> {
3243
taskName,
3344
DeviceProviderInstrumentTestTask::class.java,
3445
).get()
35-
registerTasksForVariant(project, taskName, deviceProviderTask, extension, isRecordMode, providerFactory)
46+
val variantName = deviceProviderTask.variantName
47+
val applicationIdProvider = applicationIds[variantName]
48+
?: throw RuntimeException(
49+
"applicationId not found for test variant '$variantName'. " +
50+
"Available variants: ${applicationIds.keys}. " +
51+
"Make sure the plugin is applied to a com.android.application module."
52+
)
53+
registerTasksForVariant(
54+
project, taskName, deviceProviderTask,
55+
androidComponents, applicationIdProvider,
56+
isRecordMode, providerFactory,
57+
)
3658
}
3759
}
3860
}
3961

40-
@Suppress("DEPRECATION")
4162
private fun registerTasksForVariant(
4263
project: Project,
4364
taskName: String,
4465
deviceProviderTask: DeviceProviderInstrumentTestTask,
45-
extension: TestedExtension,
66+
androidComponents: AndroidComponentsExtension<*, *, *>,
67+
applicationIdProvider: Provider<String>,
4668
isRecordMode: Boolean,
4769
providerFactory: ProviderFactory,
4870
) {
4971
val capitalizedVariant = deviceProviderTask.variantName.capitalizeFirstLetter()
50-
51-
val testedVariant = extension.testVariants
52-
.firstOrNull { it.name == deviceProviderTask.variantName }
53-
?: throw RuntimeException("TestVariant not found for ${deviceProviderTask.variantName}")
54-
val applicationIdProvider = providerFactory.provider { testedVariant.applicationId }
55-
val adbExecutablePath = extension.adbExecutable.absolutePath
72+
val adbExecutablePath = androidComponents.sdkComponents.adb.get().asFile.absolutePath
5673

5774
val goldenSnapshotsSourcePath = run {
5875
val variantSourceFolder = deviceProviderTask

‎include-build/gradle-plugin/src/main/java/com/telefonica/androidsnaptesting/DeviceFileManager.kt‎

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package com.telefonica.androidsnaptesting
22

33
import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
44
import com.android.build.gradle.internal.testing.ConnectedDevice
5-
import com.android.ddmlib.CollectingOutputReceiver
6-
import com.android.ddmlib.FileListingService
7-
import com.android.ddmlib.FileListingService.FileEntry
8-
import com.android.ddmlib.IDevice
95
import org.gradle.api.file.RegularFile
106
import org.gradle.api.provider.ProviderFactory
117
import java.io.File
8+
import java.util.concurrent.TimeUnit
129

1310
fun DeviceProviderInstrumentTestTask.deviceFileManager(
1411
applicationId: String,
@@ -23,39 +20,25 @@ class DeviceFileManager(
2320
private val providerFactory: ProviderFactory,
2421
) {
2522

26-
fun pullRecordedSnapshots(
27-
destinationPath: String,
28-
) {
23+
fun pullRecordedSnapshots(destinationPath: String) {
2924
pullSnapshots("recorded", destinationPath)
3025
}
3126

32-
fun pullFailuresSnapshots(
33-
destinationPath: String,
34-
) {
27+
fun pullFailuresSnapshots(destinationPath: String) {
3528
pullSnapshots("failures", destinationPath)
3629
}
3730

3831
fun clearAllSnapshots() {
3932
withConnectedDevices { devices ->
40-
devices.forEach {
41-
val receiver = CollectingOutputReceiver()
42-
it.iDevice.executeShellCommand("rm -rf ${getDeviceAndroidSnaptestingRootAbsolutePath()}", receiver)
43-
println(receiver.output)
33+
devices.forEach { device ->
34+
runAdb(device.serialNumber, "shell", "rm", "-rf", getDeviceAndroidSnaptestingRootAbsolutePath())
4435
}
4536
}
4637
}
4738

48-
private fun String.toFileEntry(): FileEntry {
49-
val parts = this.split("/")
50-
var fileEntry = FileEntry(null, null, FileListingService.TYPE_DIRECTORY, true)
51-
parts.forEach {
52-
fileEntry = FileEntry(fileEntry, it, FileListingService.TYPE_DIRECTORY, false)
53-
}
54-
return fileEntry
55-
}
56-
5739
private fun getDeviceAndroidSnaptestingRootAbsolutePath(): String =
58-
"${FileListingService.DIRECTORY_SDCARD}/Download/android-snaptesting/$applicationId"
40+
"/sdcard/Download/android-snaptesting/$applicationId"
41+
5942
private fun getDeviceAndroidSnaptestingSubfolderAbsolutePath(subFolder: String): String =
6043
"${getDeviceAndroidSnaptestingRootAbsolutePath()}/$subFolder"
6144

@@ -77,25 +60,61 @@ class DeviceFileManager(
7760
androidSnaptestingSubFolderInDevice: String,
7861
destinationPath: String,
7962
) {
80-
val fileEntry = getDeviceAndroidSnaptestingSubfolderAbsolutePath(androidSnaptestingSubFolderInDevice).toFileEntry()
63+
val remotePath = getDeviceAndroidSnaptestingSubfolderAbsolutePath(androidSnaptestingSubFolderInDevice)
8164
withConnectedDevices { devices ->
82-
devices.forEach {
83-
pullFolderFiles(
84-
fileEntry,
85-
it.iDevice,
86-
destinationPath,
87-
)
65+
devices.forEach { device ->
66+
val serial = device.serialNumber
67+
val lsResult = runAdbCapture(serial, "shell", "ls", remotePath, logErrors = false)
68+
val fileNames = lsResult.output.lines()
69+
.map { it.trim() }
70+
.filter { it.isNotBlank() && !it.startsWith("ls:") && !it.contains("No such file") }
71+
fileNames.forEach { fileName ->
72+
runAdb(serial, "pull", "$remotePath/$fileName", "$destinationPath/$fileName")
73+
}
8874
}
8975
}
9076
}
9177

92-
private fun pullFolderFiles(
93-
androidSnaptestingDeviceFolder: FileEntry,
94-
device: IDevice,
95-
destinationPath: String,
96-
) {
97-
device.fileListingService.getChildrenSync(androidSnaptestingDeviceFolder).forEach {
98-
device.pullFile(it.fullPath, "$destinationPath/${it.name}")
78+
private fun runAdb(serial: String, vararg args: String) {
79+
val result = runAdbCapture(serial, *args, throwOnError = true)
80+
println(result.output)
81+
}
82+
83+
private fun runAdbCapture(
84+
serial: String,
85+
vararg args: String,
86+
throwOnError: Boolean = false,
87+
logErrors: Boolean = true,
88+
): AdbResult {
89+
val command = buildList {
90+
add(adbExecutablePath)
91+
add("-s")
92+
add(serial)
93+
addAll(args.toList())
9994
}
95+
val result = try {
96+
val process = ProcessBuilder(command)
97+
.redirectErrorStream(false)
98+
.start()
99+
val output = process.inputStream.bufferedReader().readText()
100+
val error = process.errorStream.bufferedReader().readText()
101+
val finished = process.waitFor(60, TimeUnit.SECONDS)
102+
val exitCode = process.exitValue()
103+
AdbResult(output, error, exitCode, finished)
104+
} catch (e: Exception) {
105+
val message = "Exception running ADB command: ${command.joinToString(" ")}\n${e.message}"
106+
if (throwOnError) throw RuntimeException(message, e)
107+
else if (logErrors) println(message)
108+
return AdbResult("", e.message ?: "", -1)
109+
}
110+
111+
if (!result.finished || result.exitCode != 0) {
112+
val message = "ADB command failed: ${command.joinToString(" ")}\nExit code: ${result.exitCode}\nOutput: ${result.output}\nError: ${result.error}"
113+
if (throwOnError) throw RuntimeException(message)
114+
else if (logErrors) println(message)
115+
}
116+
return result
100117
}
118+
119+
private data class AdbResult(val output: String, val error: String, val exitCode: Int, val finished: Boolean = true)
101120
}

‎include-build/gradle/libs.versions.toml‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
[versions]
2-
agp = "8.4.1"
3-
common = "31.4.1"
4-
ddmlib = "31.4.1"
5-
kotlin = "1.9.23"
2+
agp = "9.1.0"
3+
ddmlib = "32.1.0"
4+
kotlin = "2.3.0"
65
detekt = "1.23.6"
76
publish-plugin = "1.2.0"
87

98
[libraries]
109
android-builder-test-api = { module = "com.android.tools.build:builder-test-api", version.ref = "agp" }
11-
android-common = { module = "com.android.tools:common", version.ref = "common" }
1210
android-ddmlib = { module = "com.android.tools.ddms:ddmlib", version.ref = "ddmlib" }
1311
android-gradle = { module = "com.android.tools.build:gradle", version.ref = "agp" }
1412

‎mavencentral.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ publishing {
1717
artifactId 'androidsnaptesting'
1818
version version
1919

20-
artifact("$buildDir/outputs/aar/android-snaptesting-release.aar")
20+
artifact("${layout.buildDirectory.get().asFile}/outputs/aar/android-snaptesting-release.aar")
2121
artifact androidSourcesJar
2222

2323
pom {

0 commit comments

Comments
 (0)