Skip to content

Commit

Permalink
Simplify ADB lookup (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
technoir42 authored Jul 31, 2024
1 parent d448ab9 commit d7af783
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import com.malinskiy.marathon.android.DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS
import com.malinskiy.marathon.android.serial.SerialStrategy
import com.malinskiy.marathon.execution.Configuration
import ddmlibModule
import org.gradle.api.Project
import java.io.File

internal fun createCommonConfiguration(
project: Project,
extensionConfig: MarathonExtension,
sdkDirectory: File
adbPath: File,
outputDir: File
): Configuration = Configuration(
outputDir = project.layout.buildDirectory.dir("reports/marathon").get().asFile,
outputDir = outputDir,
customAnalyticsTracker = extensionConfig.customAnalyticsTracker,
poolingStrategy = extensionConfig.poolingStrategy?.toStrategy(),
shardingStrategy = extensionConfig.shardingStrategy?.toStrategy(),
Expand All @@ -39,10 +38,10 @@ internal fun createCommonConfiguration(
testOutputTimeoutMillis = extensionConfig.testOutputTimeoutMillis,
noDevicesTimeoutMillis = extensionConfig.noDevicesTimeoutMillis,
debug = extensionConfig.debug,
vendorConfiguration = createAndroidConfiguration(extension = extensionConfig, sdkDirectory = sdkDirectory)
vendorConfiguration = createAndroidConfiguration(extensionConfig, adbPath)
)

private fun createAndroidConfiguration(extension: MarathonExtension, sdkDirectory: File): AndroidConfiguration {
private fun createAndroidConfiguration(extension: MarathonExtension, adbPath: File): AndroidConfiguration {
val autoGrantPermission = extension.autoGrantPermission ?: DEFAULT_AUTO_GRANT_PERMISSION
val instrumentationArgs = extension.instrumentationArgs
val applicationPmClear = extension.applicationPmClear ?: DEFAULT_APPLICATION_PM_CLEAR
Expand All @@ -63,7 +62,7 @@ private fun createAndroidConfiguration(extension: MarathonExtension, sdkDirector
val usedStorageThresholdInPercents = extension.usedStorageThresholdInPercents ?: DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS

return AndroidConfiguration(
sdkDirectory,
adbPath,
listOf(ddmlibModule),
autoGrantPermission,
instrumentationArgs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.android.build.api.variant.Component
import com.android.build.api.variant.GeneratesTestApk
import com.android.build.api.variant.TestVariant
import com.android.build.api.variant.Variant
import com.malinskiy.marathon.android.androidSdkLocation
import com.malinskiy.marathon.android.findAdbPath
import com.malinskiy.marathon.worker.MarathonWorker
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -34,7 +34,8 @@ class MarathonPlugin : Plugin<Project> {
tasks.register(WORKER_TASK_NAME, MarathonWorkerRunTask::class.java)

gradle.projectsEvaluated {
val configuration = createCommonConfiguration(project, marathonConfig, androidSdkLocation)
val outputDir = layout.buildDirectory.dir("reports/marathon").get().asFile
val configuration = createCommonConfiguration(marathonConfig, findAdbPath(projectDir), outputDir)
MarathonWorker.initialize(configuration)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.malinskiy.marathon.android

import com.android.SdkConstants.FN_LOCAL_PROPERTIES
import java.io.File
import java.util.*

fun findAdbPath(rootDir: File): File {
val localProperties = File(rootDir, FN_LOCAL_PROPERTIES)
val properties = Properties()

if (localProperties.isFile) {
localProperties.bufferedReader().use {
properties.load(it)
}
}

return findSdkLocation(properties, rootDir)
?.resolve("platform-tools")
?.resolve("adb")
?: throw RuntimeException("SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.")
}

private fun findSdkLocation(properties: Properties, rootDir: File): File? {
var sdkDirProp: String? = properties.getProperty("sdk.dir")
if (sdkDirProp != null) {
var sdk = File(sdkDirProp)
if (!sdk.isAbsolute) {
sdk = rootDir.resolve(sdkDirProp)
}
return sdk
}

sdkDirProp = properties.getProperty("android.dir")
if (sdkDirProp != null) {
return rootDir.resolve(sdkDirProp)
}

val envVar = System.getenv("ANDROID_HOME")
if (envVar != null) {
var sdk = File(envVar)
if (!sdk.isAbsolute) {
sdk = rootDir.resolve(envVar)
}
return sdk
}

val property = System.getProperty("android.home")
return when {
property != null -> File(property)
else -> null
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const val DEFAULT_INSTALL_OPTIONS = ""
const val DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS = 85

data class AndroidConfiguration(
val androidSdk: File,
val adbPath: File,
val implementationModules: List<Module>,
val autoGrantPermission: Boolean = DEFAULT_AUTO_GRANT_PERMISSION,
val instrumentationArgs: Map<String, String> = emptyMap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.newFixedThreadPoolContext
import kotlinx.coroutines.runBlocking
import java.awt.image.BufferedImage
import java.io.File
import java.io.IOException
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.coroutines.CoroutineContext

class DdmlibAndroidDevice(
val ddmsDevice: IDevice,
private val adbPath: String,
private val adbPath: File,
private val track: Track,
private val timer: Timer,
private val androidAppInstaller: AndroidAppInstaller,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.newFixedThreadPoolContext
import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -66,7 +65,7 @@ class DdmlibDeviceProvider(
DdmPreferences.setTimeOut(DEFAULT_DDM_LIB_TIMEOUT)
AndroidDebugBridge.initIfNeeded(false)

val absolutePath = Paths.get(vendorConfiguration.androidSdk.absolutePath, "platform-tools", "adb").toFile().absolutePath
val adbPath = vendorConfiguration.adbPath

listener = object : AndroidDebugBridge.IDeviceChangeListener {
override fun deviceChanged(device: IDevice, changeMask: Int) {
Expand All @@ -76,7 +75,7 @@ class DdmlibDeviceProvider(
val maybeNewAndroidDevice =
DdmlibAndroidDevice(
device,
absolutePath,
adbPath,
track,
timer,
androidAppInstaller,
Expand Down Expand Up @@ -113,7 +112,7 @@ class DdmlibDeviceProvider(
androidAppInstaller = androidAppInstaller,
attachmentManager = attachmentManager,
reportsFileManager = fileManager,
adbPath = absolutePath,
adbPath = adbPath,
logcatListener = logcatListener,
strictRunChecker = strictRunChecker
)
Expand Down Expand Up @@ -181,7 +180,7 @@ class DdmlibDeviceProvider(
}
}
AndroidDebugBridge.addDeviceChangeListener(listener)
adb = AndroidDebugBridge.createBridge(absolutePath, false)
adb = AndroidDebugBridge.createBridge(adbPath.absolutePath, false)
logger.debug { "Created ADB bridge" }

var getDevicesCountdown = config.noDevicesTimeoutMillis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.text.SimpleDateFormat
import java.util.*

class CliLogcatReceiver(
private val adbPath: String,
private val adbPath: File,
private val fileManager: FileManager,
private val device: IDevice,
private val listener: (List<LogCatMessage>) -> Unit
Expand Down Expand Up @@ -43,7 +43,7 @@ class CliLogcatReceiver(
vararg command: String
): Process =
spawnProcess(
command = arrayOf(adbPath, "-s", device.serialNumber) + command,
command = arrayOf(adbPath.absolutePath, "-s", device.serialNumber) + command,
outputTo = redirectOutputTo
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import org.mockito.kotlin.whenever
import java.io.File
import java.time.Clock

class AndroidDeviceSpek : Spek(
Expand All @@ -28,7 +29,7 @@ class AndroidDeviceSpek : Spek(
whenever(iDevice.getProperty("ro.product.model")).thenReturn(null)
DdmlibAndroidDevice(
iDevice,
"",
File("adb"),
track,
timer,
appInstaller,
Expand All @@ -43,7 +44,7 @@ class AndroidDeviceSpek : Spek(
whenever(iDevice.getProperty("ro.product.manufacturer")).thenReturn(null)
DdmlibAndroidDevice(
iDevice,
"",
File("adb"),
track,
timer,
appInstaller,
Expand All @@ -60,7 +61,7 @@ class AndroidDeviceSpek : Spek(
whenever(iDevice.getProperty("ro.build.version.sdk")).thenReturn("INVALID_VERSION")
DdmlibAndroidDevice(
iDevice,
"",
File("adb"),
track,
timer,
appInstaller,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AndroidDeviceTestRunnerSpek : Spek(
val appInstaller = mock<AndroidAppInstaller>()
val device = DdmlibAndroidDevice(
ddmsDevice,
"",
File("adb"),
Track(),
SystemTimer(Clock.systemDefaultZone()),
appInstaller,
Expand Down Expand Up @@ -73,7 +73,7 @@ class AndroidDeviceTestRunnerSpek : Spek(
noDevicesTimeoutMillis = null,
debug = null,
vendorConfiguration = AndroidConfiguration(
androidSdk = File(""),
adbPath = File("adb"),
implementationModules = emptyList()
)
)
Expand Down

0 comments on commit d7af783

Please sign in to comment.