An important part of App Inventor development is testing code using real projects, including testing with the AI2 Companion. The Companion can be run either on a real device, or via the Android Emulator.
App Inventor has a set of Setup Tools that permit people to use the Android Emulator instead of a device for doing App Inventor Development. These tools consist of a selection of tools from the Android Software Development Kit, including the Emulator itself and the “adb” tool used to communicate via the Android Debug Bridge. For AI2 we also distribute a helper program called the “aiDaemon.” The aiDaemon is used by the browser to perform those actions that normal browser security restrictions do not permit the browser itself to perform.
The aiDaemon is distributed as an executable, but it is infact a compiled python program.
The Android Emulator itself consists of a program that emulates the Android hardware on your PC along with a set of virtual disks that emulate the storage devices found on real Android devices. For App Inventor we distribute a version of Android 1.6 as a virtual disk. Because running the emulator modifies the disk, and some modifications may result in a non-working emulator, the App Inventor tools make a fresh copy of the virtual disk each time you invoke the emulator. The distribution contains a “canonical” image which is located in one location on your computer and it is copied to your personal directory and run from there each time. This canonical disk contains a copy of the MIT AI2 Companion application.
Although this approach to distributing the necessary programs to run the emulator works for most users, it is particularly difficult for developers who often need to build their own version of the MIT AI2 Companion that matches their own development source.
The purpose of this document is to help you customize the setup tools to make this process a bit easier. We will specifically discuss how this is done on a Mac, but the techniques also apply to Linux and Windows, just the file pathnames will be different.
Rather than running the aiDaemon program in the Setup Tools, it is far easier to run the python script directly out of the App Inventor source tree. All that is required is a version of Python. The aiDaemon.py script is located in appinventor/misc/emulator-support/aiDaemon.py. It is tested and known to work with Python version 2.7, the version that is distributed as part of MacOS 10.8. NOTE: It will *not* work with Python 3.X!
Start a terminal program and go to the directory where the aiDaemon source resides.
(where “...” is the parent directory of where you have your copy of the App Inventor source tree).
and the aiDaemon should start in that window.
You will also be able see what the program is doing while it is running when you run it in a terminal, often useful for debugging communication problems between your PC and the emulator (or phone when connected via USB).
Distributed with the Setup Tools is the script “run-emulator.” On the Mac it is located in /Applications/AppInventor/commands-for-Appinventor/run-emulator. It is a simple shell script that sets up and starts the emulator itself. It is invoked from the aiDaemon.py script.
The canonical copy of the emulator disk is located in /Applications/AppInventor/extras/Appinventor-emulator-data. This is copied by the run-emulator script into your home directory under ~/.appinventor/emulator/Appinventor-emulator-data.
You should run the script at least once, so you have a copy of the disk in your home directory. You can then edit the run-emulator script (you will likely need root access on your machine as the /Applications/AppInventor directory is protected) to comment out the copying of the canonical disk to your home directory. This can be found on a lines that looks like:
if [ ! -d $DATAFILE ]
cp ../extras/$EMDATA $DATAFILE
Simple add a “#” character at the beginning of each of these lines to comment it out. Now every time you run the emulator, you will use the same disk that is located in your home directory.
Make sure “adb” is in your path
In a terminal session run the command:
This will put the “adb” tool (and other tools) in your command search path. You can now start the emulator (just type “run-emulator” as it is now in your search path). You can then use the ant build target “installplay” to install your own companion into the virtual disk of the emulator (aka “ant installplay”).
Now every time you run the emulator, your own copy of the Companion will be used. Update it as you need to.
Under Linux, you will need to use the setup tools available for App Inventor 1 to get you started. You can download a .deb package from here.
The installer installs at /usr/google
and adb and run-emulator are at: /usr/google/appinventor/commands-for-Appinventor/
Update your PATH as:
Now you can run aiDaemon with:
Follow the same instructions as in Mac to load the Companion app to your emulator, and to stop the ‘canonical’ image to be loaded every time.
If you have a phone or tablet running Android 4.2.2 or newer, there is a trick you can do that will permit you to use USB connectivity without actually having to use the USB Cable. This is particularly useful when working with a tablet that cannot charge directly from a computer’s USB port. You can leave your tablet plugged into its AC charger and still use it as if you had a USB Cable. AI2 now supports the USB Cable.
First you plug your device into the USB via the cable (for real!). You then issue the “adb” command “adb tcpip 5555”. This tells the adb daemon on your device to listen to the network on port 5555. You can now unplug the USB Cable. Then from a command line prompt on your PC you do:
adb connect <ip-address-of-device>
You can learn the IP address of your device simply by starting the AI2 Companion. It will tell you the address on the startup screen. You can now use all adb commands as if your device was physically connected with a USB Cable. This includes the “ant installplay” target to install a new version of the Companion on your device.