Coding for Android typically means writing Java, for all the good and bad it entails. The language itself is known of its verbosity, but the Android itself does not really encourage conciseness either.
To illustrate, look at this trivial activity that simply displays its application name and version, complete with a button that allows to close the app:
Phew, that’s a lot of work! While the IDE will help you substantially in crafting this code, it won’t help that much when it comes to reading it. You can easily see how a lot of stuff here is repeated over and over, most notably fields holding
View objects. Should you need to change something about them or add a new one, you have to go through all these places.
Not to mention that it simply looks cluttered.
What to do about it, though?… As it turns out there is a way to structure your Android code in a more succinct and readable way. Like a few other approaches to modern Java, it employs a palette of annotations. There is namely a project called Android Annotations that offers few dozens of them and aims to speed up Android development, making the code easier and more maintainable.
And it’s pretty damn good at that, I must say. Rewriting the previous snippet to use those annotations results in a class which looks roughly like this:
Not only we have eliminated all the boilerplate, leaving only the actual logic, but also made the code more declarative and explicit. Some of the irrelevant entities has been completely removed, too, like the
btnExit field which was only used to bind an event listener. Overall, it’s much more elegant and understandable code.
How about a bigger example? There is one on the project’s official page which looks pretty impressive. I can also weigh in my own anecdotal evidence of going through the Android game I wrote long ago and molding its code (a few KLOC) to work with AA. The result has been rather impressive, partially thanks to very light dependency injection facilities that the project provides, allowing me to replace many occurrences of
Game.get().getGfx().draw(...); silliness with just
So in closing, I can recommend AA to all Android devs out there. It will certainly make your lives easier!
What’s your opinion about RoboGuice? It does similar work to Android Annotations, but I see that Android Annotations probably has more features. I am only curious whether Android Annotations would work properly with other libraries I use in some projects.
AA will work with RoboGuice through AA’s @RoboGuice annotation . Both frameworks are a bit tangential to each other but RoboGuice might be just a natural step up from AA, especially if you end up using @EBean and @Bean annotations extensively.
I haven’t used RoboGuice specifically, but I use the original Guice and I quite like the idea. As long as you can afford not to care that much about your objects lifetime (or are willing to spend some time defining custom scopes), it’s a nice framework at least for two things: (1) decoupling for the purpose of writing tests and (2) separating dev/staging/production configuration. (If you don’t care about either, the benefits of DI may not be apparent until the codebase grows large).
It has some learning curve though, as well as few dos and don’ts that you need to follow (like not putting extensive logic into modules) in order to be effective. But all in all, it’s a good trade off.
I have been using AA for a few months, and I wouldn’t recommend it. The problem is, if you change anything in an @Enchanced class, you have to rebuild the whole project (at least in IntelliJ). Otherwise very strange crashes occurs at runtime. Sometimes I think I spend more time rebuilding project, than I would have spent writing that boilerplate code.
For the next project I’ll go for RoboGuice. Or Scala ;)
For a project so large that build time becomes so severe an issue, a proper DI framework like Guice is probably necessary. That doesn’t preclude using AA in smaller ones, I suppose.
Plus, I haven’t heard about this strange crashes in Eclipse so it might be an IDE or Android plugin/toolchain issue.
I’ve read more about Android Annotations and I see one specific thing I don’t like.
Quote from project wiki:
You should always register your activity with an _ suffix in the Android Manifest.
This is because AndroidAnnotations generates a subclass for each annotated activity. It has the same package and name, plus an _ suffix.
This requirement creates some mess in the application.
The only change this mandates is in AndroidManifest.xml. I don’t see a big deal here.