Skip to content

Commit

Permalink
Default espresso-device's emulator host connection to '10.0.2.2' inst…
Browse files Browse the repository at this point in the history
…ead of localhost.

PiperOrigin-RevId: 529543940
  • Loading branch information
brettchabot committed May 4, 2023
1 parent d5c5fa3 commit f3389d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ interface EmulatorGrpcConn {
fun <T : AbstractFutureStub<T>?> emulatorFutureStub(builder: (Channel) -> T): T

companion object {
val EMULATOR_ADDRESS = "localhost"
val EMULATOR_ADDRESS = "10.0.2.2"
val ARGS_EMULATOR_ADDRESS = "grpc.host"
val ARGS_GRPC_PORT = "grpc.port"
val ARGS_GRPC_TOKEN = "grpc.token"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ internal class DeviceControllerModule {

private fun getEmulatorConnection(): EmulatorGrpcConn {
val args = InstrumentationRegistry.getArguments()
var defaultEmulatorAddress = EmulatorGrpcConn.EMULATOR_ADDRESS
var grpcPortString = args.getString(EmulatorGrpcConn.ARGS_GRPC_PORT)
var grpcPort =
if (grpcPortString != null && grpcPortString.toInt() > 0) {
grpcPortString.toInt()
} else {
getEmulatorGRPCPort()
}
var grpcPort: Int
if (grpcPortString != null && grpcPortString.toInt() > 0) {
grpcPort = grpcPortString.toInt()
} else {
grpcPort = getEmulatorGRPCPortFromSystem()
defaultEmulatorAddress = "localhost"
}
var emulatorAddressArg = args.getString(EmulatorGrpcConn.ARGS_EMULATOR_ADDRESS)
var emulatorAddress =
if (emulatorAddressArg != null) emulatorAddressArg else EmulatorGrpcConn.EMULATOR_ADDRESS
if (emulatorAddressArg != null) emulatorAddressArg else defaultEmulatorAddress

return EmulatorGrpcConnImpl(
emulatorAddress,
Expand All @@ -91,8 +93,12 @@ internal class DeviceControllerModule {
)
}

/* Gets the emulator gRPC port for emulators that do not specify a port through instrumentation args */
private fun getEmulatorGRPCPort(): Int {
/**
* Gets the default emulator gRPC port set directly in system properties, for emulators whose port
* is configured dynamically at launch. If set, this port always implies a default emulator
* address of 'localhost'
*/
private fun getEmulatorGRPCPortFromSystem(): Int {
val gRpcPort =
if (getDeviceApiLevel() >= 17) {
Settings.Global.getString(
Expand Down

0 comments on commit f3389d1

Please sign in to comment.