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
- change Build Tool to
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
antto./xant - Make a file
xantin 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.
RE: if you figure out how to have a Build vs. Build & Install (e.g. ⌘ENTER) please let me know!.
If you edit your python script add these lines after:
av = list(sys.argv)
av[0] = “/usr/bin/ant”
if len(av) == 1 :
av.append(“compile”)
then and leave Xcode’s build argument set to $ACTION, then you’ll find that get Xcode’s builds, installs, and clean connected through to ant.
Oh god, awesome, thanks.
After a further look, I’ve been using:
#!/bin/bash
if [ "x${ACTION}" == "xclean" ] ; then
echo CLEAN
# remove the generated files in bin (Assumes all files in bin are discardable.)
cp /dev/null bin/a
rm -rf bin/*
else
echo /usr/bin/ant -emacs ${ACTION} $(echo ${BUILD_STYLE} | tr ‘[A-Z]‘ ‘[a-z]‘)
/usr/bin/ant -emacs ${ACTION} $(echo ${BUILD_STYLE} | tr ‘[A-Z]‘ ‘[a-z]‘)
fi
which handles Build, and Clean, for both the Debug and Release targets, but doesn’t do the install, and doesn’t have your Python error editing so Xcode will see it. You script does the install, which is more useful.
Rather than following Apple’s directions and using an “External Build System” style target, an empty target with a series of custom build phases might work better. You could have a phase like this;
#!/bin/bash
if [ "false" == $(osascript -e 'tell application "Finder" to exists process "emulator"') ] ; then
echo starting Emulator
../../android-sdk-mac/tools/emulator -avd ${MYAVD}
else
/usr/bin/ant install bin/${TARGET}
fi
to start the emulator, or if it was started, install the .apk on it. But that still doesn’t solve the problem of connecting Xcode’s Java debugger via TCP/IP to the environment in the emulator. Sorry I don’t have all the answers.
But it does look like the -emacs command line option to ant makes the errors from ant immediately compatible with Xcode without any need to post process the error messages with Python.
I’ve never worked with Android, but it might also be worth looking at Xcode’s Organizer feature. The Organizer lets you work with Ant/Java projects without cluttering them with Xcode project files. Apple’s doco on using the Organizer with Java is sparse, but these links are a start:
http://developer.apple.com/mac/library/documentation/Java/Conceptual/Java14Development/02-JavaDevTools/JavaDevTools.html
http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/XcodeProjectManagement/140-Using_the_Organizer/using_the_organizer.html
Thank you Robert – I’ll check this out, as I’m just in the process of picking up this project again. Timely!
[...] Between the two platforms, Xcode wins hands down as the more friendly dev environment for me. Others have even gone as far as configuring Xcode for Android development. [...]
Unless I missunderstood, can we export an iphone app to blackburry with that method ?
[...] How to use XCode for Android Projects ‹ Previous Post UITextField/UITextViewの文字入力で覚えておくと便利なこと [...]
unfortunately, the “scarily misnamed Replace option” seems to be scarily correct in Xcode 4. I just used it and nuked a folder full of code. I had a copy, though, so no harm done here. you might want to alter your article.
Thus updated! Thanks.
@David, I’m having trouble with the last part of the xant file creation in terminal.
I’m using chmod a+x xant but I get an error saying: “No such file or directory.”
I’m in my project directory. Is there anything I’m doing wrong?
What does “ls -l” and “pwd” show?
hey everyone, i was hoping you could lend me a hand…
i would like to create an ebook, with some swf animation, compatible with android.
is Xcode compatible with android?
and what open source (or not) program would you recommend for such a progect (given also my limited programing skills).
thank you in advance for your help.
Diane
/Users/myuser/Applications/Eclipse/CIT252 — pwd
drwxr-xr-x 14 myuser staff 476 Oct 31 21:25 CP Android
Updated for XCode 4 and cleaned up a bit, see https://gist.github.com/1340255 — thanks for the great starting point, David!
Bill: you’re not in the directory you created xant in.