Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Allow Maestro to work on Android API levels 25 and lower (Android 7.1.1 and lower) #1527

Merged
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
2 changes: 1 addition & 1 deletion maestro-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {

defaultConfig {
applicationId "dev.mobile.maestro"
minSdk 21
minSdk 24
targetSdk 31
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.mobile.maestro

import android.os.Build
import android.view.accessibility.AccessibilityNodeInfo

object AccessibilityNodeInfoExt {

/**
* Retrieves the hint text associated with this [android.view.accessibility.AccessibilityNodeInfo].
*
* If the device API level is below 26 (Oreo) or the hint text is null, this function provides a fallback
* by returning an empty CharSequence instead.
*
* @return [CharSequence] representing the hint text or its fallback.
*/
fun AccessibilityNodeInfo.getHintOrFallback(): CharSequence {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && this.hintText != null) {
this.hintText
} else {
""
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.widget.ListView
import android.widget.TableLayout
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import dev.mobile.maestro.AccessibilityNodeInfoExt.getHintOrFallback
import org.xmlpull.v1.XmlSerializer
import java.io.IOException
import java.io.OutputStream
Expand Down Expand Up @@ -120,7 +121,7 @@ object ViewHierarchy {
serializer.attribute("", "NAF", java.lang.Boolean.toString(true))
}
serializer.attribute("", "index", Integer.toString(index))
serializer.attribute("", "hintText", safeCharSeqToString(node.hintText))
serializer.attribute("", "hintText", safeCharSeqToString(node.getHintOrFallback()))
serializer.attribute("", "text", safeCharSeqToString(node.text))
serializer.attribute("", "resource-id", safeCharSeqToString(node.viewIdResourceName))
serializer.attribute("", "class", safeCharSeqToString(node.className))
Expand Down Expand Up @@ -276,4 +277,4 @@ object ViewHierarchy {
}
}

}
}
21 changes: 18 additions & 3 deletions maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ class AndroidDriver(

private fun startInstrumentationSession() {
val startTime = System.currentTimeMillis()
val instrumentationCommand = "am instrument -w -m -e debug false " +
"-e class 'dev.mobile.maestro.MaestroDriverService#grpcServer' " +
"dev.mobile.maestro.test/androidx.test.runner.AndroidJUnitRunner &\n"
val apiLevel = getDeviceApiLevel()

val instrumentationCommand = buildString {
append("am instrument -w ")
if (apiLevel >= 26) append("-m ")
append("-e debug false ")
append("-e class 'dev.mobile.maestro.MaestroDriverService#grpcServer' ")
append("dev.mobile.maestro.test/androidx.test.runner.AndroidJUnitRunner &\n")
}

while (System.currentTimeMillis() - startTime < getStartupTimeout()) {
instrumentationSession = dadb.openShell(instrumentationCommand)
Expand All @@ -105,6 +111,15 @@ class AndroidDriver(
throw AndroidInstrumentationSetupFailure("Maestro instrumentation could not be initialized")
}

private fun getDeviceApiLevel(): Int {
val response = dadb.openShell("getprop ro.build.version.sdk").readAll()
if (response.exitCode != 0) {
throw IOException("Failed to get device API level: ${response.errorOutput}")
}
return response.output.trim().toIntOrNull() ?: throw IOException("Invalid API level: ${response.output}")
}


private fun allocateForwarder() {
PORT_TO_FORWARDER[hostPort]?.close()
PORT_TO_ALLOCATION_POINT[hostPort]?.let {
Expand Down
Binary file modified maestro-client/src/main/resources/maestro-app.apk
Binary file not shown.
Binary file modified maestro-client/src/main/resources/maestro-server.apk
Binary file not shown.