When I first looked at building for Android I followed the route that led me to install Eclipse and all the associated bits required. Having not done anything with Java for over 10 years it was a bit of a shock to the system and figuring out what went where and how it all hung together wasn’t easy. The version of Eclipse that apt-get install gave me was also not the current one and all the tutorials assumed I had the latest, so that probably didn’t help. After some head scratching and wandering of the web I found enough to get some simple apps working, but was left with more questions than I had before starting. Since those first attempts I’ve kept things updated but every time I look at Eclipse I find it getting in the way more than helping. having spent so long using the command line for building I’m likely institutionalised Of course had I thought about it a little more I’d have realised that it’s perfectly possible to use ant from the command line to build a project. D’oh.
What Do I Need?
After a quick search i found that there is a page on developer.android.com that details what is needed. the list isn’t even that daunting and all of it was already installed on my system. I have the latest SDK updates and adb is installed and works. I should be good to go!
Where to start?
My inspiration was when looking at AndroidDnssdDemo and it’s command line version. This seemed to give me a starting point for figuring out what files would be needed.
Running ant from the command line with no arguments results in a complaint about the lack of build.xml, so I thought I’d start there.
build.xml is basically just an xml file that imports a number of settings files, so I just added some pieces that make sense using the one from the git repository as a template.
Running ant again revealed it wasn’t happy about the lack of a project.properties file, which I added with a single line specifying the version of android to be targetted
Once more ant was run and this time it complained about the lack of the SDK directory location. This should go into the local.properties file, but that’s a generated file. Generating it seemed straightforward enough
android update project -p .
Of course this wasn’t going to work without the AndroidManifest.xml file (as it helpfully reminded me) so I created that and tried again. This time it completed and I had a local.properties with the sdk.dir setting defined. Running ant now gave me a list of options. Progress, but of course I needed something to build!
Actual Source Files
By default source files need to be in the src directory, but also under the usual java directory structure, which means that in your AndroidManifest.xml file if you use
You need to have a directory structure of
src / com / blah / foo
After creating the directory structure and adding a source file, running ant completed and I had a generated apk. Gulp.
Going through the above steps was more for my interest and education than as a useful way of doing things, so in order to generate the directory structure and all required files, you should use
android create project --target android-<sdk version> --name <project name>
--path . --activity <name of main acivity class>
--package <package name>
This will generate all required files and directory in one simple step
From start to finish, including writing this post as I went, it’s taken me 20 minutes to get to a point where I can start coding with files and settings I understand. This may not be a huge accomplishment, but I feel more comfortable with the environment and more in control than I did with Eclipse. As I said, this is far more a comment on my background and preferences than Eclipse but I’ve been pleasantly surprised at how straightforward it’s been.
This has been the easy part as now I need to start writing the actual code. So, Java, eh?