In last tutorial, we learned about creating a sample android app and running it in a android virtual device (AVD). In this tutorial, I am giving short introduction of the project structure of an android app, the files and folders created in android app and their usage. This information will be discussed in more detail in later tutorials, but for now, a small introduction will set the stage for moving forward.
1) Project Root Content / Folders
When you create a new Android project (e.g., via android create project), you get several items in the project’s root directory, including:
- AndroidManifest.xml which is an XML file describing the application being built and what components — activities, services, etc. — are being supplied by that application
- bin/ folder which holds the application once it is compiled (note: this directory will be created when you first build your application. Usually, in eclipse “auto build” is enabled, so you will get it directly after project is created.)
- res/ folder which holds “resources”, such as icons, GUI layouts, and the likes, that will get packaged with the compiled application
- src/ folder which holds the Java source code for the application
- lib/ folder which holds extra jar files required at runtime, if any
- assets/ folder which holds other static files you wish packaged with the application for deployment onto the device
- gen/ folder holds source code that Android’s build tools generate
2) Java Source Code
When you created the project (e.g., via android create project), you supplied the fully-qualified class name of the “main” activity for the application (e.g.,
com.howtodoinjava.DemoApp). You will then find that your project’s src/ tree already has the package’s directory tree in place, plus a stub Activity subclass representing your main activity (e.g.,
src/com/howtodoinjava/DemoAppMainActivity.java). You are welcome to modify this file and add others to the src/ tree as needed to implement your application.
The first time you compile the project out in the project’s package’s directory, the Android build chain will create
R.java. This contains a number of constants tied to the various resources you placed out in the res/ directory tree.
R.javayourself, letting the Android tools handle it for you.
3) Android App Resources
You will also find that your project has a res/ directory tree. This holds “resources” — static files that are packaged along with your application, either in their original form or, occasionally, in a preprocessed form. Some of the sub-directories you will find or create under res/ include:
- res/drawable/ for images (PNG, JPEG, etc.)
- res/layout/ for XML-based UI layout specifications
- res/menu/ for XML-based menu specifications
- res/raw/ for general-purpose files (e.g,. an audio clip, a CSV file of account information)
- res/values/ for strings, dimensions, and similar things
- res/xml/ for other general-purpose XML files you wish to ship with your app
Some of the directory names may have suffixes, like res/drawable-hdpi/. This indicates that the directory of resources should only be used in certain circumstances — in this case, the drawable resources should only be used on devices with high-density screens.
In our initial project, you will find files like:
- res/drawable-hdpi/icon.png, res/drawable-ldpi/icon.png, and res/drawable-mdpi/icon.png, which are three renditions of a placeholder icon for your application for high-, low-, and medium-density screens, respectively
- res/layout/main.xml, which contains an XML file that describes the very simple layout of your user interface
- res/values/strings.xml, which contains externalized strings, notably the placeholder name of your application
4) What happen when we compile android project
When you compile your project (via ant or the IDE), the results go into the bin/ directory under your project root.
- bin/classes/ holds the compiled Java classes
- bin/classes.dex holds the executable created from those compiled Java classes
- bin/resources.ap_ holds your application’s resources, packaged as a ZIP file (where yourapp is the name of your application)
- bin/DemoApp.apk is the actual Android application
The .apk file is a ZIP archive containing the .dex file, the compiled edition of your resources, any un-compiled resources (such as what you put in res/raw/) and the
If you build a debug version of the application — you will have
DemoApp-debug-aligned.apk as two versions of your APK. The latter has been optimized with the zipalign utility to make it run faster.
That’s all for this small introduction post discussing various files and folders created with every android app by default. We will discuss them in deeper as and when required in future tutorials.
Happy Learning !!