Android Studio: Fast(er) Emulator
Eventually, I’ll let you know why Android on the SOCMAKER blog. Hint: USB OTG and PSOC5.
Android Studio Program (ASP) is a mess. The IntelliJ IDE may be good, but Google has burdened it so badly it can barely function on a 3.6ghz 8 core I9 with 40gb of RAM and a 2 terabyte Fusion drive (128gb flash for most often used disk accesses). On a i7 8gb 2ghz Macbook Air, it is painful, but it does work.
The initial ASP’s were fairly fast, but the servers they need are now taken offline, and you can no longer develop on them. They are good for looking around. The VSCode System does not easily integrate into a complex Java/C++ Android app development, so for me it is only good for looking around.
If you want any speed at all in your application, you have to use JNI (Java Native Interface) and C/C++. If you investigate the Android source code, you will find it jumps into native code as fast as it can in order to get the work done for the GUI. Think about it. The conclusion is Interpreted Java is so slow it takes a 2ghz processor to its knees. (If you look at the specs for most phones, their processors run about 2ghz.)
One good thing the current Android Studio does is create JNI interfaces for you automatically, so you don’t have to go looking up the strange, esoteric, JNI format. The other thing that ASP does for you is on the fly check for errors in your code. It also tracks usage and you can go back and forth as you work. These features allow me to use it. I put up with it being slow as molasses in December.
Starting up an emulator in ASP is also a very slow process. It takes quite some time. Minutes. (About the same length of time as your very first build in ASP.) Longer than it takes to start ASP itself the second time on a project. That is saying something, as waiting for ASP to start will put most senior citizens to sleep. I know, I am one.
Fast(er) Emulator
The free solution for casual debugging is Android-x86.org. You can download a 32 bit or 64 bit emulator that runs on VIrtualBox or VMWare. I use VMWare, which is now free on Mac for open source developers. However, on the Macintosh, it will not work out of the box. You can see some YouTube videos on it. The symptom is a black screen on your vmware booted machine. I will give you the list of things to do here to fix it. (The slax is optional; you can user other linuxes, but it is a very compact linux live boot machine.)
- Quit Android Studio. You will have to restart it later if you don’t.
- Download Android X86 ( 64 bit. Typically choose 64 bit unless you have older machine and know what you are doing.)
- Download Linux SLAX, same 32/64 flavor as above.
- Drag the Android iso to the VMWare create new VM interface to set it as its boot CD
- Choose Linux 5.x kernel
- Customize the machine. (click on the customize button)
- Choose video size 768 meg, 3d Accelerator hardware
- Choose 2 processors, 2048 megabyte of memory (minimum, you can choose more)
- Change the Hard Disk from SCSI to SATA
- At boot, choose “Advanced” using arrow keys and enter key on the GUI Android menu
- Choose Automatic install.
- After it finishes, reboot and shut down the Android machine
- Point the CD/DVD drive to the SLAX iso file.
- In the “Startup disk menu” choose the CD. (available on VMWare Fusion 13)
- Boot slax
- Click on the start menu on the task bar (left hand 4 box icon)
- Choose file manager from the list of icons.
- In File Manager, double click on “media.” Double click on “sda1.” Double click on “grub.”
- Double click on “menu.lst”
- Change the text for the first line to include “nomodeset” and “xforcevesa_” after the kernel selection. (see the image following)
- Save, then reboot.
- You should see the android logo and boot into a GUI screen. You can experiment with settings on android.
Debug With Android-X86
Boot into Android X86. With the Android-x86 virtual machine having focus, hit Alt-F1. You should see a terminal console. Type “ifconfig” and press enter. You should see a list of ip addresses. The wlan0 “inet” address is what you are interested in:
In my case, the address is 192.168.97.134. Open a Terminal window. (On the Mac, it is /Applications/Utilities/Terminal.app).
If you have installed ASP and not moved it from the “default” installation, you can run adb from your terminal window like this:
mymachine:~ myname$ ~/Library/Android/sdk/platform-tools/adb connect 192.168.97.134
connected to 192.168.97.134:5555
If you see the connected message, all is good. Otherwise, locate the path to the adb program and try again.
Android Studio Restart
If you prevously quit Android Studio, launch. If you did not, quit ASP, and re-launch it. You should see the VMWare Android x86 as a Physical Machine listed on your Android Studio bar, as shown:
At this point, you can treat this machine as a standard tablet emulator, without play store support. I noticed that you have to update the Google infrastructure on Android x86 or it will keep complaining with pop-up banners. Once you do, the Android Play Store app crashes every time you use it. However, that is not what I am using the emulator for, so it is no big deal for me.
If you don’t start the emulator and connect using adb before starting ASP, good luck with that. Fortunately, an ASP re-launch (and one cup of coffee later), you are ready to go. It is, overall, much faster than the Android Emulators. Both startup and loading apps for testing is much faster. Don’t expect it to work for any Play Store interactions, that part is still wonky.
Android Black Screen Part Deux
If your emulator shows a black screen after a few minutes of inactivity, simply reboot it. ADB will stay connected, and you can resume your debugging. This is a very good feature of adb, it automatically reconnects.
If you are not doing any GUI debugging, you can still debug app and step into it while the screen is black. You can’t do any GUI interaction, but the debugging of core code will still work.
To lengthen the time between issues, turn off screen saver on the Android VM by going to settings>display. Also set Display>Sleep to never, or to 30 minutes. In my case, only “30 minutes” was available.
Fini
Enjoy!