How to set the Android emulator DNS server from Android Studio - android

Essentially my issue is that when I run emulator -verbose -avd Nexus_5X_API_19 in the command line the emulator starts up with the argument -dns-server = "w,x,y,z" where w,x,y,z are 4 ip addresses for DNS servers. When I run ipconfig /all I only see x,y,z listed as my valid DNS servers in Windows. Because of this odd first DNS server, I am unable to access the internet within the emulator. When I run the emulator with emulator -verbose -avd Nexus_5X_API_19 -dns-server "x,y,z" everything works fine.
But now I want to be able to run my app from within Android Studio 2.2.3 with the corrected DNS servers. So does anyone know how to specify the emulator command line arguments within Android Studio (similar to this answer for the older Eclipse based version: https://stackoverflow.com/a/4736518/1088659), or how to set the default DNS for the emulator to start with?

Unfortunately, as of 3.0.1, this isn't possible. They removed adding additional arguments for emulators launched from Android Studio. Until they add it back in, starting the emulator from the command line (as you showed) is the only option.
You can track this issue here: https://issuetracker.google.com/issues/37071385

If you are on MacOS or Linux, you could rename the Android Emulator executable to something else (say emulator-binary) and create a script with the actual emulator name (emulator) in its place, that calls the executable with the -dns-server parameter.
Here are the steps required:
Find the path where the Android SDK is located in your system. This answer will help you find it.
cd <your-SDK-path>/emulator.
Rename the original executable: mv emulator emulator-binary.
Finally, create an emulator shell script named emulator with the following contents:
<your-SDK-path>/emulator-binary -dns-server "8.8.8.8,8.8.4.4" $#

Unfortunately, I ran into the same issue. I wrote a simple script that pulls the DNS address from settings automatically. Script was written for MacOS, but should work for Linux just the same. Note: You may need to change the path to the Android Emulator script.
#!/bin/bash
set -m
dns=$(grep "nameserver" /etc/resolv.conf | grep "\." | awk '{split($0,a," "); print a[2]}')
echo "Running emulator with DNS address: $dns"
~/Library/Android/sdk/emulator/emulator -avd $1 -dns-server $dns -no-snapshot-load &>/dev/null &
disown
You just need to give it your emulator name (note: spaces become underscores), e.g.:
runemu Pixel_4_API_30
where "runemu" is what I named the script on my machine.

Related

Running the modifed Goldfish kernel on Android from Source

I am trying to run a emulator from the Android source by loading the goldfish kernel with some of my configuration changes. Though I found a lot of questions on this, but could not find my answer.
1. I downloaded the android source.
2. I also successfully built my goldfish kernel. Now I can see the zImage in the /goldfish/arch/arm/boot folder.
After this I tried running this emulator by referring to a lot of similar questions.
Building Android from source - emulator and AVDs, Running emulator after building Android from source, How to compile android goldfish 3.4 kernel and run on emulator. But unfortunately didn't solve my problem. When I try to run the emulator with :
./emulator -debug init -kernel Thepath/goldfish/arch/arm/boot/zImage -avd firstAvd -wipe-data
I get an error that AVD is not found. I then found this blog, https://yaapb.wordpress.com/2012/09/22/build-a-custom-android-emulator-image/ where it says to first make the emulator from the source tree by the following commands:
$ source build/envsetup.sh
$ lunch full-eng
$ make -j4
$ emulator -wipe-data &
Then to run the emulator with my modified kernel, I need to run :
$ cd ${ANDROID_BUILD_TOP}
$ emulator -kernel ~/workspace/android/goldfish/arch/arm/boot/zImage -wipe-data &
Though I am trying to do this, the process of creating the emulator with the make command is taking hours. 7% in 90 mins till now. So in the mean time I wanted to know if anyone can tell me that to run an emulator with my kernel, do I need to first create an emulator from the source? Also, I dont find the system.img, ramdisk.img files in the android source tree. Is it normal or I ge them when I create an emulator?
**
EDIT
**
I would also like to know if I can run the emulator without the make -j4. The Android source tree also includes the SDK. Can I run the Emulator from the SDK. If yes, how? The google official docs say to run the following command form the /tools folder in the sdk. But there is no tools folder in the sdk folder.
After you have built the sources, you should not run build process once again in order to use the emulator. Simply go to the folder with your AOSP code and run the following commands:
$ cd ${ANDROID_BUILD_TOP}
$ source build/envsetup.sh
$ emulator -kernel ~/workspace/android/goldfish/arch/arm/boot/zImage -wipe-data &
The command source build/envsetup.sh will add additional commands to your shell, and you should be able to run emulator without building it one more time from scratch.

Cannot test my AOSP build on the emulator

I built the Android AOSP tree for the mako device successfully on my Ubuntu 14.04 LTS desktop with the '. build/envset.sh', 'lunch mako-userdebug', and 'make -j4' series of commands. I tried to run it in the emulator by following the Google-provided instructions (i.e. typing the 'emulator' command in a terminal right after the build is complete) but it does not work. The emulator starts up but is stuck on a black screen. I noticed the following warning/errors while the emulator starts up:
emulator: control console listening on port 5554, ADB on port 5555
emulator: can't connect to ADB server: Transport endpoint is not connected (errno = 107)
Any help in resolving this error is greatly appreciated.
Phil,
To make an image for the emulator, you will need to make one of the generic images. e.g. lunch aosp_arm-eng (you can also us userdebug, etc). It may not show up in the lunch menu, but it should be available nonetheless. The emulator was made strictly to test the changes made in the framework and to the default system properties (those that are hardware independent). Building the arm version should fix your errors!
Best!

Eclipse Android SDK ERROR: unknown virtual device name

So, the issue i correctly installed eclipse,android sdk,ndk, coocs2d-x 2.1.4. I can build cocos2d-x android project, import it to eclipse, etc. Everything seems fine, but not really. The problem is that i can't launch my virtual machine to emulate andoid OS. I created avd, it works fine with simple android projects, but when i use it to run cocos2d-x project as android application it does nothing. No output at all. I found this guide and followed it. But i faced a problem. 1st of all, when i follow this step:
Now start the Android for x86 Intel Emulator using the following command:
$ <SDK directory>/tools/emulator-x86 -avd Your_AVD_Name -qemu -m 2047 -enable-kvm
i have an error message:
gloomist-CN tools # ./emulator-x86 #myavd -qemu -m 2047 -enable-kvm
emulator: ERROR: unknown virtual device name: 'myavd'
emulator: could not find virtual device named 'myavd'
But it is in sdk as a valid android virtual device. And the next one. In that tutorial that i pasted link to above, the last step is:
Run the emulator with the correct library path and options
Make sure to
Set the library path to the directory containing the Open GL library for the emulator (LD_LIBRARY_PATH=...)
Run the x86 version of the emulator (run /tools/emulator-x86)
Enable GPU use (Use the option "-gpu on")
Also, you need to specify the name of the Android Virtual Device to use (Use the option "-avd ")
On OS X the command line looks like this
LD_LIBRARY_PATH=~/bin/android-sdk/tools/lib ~/bin/android-sdk/tools/emulator-x86 -verbose -avd android17x86 -gpu on
i don't really know what author meant by all this. Could anyone explain me the way to do it in linux?
So basically, the main problem is: how to run a cocos2d-x project on AVD in eclipse on lixnux? That's it.
I use Linux version 3.2.0-23-generic (buildd#palmer) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu4) ) #36-Ubuntu SMP Tue Apr 10 20:41:14 UTC 2012 (Ubuntu 3.2.0-23.36-generic 3.2.14).
Can anyone help me to figure out how can i accurately launch my cocos2d-x project on AVD using eclipse?
Are you sure 'myavd' is a valid avd?
Try to run this command to get a list of AVD's you have created in your system:
# <SDK directory>/tools/android list avd
And re-run emulator-x86 passing a valid avd.
I also suggest you follow the step by step guide about how to configure your android enviroment which comes with cocos2d-x dist. You can find it in: /samples/Cpp/TestCpp/proj.android/README.md
Hope this helps
I was able to fix this error on Ubuntu 18.04 by running:
sudo chmod 777 /dev/kvm
Its not exactly high security, but it gets the job done.

Installing an apk on android emulator on Mac OS

I tried following these steps(from here):
go to sdk folder, then go to tools.
copy your apk file inside the tool directory
./emulator -avd myEmulator
to run the emulator on mac
./adb install myApp.apk
to install app on the emulator
But when I run "./emulator -avd phoneEmulator" (phoneEmulator is the name of my emulator)
it just displays:
My-MacBook-Pro:tools myName$ ./emulator -avd phoneEmulator
2012-07-30 22:44:33.377 emulator-arm[2859:80b] Warning once: This application, or a
library it uses, is using NSQuickDrawView, which has been deprecated.
Apps should cease use of QuickDraw and move to Quartz.
It's as if that command is never returning.
If I can't input the 2nd command ./adb install myApp.apk in that window, where do I call it?
Open Terminal
go to android-sdk-mac\platform-tools
type ./adb install myApp.apk
You can work around by:
You have a *.apk file with you if you don't have, download it.
Open Android Studio and Run the emulator.
Open the finder and go to the location which contains your *.apk file.
Drag that *.apk and drop into the emulator and it will ask for confirmation after your confirmation it will install the *.apk in the emulator.
You can find it in application menu once installation succeeds.
First of all, you SHOULD NOT copy APKs to the tools or any other SDK directory.
Do not cd to the SDK directories, just add them to the PATH environment var or use their absolute path in command lines.
Then, if you want to run the emulator and then execute command from the same terminal, send the process to background (&):
$ export PATH=$PATH:/path/to/sdk/tools:path/to/sdk/platform-tools
$ emulator -avd myEmulator &
$ adb install /path/to/my.apk
You can work around by:
Upload yourapp.apk to internet. I.e: yourhost.com/yourapp.apk
Run emulator
Open internet browser on your emulator, visit link yourhost.com/yourapp.apk
Download and install
adb can be run from a seperate terminal once this emulator boots up.
May Be, Useful to all the command in MAC OS Terminal
Am I the only one whose apps install in the emulator when I run them as Android Application from eclipse?
I mean - why go though the trouble of adb when you could just use eclipse? (unless you have something against eclipse!)
Try this app. It will install the apk by just double clicking on it after you create an association to .apk files. It's all explained in the github source.
Alternatively You can use 'adb install' command as follows. "adbinstall/filename.apk"
ex. adb install /Users/manojclinberg/Downloads/ad3e0e46-d716-4fa9-b603-b8db3accf260.apk

How do I launch the Android emulator from the command line?

I'm on Mac, working on Android development from the terminal. I have successfully created the HelloWorld project and now I'm trying to run it from the command line in the Android emulator. Which command runs the emulator for my HelloWorld project?
I already have the Android tools and platform-tools in my PATH.
Edit:
How do I tell the emulator to run my HelloWorld project from the command line? I've already built the project with ant.
I assume that you have built your project and just need to launch it, but you don't have any AVDs created and have to use command line for all the actions. You have to do the following.
Create a new virtual device (AVD) for the platform you need. If you have to use command line for creating your AVD, you can call android create avd -n <name> -t <targetID> where targetID is the API level you need. If you can use GUI, just type in android avd and it will launch the manager, where you can do the same. You can read more about AVD management through GUI and through command line.
Run the AVD either by using command emulator -avd <name> or through previously launched GUI. Wait until the emulator fully loads, it takes some time. You can read about additional options here.
Now you have to install the application to your AVD. Usually during development you just use the same Ant script you used to build the project, just select install target. However, you can install the application manually using command adb install <path-to-your-APK>.
Now switch to emulator and launch your application like on any normal device, through the launcher. Or, as an alternative, you can use the following command: adb shell am start -a android.intent.action.MAIN -n <package>/<activity class>. For example: adb shell am start -a android.intent.action.MAIN -n org.sample.helloworld/org.sample.helloworld.HelloWorld. As a commenter suggested, you can also replace org.sample.helloworld.HelloWorld in the line above with just .HelloWorld, and it will work too.
List all your emulators:
emulator -list-avds
Run one of the listed emulators with -avd flag:
emulator -avd name-of-your-emulator
where the emulator executable is under:
${ANDROID_SDK}/tools/emulator
Or, for newer SDKs, try:
cd ${ANDROID_HOME}/emulator
emulator -avd name-of-your-emulator
I think the best way to reach it via terminal is :
cd ~/Library/Android/sdk/emulator
Or for Windows:
cd %userProfile%\AppData\Local\Android\sdk\emulator
To run a certain AVD directly:
./emulator -avd {AVD_NAME}
To list your AVDs use :
./emulator -list-avds
Just to add here, whenever you get "error: device offline" means that connection with emulator & adb bridge has been broken due to time taken in emulator startup.
Rather than re-starting emulator at this point try below two commands which stops & start adb bridge again.
adb kill-server
adb start-server
Here you can check the options to execute this command via command-line:
emulator -avd avd_name [-option [value]] ... [-qemu args]
For example, I use it like this below:
emulator -avd ICS_ARM -wipe-data -no-boot-anim -cpu-delay 0
open CMD
Open Command Prompt
type the path of emulator in my case
C:\adt-bundle-windows-x86_64-20140702\sdk\tools
write "emulator -avd emulatorname" in my case
emulator -avd AdilVD
If you are strictly trying to run the emulator from the command line try this in OSX.
"/Applications/Android Studio.app/sdk/tools/emulator" -avd <NAMEOFDEVICE> -netspeed full -netdelay none
You can simplify it by adding an alias to the .bash_profile, and sending it to a background job.
alias android='/Applications/Android\ Studio.app/sdk/tools/emulator <NAMEOFDEVICE> -netspeed full -netdelay none &'
Let Bash know about the changes.
source ~/.bash_profile
Instructions for Mac with zsh:
Open terminal window (CTRL+ALT+T)
Run command
nano ~/.zshrc
to edit your profile
Add following lines in the opened file:
export ANDROID_SDK_HOME="~/Library/Android/Sdk"
alias emulator="$ANDROID_SDK_HOME/emulator/emulator"
Save the file (CTRL+O, CTRL+X)
Source the profile by running command source ~/.zshrc or just log out and log back in
Test by running the command:
emulator -help or emulator -list-avds
to show your simulator in terminal
and run Android emulator with command:
emulator -avd <name>
NOTE: Should be same for bash by replacing .zshrc with .bashrc
Open command prompt and go to <android-Home>\sdk\tools>emulator -avd <AVD_NAME>
Here "emulator" is the command used to open your Android Virtual Device.
If you have Git Bash you can open your emulator with a double-click, with some initial setup (tested in Windows 10 only):
Open Git Bash.
Run cd ~/AppData/Local/Android/sdk/emulator
Run ./emulator -list-avds and keep note of the emulator's name. MY_EMULATOR for example.
Open Notepad and add these two lines (insert your emulator's name instead of MY_EMULATOR):
cd ~/AppData/Local/Android/sdk/emulator; ./emulator -avd MY_EMULATOR
Save the file somewhere convinient as myfile.sh. You can change "myfile" as suits you best, but the extension .sh is needed.
Open Git Bash at the directory where you stored myfile.sh and run chmod +x myfile.sh. Remember to put your file's name instead of myfile.
And that's it! From now on, everytime you need to start that emulator, just locate your myfile.sh and double click on it and wait for your Android emulator to launch!
To list all the devices installed: emulator -list-avds
This will list all the devices installed inside your Android Studio.
To run emulator: emulator -avd device_name
For example, if you got Pixel_3a_API_30_x86 after running the command in option 1, then your command to run emulator will be: emulator -avd Pixel_3a_API_30_x86
The android create avd command is deprecated. It's now recommended to use avdmanager instead to launch emulators from the command line.
First, create a new emulator if one doesn't already exist:
avdmanager create avd --name "MyEmulator" -k "system-images;android-
26;google_apis;x86"
This assumes that you already have an X86 system image installed that matches API 26, and has the Google APIs installed.
You can then launch the emulator with emulator #MyEmulator.
Open CMD
To get all Emulator list (C:\Users\DJango\AppData\Local\Android\sdk\emulator>emulator -list-avds)
Choose any emulator
(C:\Users\DJango\AppData\Local\Android\sdk\emulator>emulator -avd Pixel_2_API_28)
(Linux)
In you terminal Copy & Paste and changing the three fields in blue
/home/YouUser/Folder/adt-bundle-linux-x86_64-20140702/sdk/tools/emulator64-x86 -avd Android5.1.1
YouUser = user of session in linux
Folder = path of folfer
Android5.1.1 = You version of android in the emulator,for example (Android4.4.2)
you can view the emulators in you folder home
cd /home/Youuser/.android/avd/
Nowadays asuming you have Android Studio installed (2.2) in my case and just 1 emulator you might use this one liner
export ANDROID_SDK_ROOT=~/Library/Android/sdk/ && emulator '#'`emulator -list-avds`
If you do this often, make it easier:
$ echo 'export ANDROID_SDK_ROOT=~/Library/Android/sdk/' >> ~/.profile
Add an alias to ~.aliases
alias androidup="emulator '#'`emulator -list-avds`"
Recall to source ~/.profile ~/.aliases before testing it
Next time just $ androidup
Go to the directory where emulator is located
${ANDROID_SDK}/tools/emulator
Check emulator list from CLI
emulator -list-avds
Run a AVD from the list
emulator -avd {avd_name}
list of avd name available on
.android/avd
/SDK_PATH/tools/emulator -netdelay none -netspeed full -avd
"AVD_NAME"
In windows, I use this PowerShell script to start it up.
$em = $env:USERPROFILE+"\AppData\Local\Android\sdk\tools\emulator.exe";
Start-Process $em " -avd Nexus_5X_API_24" -WindowStyle Hidden;
For automation (CI) we did something like below steps:
Frist, find SDK's tools location, and store in variable for later use.
tools=$ANDROID_HOME/cmdline-tools/latest/bin
arch="x86_64"
package="system-images;android-23;google_apis;$arch"
Ensure Android-image's downloaded.
$tools/sdkmanager "$package"
Accept the licenses.
echo yes | $tools/sdkmanager --licenses
Create AVD.
echo no | $tools/avdmanager create avd --force --name MyAVD --abi google_apis/$arch --package "$package"
Says "no" to "Do you wish to create a custom hardware profile? ..."
Launch emulator parallel.
$ANDROID_HOME/emulator/emulator -netdelay none -netspeed full -no-snapshot-load -no-snapshot -avd MyAVD > /dev/null &
Build APK.
./gradlew assembleDebug
At last, Android-tests (Instrumented tests) automatically detect and run on Emulator.
./gradlew connectedAndroidTest
Note that because we launch Emulator parallel, you need to wait until Emulator is ready, and that before Step #7.
Maybe count build-time with script, and if build takes less than what Emulator-launch takes, call sleep.
I am late, here but want to share so may be it help some one and me too when ever needed later :) ,
So below is the way to open emulator from command line with one command using bash script.
I am using MX Linux but process is same on all operating systems
1- First Check the installed emulators
emulator -list-avds
it will result like below
emulator -list-avds
Nexus_4_API_28
Pixel_2_API_28
2- open any plain text or code editor and create a new file and write as below
#!/bin/sh
emulator -avd Nexus_4_API_28
Nexus_4_API_28 is the emulator that i want to open you write yours which you got from first step
save this file with .sh extension
3- Then, change the permissions on the file to make it executable:
chmod u+x emu.sh
4- Now open the emulator just executing this bash script file with following command
./emu.sh
For one-click (BATCH file) launch, this is what I've done:
got the name of AVD from Android Studio -> Tools -> AVD Manager -> Click on Arrow Down and choose View Details ( for me it's Pixel_2_API_28, so change accordingly below)
create an start.bat file and put the following inside:
c:
cd C:\Program Files (x86)\Android\android-sdk\emulator\
emulator -avd Pixel_2_API_28
Explanations:
First line: for me was necessary because I am launching it from d:
Second line: for me was not working the tools\emulator
Third line:
make sure you change the name of the AVD with yours (here
Pixel_2_API_28)
Using:
launch start.bat from command line
easily type this command in cmd.
replace after Users\ your user name \
if you don't have this file reinstall android studio.
On Mac (and Linux I think), after you have created your AVD, you can make an alias:
alias run-android='~/Library/Android/sdk/tools/emulator -avd ${YOUR_AVD_NAME} &'
Note: the execution of the alias will not lock your terminal, if you want that, just remove the last '&'.
Run emulator it self will give you an error because he expect that, in your current position, you have: /emulator/qemu/${YOUR_PATFORM}/qemu-system-x86_64' to start the emulator.
I wrote this simple shell script using Zenity that lets you pick which avd you want to run. If you don't have ANDROID_HOME defined, you can just replace that with the full path to emulator.
This would be easy to do with select instead of Zenity also, but I opted for Zenity since I'm running it from the xfce-application menu (though a .desktop-file).
#!/bin/sh
opt=$(zenity --title="Choose AVD" --text="Choose which emulator to start" --list \
--column="Devices" --separator="\n" `$ANDROID_HOME/emulator/emulator -list-avds`);
$ANDROID_HOME/emulator/emulator -avd $opt
Run the first emulator available:
~/Library/Android/sdk/tools/emulator -avd `~/Library/Android/sdk/tools/emulator -list-avds | awk '{print $1}'`
A launcher script which lists existing AVDs and lets you pick the one you want. Requires python3 (at least v3.4) and a valid environment variable ANDROID_HOME or ANDROID_SDK pointing to the Android SDK dir.
#!/usr/bin/env python3
import os
import subprocess
from pathlib import Path
android_emulator_dir: Path = Path(os.environ['ANDROID_HOME'] or os.environ['ANDROID_SDK']) / 'emulator'
if android_emulator_dir.exists():
emulator_dir = android_emulator_dir.absolute()
print(f'SDK emulator dir: {emulator_dir}', end='\n\n')
proc = subprocess.Popen(['./emulator', '-list-avds'], stdout=subprocess.PIPE, cwd=emulator_dir, text=True)
avds = {idx: avd_name.strip() for idx, avd_name in enumerate(proc.stdout, start=1)}
print('\n'.join([f'{idx}: {avd_name}' for idx, avd_name in avds.items()]))
avd_idx = input("\nType AVD index and press Enter... ")
avd_name = avds.get(int(avd_idx))
if avd_name:
subprocess.Popen(['./emulator', '-avd', avd_name, '-no-boot-anim'], cwd=emulator_dir)
else:
print('Invalid AVD index')
else:
print(f'Either $ANDROID_HOME or $ANDROID_SDK must be defined!')
AppleScript version (MacOS only)
osascript -e '
set avds to paragraphs of (do shell script "~/Library/Android/sdk/emulator/emulator -list-avds")
set avd to (choose from list avds with prompt "Please select an AVD to start" default items "None" OK button name {"Start"} cancel button name {"Cancel"})
do shell script "~/Library/Android/sdk/emulator/emulator -avd " & avd & " -no-boot-anim > /dev/null 2>&1 &"
'
The script above can also be run from the Script Editor.app or from Automator.app (the bit between the single quotes). Also you can use Script Editor to save it as a native macos app: select File on the menu, press and hold the ⌥ (Option) key, select Save As and then select File Format: Application.
windows 10 what helped me:
1 cd C:\Users\xxx\AppData\Local\Android\Sdk\emulator
2 list all the available: emulator -list-avds
3 start as NON-GUI mode: emulator -avd Pixel_3a_API_30_x86 -no-window
For Windows:
Search Environment in Windows Search Bar and choose "Edit the System Environment Variable"
In the System Properties window, click on Environment Variable and specify the value of the PATH environment variable:
C:\Users\{USER}\AppData\Local\Android\Sdk\emulator.
Now open CMD or Powershell and type:
emulator -list-avds
This will show list of all AVDs.
To run specific AVD, type in CMD/Powershell: emulator -avd {EmulatorName}
If you have added correct path of the android emulator, AVD will open.
If you already have emulator and wanted to use that using command just use this 1 line code only
cd C:\Users\yourUserName\AppData\Local\Android\Sdk\emulator | ./emulator -avd Pixel_5_API_32
Thanks me Later...
Starting emulator from command line with dns help
Emulator program location : /Users/{{UserName}}/Library/Android/sdk/tools
Check existing avd :-
emulator -list-avds
Start emulator with dns set
/Users/{{UserName}}/Library/Android/sdk/tools/emulator -avd Pixel_API_26 -dns-server 8.8.8.8

Categories

Resources