How Do I View the Android Sources in my IDE?
I get asked this quite often. So often, in fact, that I figured I'd spend a few moments walking you through the process we teach to load the source code for the Android Open Source Project (AOSP) into your favorite Java IDE.
Before you can import AOSP into your IDE, you need the appropriate project files that provide the IDE with the proper classpath structure to understand the symbols and structure of the source tree. It turns out, this structure is slightly variable depending on which target you have built, so it is not included as a set of static files--it must be generated.
AOSP includes a helpful package to do the job called IDEGen, which contains a series of scripts that will traverse your built sources and generate the project files. Yes, I said built sources…you have to build the target image first! There are several generated classes (AIDL stubs, resource files, etc.) that are needed to satisfly many of the cross-references.
The project has a handy README that explains all the details, but here it is spelled out:
$ source build/envsetup.sh
$ lunch aosp_x86-eng #(or pick your favorite lunch target)
$ mmm development/tools/idegen/
The first three steps just ensure that you have a built image ready…
The next step is to build the IDEGen module. This is the process that will fail if you don't have the proper intermediates in place first. Finally we run the generator script, which can take a few minutes to complete as it traverses the entire source tree.
What are you left with?
The root of your AOSP tree will now have three new files in it:
android.ipr(IntelliJ / Android Studio)
android.iml(IntelliJ / Android Studio)
NOTE: Consult the README if this is your first run. You'll need to bump the VM memory and do some additional setup for either IDE to import cleanly.
IntelliJ + Android Studio
The process for these two is the same, since they share a common base. Choose to open an existing project, and select the
android.ipr file in the AOSP root directory.
NOTE: On the first import, the IDEs will take a few minutes to fully index the source files.
Import the root directory of your AOSP tree into Eclipse as a Java project. The generated
.classpath file will ensure you have no build errors.
Can't I just import root sources directory?
I understand that building the tree takes a long time if you are doing it just for this purpose. You can import the raw files, but you want the IDE to understand the full AOSP tree structure. This is the only way to enable the features that you expect from your IDE--such as jumping between source definitions, cross-references, and searching symbols. Without a valid project file, you will be limited to some basic searching on file names. But hey, at least you've got syntax highlighting!
Hopefully this tip helps you be more productive with the AOSP sources.
Want to get deeper into Android and AOSP? Sign up for one of our upcoming Android Internals classes!