Applying the AOSP Coding Style in Android Studio

No matter if you are a one man operation, or a cog in a large team, coding with a consistent style makes life easier when it comes to maintenance, refactoring and reuse. Modern IDE’s are powerful and highly configurable, with Android Studio supporting configuration for Java, HTML, XML and your gradle files. The AOSP (Android Open Source Project) has a defined code style that does not differ too much from the Android Studio default, if you want to use the AOSP coding style in Android Studio take a look at the steps below.

Step 1: Download the style definitions

Grab the xml code styles definitions for the Intellij platform.  These can be downloaded from the AOSP source, or for easy access from a GitHub mirror.

Step 2: Add the definitions file to your Android codestyles folder

You need to move this file within your .AndroidStudio configuration folder on your machine.  The exact location will vary not only between operating systems, but also Android Studio versions.  These folders are valid for the latest Android Studio 1.0 and 1.1 versions.

On linux:

/home/username/.AndroidStudio/config/codestyles

On Windows:

C:\Users\JohnDoe\.AndroidStudio\config\codestyles

On MacOS:

~/Library/Preferences/.AndroidStudio/config/codestyles

Note that these folders are often hidden so be sure to enable displaying hidden files and folders.

Step 3: Apply the code style to your project

Restart Android Studio to allow the new code style to be found.

Open the Code Style settings in the IDE (File > Settings… > Code Style) and click the Manage… button.  A new entry AndroidStyle should be in the list of available code styles.  Select and click Copy to Project to apply it to the current project. Then apply the formatting by running the Reformat Code tool across your source.

You should also take note of some more rules that are enforced for AOSP contributions, even if you are not contributing to AOSP, they are rules that should be applied to any project.

Remember this is the style used in AOSP, there is nothing wrong with the default code style settings in Android Studio, what is important is that you ensure you and your team follow a consistent style.

Leave a comment

Filed under Android Studio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s