Yesterday I talked about getting started with Android dev. The result of that exercise was to get a working demo of the various APIs at your disposal. With the source to each demo handy you can easily go and plug the bits together to realise your end goal. The next big thing, right?
As a standalone app though the ApiDemo is useless. Not only because it doesn't do anything, but mainly because it's not built like a real app and so you can't learn from it how you'd build a proper app.
When I'm getting to grips with a new platform the first thing I want to look at is a real-world application. Something built be experts using patterns and methodologies accepted as the best.
Luckily for us Android is open source.
You can browse the entire source of the latest stock Android build here. If you Look towards the bottom of that page you'll see packages in the folder "platform/packages/apps/". Notice there are apps called Email, Calendar, Camera and many others.
One of my pet projects is an app which is built around taking photos. Obviously it needs to use the devices camera. While there's a "camera output" demo in the ApiDemos app it's by no means a working camera application. Far, far from it.
I'd started trying to write my own camera app from scratch, only to realise I was re-inventing a massive wheel. A massive and complicated wheel. Then I twigged - "Hang on a minute!", I thought, "if Android is open source and it has a camera app built in surely I can use it as a starting point?!". Turns out I could. Here's how.
First thing I did was download the stock Android camera app's GIT package. To do this I used my Mac (you can do it on Windows with 3rd party software but OSX can do it already).
At the Terminal I typed in the following command:
git clone git://android.git.kernel.org/platform/packages/apps/Camera.git
It then downloaded the app in to a folder called "Camera" on my Mac:
Next step - I moved the Camera folder over to my Windows machine where I've got Eclipse and do most of my dev work. I placed it inside the "workspace" folder for Eclipse (normally in the C:\Users\YourName\ folder).
Next I launched Eclipse and chose File -> Import... and then chose "Existing Projects in to Workspace".
After browsing to the Camera folder I clicked Finish and Eclipse imported the project. Et voila. I now had a working Camera app (I can run it in the emulator and on my phone) which I could rip apart at my leisure. In doing so I'd learn how the pros did it.
Standing on the shoulders of giants? Who? Me?
When you install an app from the Android source the code will be in packages with names beginning "com.android.*". You're going to want to change this if the same app/package is already on the phone if you, ultimately, want to ever release the app to the market.
To rename the packages you need to refactor the code. Right-click the packages in the Eclipse and choose "Refactor - Rename", like so:
Give the packages your own prefix. In my case I changed com.android.* to com.rockalldesign.*. Eclipse does a good job of changing all references of one to the other, but it leaves a few in place - particularly in the XML files. You'll need to go and address each error one-by-one until it builds ok and you can launch/install it.
There might be a couple of other gotchas and things you need to do to get it to build. I forget now. This isn't for the feint-hearted. Trust me though. I've done this and it works. I have my own prototype app on my phone now, which is based around the stock camera app!
Depending on what your goal is you might want to choose another of the stock apps as you starting point.
Is It Legal?
Is it ok to take Android source and make you own app out of it? I don't know. My understanding of what "open source" means is probably not exactly what it does mean. Anybody?