Tips and Tricks

From CS 160 Fall 2011

Jump to: navigation, search

Contents


Android Quickstart

For Android Development, you'll need in this order:

A recent (1.6) Java development system (JDK) - there is a problem with Eclipse and jdk 1.6_u21 (b6). If you have this one, download the latest (b7 or greater). If not you should be fine. On Macs there is a built-in JRE which should work fine.

Eclipse 3.6.2 or 3.7. There is a problem between the Android environment and earlier versions of Eclipse, so get one of the latest ones. On Windows, you may need to add the JDK bin directory to your path before Eclipse will run.

Download the Android SDK tools. Save to a high-level directory, and run "SDK setup" from the tools directory (on Macs, this is an app called "Android"). This setup routine will prompt you for which targets to install. Targets include different versions of Android, and third-party libraries like Google's. The tablet you will use later has Android 2.3 so you should definitely download that. You can install whatever other targets you think you might want (e.g. if you want to try things on your own phone or tablet).

If you have a 64-bit version of Windows (XP, Vista or 7), you should select the usb driver and install it. On most Macs, the builtin usb driver works fine.

Install the ADT plugin for Eclipse. Follow the instructions there to point the ADT plugin at the Android SDK. Create a virtual device based on the 2.3 SDK to use as an emulator.

Now try building one of the SDK examples. From the Eclipse Java perspective, click File→New→Project→Android→Android Project→Create Project from Existing Sample. You will need to check the 2.3 SDK box. If you're lucky, it may build right away. If not, check the build path in Project→Properties→Java Build Path→Order and Export. For some reason, it often starts with the Android SDK unchecked. Check it and try building again.

To run your code you need to define a run configuration. Select Run→Run Configurations→Android Application. Give your run configuration a name. Go to the target tab and select "manual" so you can choose between emulator and device when you have a tablet connected. When you run an error-free program, the emulator will start automatically. Its not smart about screen size however, and may be too large for some monitors. If you have this problem, start the emulator instead using "SDK setup" program from the main Android SDK directory before you run a program. It will give you a dialog to set the size or scale. Setting the height to 8" works well on 768-pixel high monitors.

Mac Specific

You don't have to install the JDK, because Apple manages its own updates. As long as you have the machine updated, you should have the latest version of Java. Also make sure you download the correct version of Eclipse depending on your machine (32bit vs 64bit). The rest of the steps should work, the way they are.

Windows Specific

Go to a command prompt (i.e. windows cmd or cygwin), cd to the Android SDK/tools directory, and type ./adb devices. The ADB server should start, and detect and list the device. If that works, try ./adb -d shell. You will now be "logged in" to a unix-like shell on the device. Congratulations! Go back to eclipse and try running your program again. It should prompt you for a machine (emulator or tablet) to run on.

Emulator Problems

If you're having problems with Eclipse losing it's connection to the emulator (which happens more often on certain 32-bit machines), this is one way to get the connection back. Restarting Eclipse doesn't always work. Restarting the Emulator is slow and it also doesn't always work.

First open the device view. Go to:

Window → Show View → Other... → Android → Devices

If the emulator is running, it should show up on the list with an "Online" status.

If the emulator says "Offline" or doesn't appear at all, but it is running in the background, this is how to fix it:

There's an arrow next to the "Screen Capture" icon on the devices panel that will show more options.

The bottom "Reset adb" option will attempt to reconnect the emulator to eclipse. It usually works.

Running on a Real Device with Windows

Not all current devices are supported by the Android/windows USB driver out of the box. The Vizio isnt there. If you try to install the Vizio tablet right away, you will get an error. To avoid this, edit the file

C:${ANDROID_SDK}\extras\google\usb_driver\android_winusb.inf

and find the line [Google.NTamd64] if you are using 64-bit windows. Then add these lines:

;Vizio Tablet
%SingleAdbInterface%        = USB_Install, USB\VID_0489&PID_E040&MI_01
%CompositeAdbInterface%     = USB_Install, USB\VID_0489&PID_E040

if you're not on 64-bit windows, find the appropriate [Google.*] section, and add them there.

