Tuesday, 10 July 2012

How to best organize your Android source?


One thing that can certainly speed up your development tasks when doing Android projects, is to reuse your Android source code. To do that, it is important to make sure that your Android source code is well organized and easy to find and reuse when you need it. Many of us are forgetting this, when things go fast, and we have to make deadlines. But if you take a little time to find some routines and methods that suits you, it can actually help you reach your deadlines in a lot more comfortable way. If you have ever found yourself writing code, feeling some kind of deja-vu, this post is for you.
There is a few different resources that will be good to keep handy, and to reuse. This includes Java classes, resources (graphic, sound and other assets) and sometimes layouts. The best way to do this, is to use library projects. You could also compile your code into static jars, which is great if you need to share your code with others, without revealing your source.
A library project can contain all of these resources – Java source, assets and XML.

Java source

This is probably the primary thing that you want to reuse and/or share. This would often be your own extension to some classes or libraries. Maybe you wrote your oven ArrayAdapter extension, that you would love to be able to easily reuse and extend further in other projects. Or you made some heavy maths, that you would not want to spend 3 nights writing again.

Resources

Resources like graphics and sounds is often very specific to an application. However, there might be stuff that you would like to be able to easily reuse in other projects. This could be some bullets, frames, background images, list item assets or others. Things that are not necessarily very specific to a single application. If you develop games, it could be some bells, boings or whistles.

XML

This would primarily be layouts, and often be related to entire components, like a specific view that you made, or a widget. It could also be menus, strings and so on.

Organizing your Android source library projects

There is probably as many ways to split up your code into projects, as there is developers around. My approach is to split it up as much as it makes sense to do. That means, do not mix multiple components in one project, but if you make a component, put all of your code in a single project.
1. Specific components. This could be your own TabHost implementation. Or a custom Dialog. If you make such components, the best way to stay organized is to create one library project for each of your components. With that exception, that you might have some very small UI extensions or components, that it does not make sense to put in a project of its own. So, make a UIToolsLibrary with your small comonly used UI tools, and then go for a full library when creating larger components. The way you mix this depends on how you feel about it.
2. Tools. Examples are simple classes or extensions. Maybe an extension to Activity, or other classes. It could also be your own utility methods, like specific math functions, or calculations. You might also want to create multiple libraries for this, for instance your own math library, or your own graphics library. Again, how you organize this is much up to your own feelings. But be ware, that as your code base grows, you will be happy to have started out with an approach that splits up things a lot. A good approach is to create a core tools library, that contains small utility classes related to Android, and then create a library for each of the areas that you are working with.
3. Resources. As mentioned above, resources like graphic and sound are often not reusable. However, if you create a lot of applications, you might want to have some resources handy. The Android SDK has some resources for menus, icons and tabbars. Likewise, you might want to create your own.
4. Layouts. This will often be related to a specific component, and as such rarely be a good idea to create a specific project for these. However, if you do a lot of code, it could be wise. If you have your own ListView items, or custom Dialog boxes, it could make sense. I would, however, put these in the resource project instead.

Creating a library project

Using Eclipse and ADT, this is how you do it:
1. Select File->New->Project
2. Select Android -> Android Project, and click next
New Library project
Type in a project name, application name and package name. Application name would probably be the same as your project name. Select Create Activity and type in an activity name if you wish to create an activity. For this example, we do not. Click Finish.
3. Right-click your project in the Project Explorer, and click Properties. Select Android.
Android Project Properties
The only thing that you has to do here, is select “Is Library”. Then click OK.
Now you can start adding code to your project. Let’s create a small class.
4. Create a class – let’s call it TestClass, and extend Object. In here, create a simple static method:
public class TestClass {
    public static String combine(String a, String b) {
        return a + b;
    }
}
Now save this, and build your library. Our small class is now ready for use in our applications.

Using your libraries

After creating a library, you can start using it.
1. Right-click your application project in the Project Explorer, and select Properties. Select Android.
2. In the Library section, click Add, and your library project (if it is still open) should appear in a dialog. Select it, and click OK.
Select Library Project
3. In your application code, you can now use the TestClass class. Try it out:
String test = TestClass.combine("Hello"" World");
Log.i("Test", test);
The same way, when you include resources and layouts in your library, you can reference it from the application where you include the library.

No comments:

Post a Comment