November 20, 2009
Let’s assume you already have an Android project on your Mac.
Create the XCode Project
- start XCode
- select File > New Project…
- select External Build System
- go to the parent directory of your Android Project
- in the Save As: field, enter the directory name of your Android Project
select the scarily-misnamed Replace option [not in XCode 4 -- thanks Jusin]
Add Files
In your new XCode project:
- select first item in the left hand column, which is the name of your project
- right-click, select Add > Existing Files…
- select add files (don’t select the Copy option)
- organize as desired (I like to do a lot of grouping). You should be probably adding at least your Java files and your Layout resources.
Configure your Build Target
In your new XCode project:
- look for Targets
- inside will be a target for your project’s name
- double click on it
- change Build Tool to
ant
- change Arguments to
install
Clicking ⌘B should now compile your project.
Note: if you figure out how to have a Build vs. Build & Install (e.g. ⌘ENTER) please let me know!.
Getting XCode to Recognize Java errors
- Reconfigure the Build Target, changing
ant to ./xant
- Make a file
xant in the project’s home directory, using the code below
- do (from a Terminal)
chmod a+x xant
#!/usr/bin/env python
import sys
import re
import subprocess
av = list(sys.argv)
av[0] = "ant"
p = subprocess.Popen(av, stdout = subprocess.PIPE)
javac_rex = re.compile(" +[[]javac[]] +")
line_rex = re.compile("[.]java:[\d]+:")
pending = ""
while True:
d = p.stdout.read(128)
if not d:
break
d = pending + d
nx = d.rfind('\n')
if nx == -1:
pending = d
continue
else:
d, pending = d[:nx + 1], d[nx + 1:]
d = javac_rex.sub("", d)
d = line_rex.sub(r"\g error: ", d)
sys.stdout.write(d)
sys.stdout.flush()
sys.stdout.write(pending)
p.wait()
sys.exit(p.returncode)
Note: this code has been updated from the original post. It now reads little chunks and outputs them immediately rather than post-processing the ant output.
November 13, 2009
Yesterday, my experiments came with Android programming came to a crashing halt when I tried to use the Google Maps libraries. No matter what I tried – adding every possible Google Maps SDK, manually adding the jar to the project’s libs directory, a few more things too foolish to talk about – I would get one of these errors:
[javac] .../LocationMapActivity.java:21: package com.google.android.maps does not exist
[javac] import com.google.android.maps.*;
[javac] ^
[javac] .../LocationMapActivity.java:24: cannot find symbol
[javac] symbol: class MapActivity
[javac] extends MapActivity
or
W/dalvikvm( 247): Unable to resolve superclass of Lcom/.../LocationMapActivity; (89)
W/dalvikvm( 247): Link of class 'Lcom/.../LocationMapActivity;' failed
D/AndroidRuntime( 247): Shutting down VM
W/dalvikvm( 247): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
or
Failure [INSTALL_FAILED_MISSING_SHARED_LIBRARY]
Well. Here’s what you need to know:
- the emulator you’re running must match the target you set up your project for
- the emulator actually (as I understand it) actually has a lot of code built into it, so it’s not enough to link against the jar file
- if you don’t have the right target specified,
ant won’t look for jar files and just give you the linking errors
- the Google Maps SDK is tied into Android’s concept of a target
In particular, this is what you want to do to fix your problem – it’s all about “targets”:
- run
android list targets to find a suitable Google Maps-enabled target
- make an avd (“Android Virtual Device”) that has that target. This is really easy to do in the UI provided by running
android &. Don’t be fooled by seeing just “Platform” and “API Level” listed there – the target determines what these are
- run the appropriate emulator to use the avd that you created, that has that target, that includes the Google Maps SDK: e.g.
emulator -avd david_6 &
- update your project to reflect the new target:
android update project --path HelloAndroid --target 7
If you’re an Eclipse user, I’m sure there’s some easy way to do this, but the command line works just fine for me. I’m not the first person to see this problem, but I’ve never really seen this spelled out so explicitly how to solve it so hopefully you fine this of use.
November 8, 2009
I was interested in trying the Android emulators yesterday on my Mac, so here’s what I did. It’s much simpler than the documentation makes it out to be:
- download and unpack the SDK zip file from http://developer.android.com/sdk/index.html
- add the SDK tools directory to your path (in
~/.bashrc):
export PATH=${PATH}:${HOME}/…/android-sdk-mac/tools
- start Terminal
- run the “Android SDK and AVD Manager”:
android &
- select Settings on the left
- select Force https://… sources to be fetched using http://…
- select Available Packages on the left
- select the checkboxes for the various SDK Platforms
- press Install Selected
- make an “Android Virtual Device”:
android create avd --target 6 --name david -c 200M
- run the emulator (note: it can take minutes to boot):
emulator -avd david