Then turn on the device (don't plug in the USB cable yet), and from its apps screen go to

Settings → Applications → Development

and turn on "USB debugging". Plug in the device and the driver should install correctly.

In case something went wrong and you installed before making the edit above, unplug the device and go ahead and make the edit. Then plug in the device again, and go to Windows Device Manager. You should see the device with an exclamation point. Right click on it and select "update driver". Select "Browse my computer for driver software" and navigate to:

C:${ANDROID_SDK}\extras\google\usb_driver\android_winusb.inf

the driver should install correctly this time. Test by doing "adb devices" from a command prompt.

Linux Specific

(based on http://developer.android.com/sdk/index.html, with additions)

Step 1. Prepare your development computer

1a. Java

If you already have a satisfactory Java setup, feel free to skip this.

Ubuntu (and many other popular Linux distributions) come by default with OpenJDK. It's open source, which is nice, but it's quite frankly an awful runtime - things will run, but they'll run slowly, and when running a heavyweight application like Eclipse can overwhelm even a fairly high-end machine. Switch to Sun's Java implementation. In Ubuntu, the command is as follows:

user@host:~$ sudo apt-get install sun-java6-jdk sun-java6-jre

Then, make sure that your computer is actually using sun-java6 by running

user@host:~$ sudo update-alternatives --display java

Check that the directories used are called java-6-sun or something along those lines.

If you are running Ubuntu 10.10 Maverick development, the Sun java packages are not yet in the partner repositories. Temporarily change the partner repos to lucid, install java, then change back to maverick.

1b. Android SDK on 64-bit machines

The Android SDK for Linux only exists in a 32-bit version. In order to get the 32-bit SDK running on a 64-bit machine, you'll have to install 32-bit libraries. For example, in Ubuntu you can either look up ia32-libs in the Software Center or enter this command:

user@host:~$ sudo apt-get install ia32-libs
1c. Eclipse

Eclipse is in the Ubuntu repositories; just use the package eclipse-jdt (this includes the base eclipse package and the Java Development tools). You'll also need to install the eclipse-pde package, without which Eclipse can't install new plugins (good going there, Ubuntu Packaging Team). So in all, it goes like this::

user@host:~$ sudo apt-get install eclipse-jdt eclipse-pde

Also, before starting to work, make sure that Eclipse is configured to compile using the Sun Java 6 tools by default (it should be)

2. Download and install the SDK starter package

Direct link (linux only)

To install the SDK, simply unpack the starter package to a safe location.

Optionally, you may want to add the location of the SDK's primary tools directory to your system PATH. On Linux, edit your ~/.bash_profile or ~/.bashrc file. Look for a line that sets the PATH environment variable and add the full path to the tools/ directory to it. If you don't see a line setting the path, you can add one:

export PATH=${PATH}:<your_sdk_dir>/tools

3. Install the ADT Plugin for Eclipse

Instructions

4. Add Android platforms and other components to your SDK

Use the Android SDK and AVD Manager, included in the SDK starter package, to add one or more Android platforms (for example, Android 2.3) and other components to your SDK. If you aren't sure what to add, see Which components do I need?. To launch the Android SDK and AVD Manager on Linux, execute the android tool in the <sdk>/tools/ folder.

5. (Optional) Install a physical device

In order to allow ADB to access a device without being run as root, you'll need to get udev to loosen up the permissions on your device when it's connected. To do so, create a file "/etc/udev/rules.d/51-android.rules" and edit it to include the line:

SUBSYSTEM=="usb", ATTRS{idVendor}=="0e79", MODE="0666"

Add one line per vendor (Vizeo, Motorola, HTC, etc.) substituting the vendor's USB ID after ATTRS{idVendor}==. For a list of the most common vendor IDs go to [1].

Add universal read permissions to the file:

chmod a+r /etc/udev/rules.d/51-android.rules

Restart udev (or your whole computer) for the new rule to be read:

sudo service udev restart

Lastly, as with Macs, you need to add a line with your device's vendor ID to ~/.android/adb_usb.ini. In one line:

echo $VENDOR_ID >> ~/.android/adb_usb.ini # The '>>' operator appends the output of echo to the file instead of overwriting

Android Resources

  • DroidDraw a new graphical GUI editor for Android apps (currently in beta).

Useful Apps from Appslib

GPS Test - does what the name suggests. Gives useful info about the number of visible satellites and signal strengths, your current position, the uncertainty in that position, and the position on a world map.

System Monitor - tells you what apps are currently resident in memory and their run status. Allows you to remove dormant apps from memory. This doesnt really save resources because Android will remove them as needed for new apps, but it may help with clean uninstalls, especially for native shared libraries.

Wiimote Controller - replaces the current input device (i.e. the touchscreen) with the Wii remote so it can be used with existing games. Make sure to undo this change before disconnecting the wiimote.

Personal tools