There is another Question similar to this with many answers. Combining the answers helped me reach a solution for Windows 10 on both systems, SDK Emulator and Android Studio. I'm sharing it here since this post comes first when you search for this problem.
Android Studio uses ADB (Android Debug Bridge) for detection and communication of android device. When it starts it will look for local emulated devices on port 555*. Usually its 5554.
It is possible to use ADB's command line to connect to another IP for the emulated device. But sadly the emulator that comes with Android SDK won't bind to 0.0.0.0 and there is no way to change its listening host-name/IP. Meaning that it will only accept local requests.
A port forwarding SSH tunnel will trick Emulator and Android Studio into thinking they're both on the same machine.
If you have SSH working between IDE and Emulator PC skip to "Tunnel is working". If not here is what i found practical:
SSH Client on IDE PC
To avoid typing SSH password every time we are going to use public key authentication.
Open a command prompt type ssh
to see if its already installed.
if not, install it by Settings > Apps > Optional features > Add a features > "OpenSSH Client".
Open Services and set OpenSSH Authentication Agent
to Automatic and start it.
Enter ssh-keygen
on command prompt. keep pressing enter for default rsa key pair and to avoid setting password on the keys.
Open C:\Users\IDE_UserName\.ssh
. Generated public and private keys are placed in 2 separate files.
Open PowerShell in the same directory (Shift + Right Click).
Enter ssh-add
you should see "Identity added". This will add the generated key into Authentication Agent used by the SSH client.
Copy id_rsa.pub
file which contains your public key to your emulator PC. Take note of its location we need it later.
SSH Server on Emulator PC
Install it by Settings > Apps > Optional features > Add a features > "OpenSSH Server".
Or you can get the latest stable version: Here
Using your favorite text editor with admin privileges open C:\ProgramData\ssh\sshd_config
UnComment StrictModes yes
and change it to no
UnComment PubkeyAuthentication yes
UnComment PasswordAuthentication yes
and change it to no
Comment AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
Save and close the config file.
Rename id_rsa.pub
of IDE_PC to authorized_keys
and place it at C:\Users\EMU_UserName\.ssh
.
Open Services and set OpenSSH SSH Server
to Automatic make sure to restart / start it for changes to take effect.
Make sure port 22 is open on emulator PC's firewall.
Back on IDE PC
Our setup is done. We should be able to ssh into Emulator PC without entering a password.
Test it by ssh IdeUser@EmuHostname|ip
like ssh mike@192.168.1.4
In case of any trouble follow a guide like: This
Tunnel is working
Now that SSH is working we can use it to forward ports 5554 and 5555 using the command:
ssh -NL 5554:localhost:5554 -L 5555:localhost:5555 IdeUser@EmuHostname
Credits and Thanks Christopher Orr
Don't close the prompt window as we should keep the port forwarding ssh tunnel open while working. Also note that it's better to open the tunnel before running Android Studio. Otherwise we should restart ADB on IDE_PC.
Finally disable ADB on EMU_PC
By changing ADB location settings in Extented Controls
or if its already set you can do this by renaming or moving ADB.exe
file located at EMU_PC\AndroidSDK\platform-tools
For smoother workflow we can place the tunnel command into a vbs (Visual Basic Script) file to avoid a command prompt forever open in our taskbar like so:
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c ssh -NL 5554:localhost:5554 -L 5555:localhost:5555 IdeUser@EmuHostname"
oShell.Run strArgs, 0, false
When you are done with the tunnel you can close it by killing ssh.exe process.