Directory structure for Android projects

Source: Internet
Author: User

Introduction

This article will introduce the directory structure of the Android project through the HelloWorld project. The main topics of this article are as follows:

    • 1. Directory Structure of HelloWorld project
      • 1.1. src Folder
      • 1.2. Gen Folder
      • 1.3. Android 2.1 Folder
      • 1.4, assets
      • 1.5. Res folder
      • 1.6, Androidmanifest.xml
      • 1.7, Default.properties
1. Directory Structure of HelloWorld project

(This HelloWorld project is based on Android 2.1) to expand the HelloWorld project on the left side of Eclipse, you can see the directory structure as follows:

Figure 1, HelloWorld project directory structure

The following sections describe the above levels of directory structure.

1.1. src folder

As the name implies (SRC, source code) The folder is put into the source code of the project. Opening the Helloworld.java file will see the following code:

Package Helloworld.test;import Android.app.activity;import Android.os.bundle;public class HelloWorld extends Activity  {    /** called when the activity is first created. *    /@Override public    void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);    }}

You know: We created a simple HelloWorld project, and the system generated a Helloworld.java file for us. He imported two classes android.app.Activity and Android.os.Bundle, and the HelloWorld class inherits from Activity and overrides the onCreate method.

The following instructions are for people who have not learned Java or have a weak Java base

@Override

When overriding the oncreate of a parent class, adding a @override system in front of the method can help you check the correctness of the method. For example, public void onCreate (bundle savedinstancestate) {...} This is the correct wording if you write the public void OnCreate (bundle savedinstancestate) {...} so the compiler returns the following error--the method OnCreate (Bundle) of type HelloWorld must override or implement a Supertype method to ensure that you correctly rewrite Onc Reate method. (because oncreate should be oncreate)

If you do not add @override, the compiler will not detect errors, but will assume that you have defined a new method OnCreate.

android.app.Activity class: Because almost all activities (activities) interact with the user, the Activity class focuses on creating windows, and you can use the method to setContentView(View) put your own UI inside. However, activities are usually presented to the user in full-screen mode, or in a floating window or embedded in another activity. There are two methods that are implemented by almost all Activity subclasses:

    1. onCreate(Bundle):初始化你的活动(Activity),比如完成一些图形的绘制。最重要的是,在这个方法里你通常将用布局资源(layout resource)调用setContentView(int)方法定义你的UI,和用findViewById(int)在你的UI中检索你需要编程地交互的小部件(widgets)。setContentView指定由哪个文件指定布局(main.xml),可以将这个界面显示出来,然后我们进行相关操作,我们的操作会被包装成为一个意图,然后这个意图对应有相关的activity进行处理。
    2. onPause(): Deal with what you have to do when you leave your activity. Most importantly, all changes made by the user should be committed here (usually ContentProvider saving the data).

More details about Activity class This series will be introduced later, if you want to learn more please refer to the relevant documentation.

Android.os.Bundle class: Maps various types of packaging (parcelable) from string values (bundle words are the meaning of bundles, all of which are well understood and remembered). If the class provides a public method--public Boolean Containkey (String key), returns False if the given key is contained in the bundle's mapping to return true. This class implements the Parceable and cloneable interfaces, so it has the characteristics of both.

1.2. Gen Folder

There is a R.java file under the folder, R.java is generated automatically when the project is built, the file is read-only and cannot be changed. The R.java file defines a class--r,r class that contains many static classes, and the name of the static class corresponds to a name in Res, which is the R class that defines the index of all resources for the project. See our HelloWorld project is not so, such as:

Figure 2, R.java corresponding res

Through R.java we can quickly find the resources we need, in addition Yi will also check the R.java list of resources are used, the unused resources will not be compiled Yi into the software, this can reduce the use of mobile phone space occupied.

1.3. Android 2.1 Folder

The folder contains the Android.jar file, a Java archive that contains all of the Android SDK libraries (such as views, Controls) and APIs required to build the application. Bind your application to the Android SDK and Android Emulator via Android.jar, which allows you to use all of your Android libraries and packages and enable your application to debug in the right environment. For example, in the Helloworld.java source file above:

Import android.app.Activity;
Import Android.os.Bundle;

Here are two lines of code that import packages from Android.jar.

1.4, Assets

Contains files such as MP3, video classes that the application needs to use.

1.5. Res folder

The resource directory that contains the resource files in your project and will be compiled into the application. When you add a resource to this directory, it is automatically recorded by R.java. Create a new project, the Res directory will have three subdirectories: Drawabel, layout, values.

    • DRAWABEL-?DPI: Contains some icon files that your application can use (*.png, *.jpg)
    • Layout: The interface layout file (main.xml) is similar to the HTML in the Web application, and the Main.xml file that has not been modified is as follows (HelloWorld has not been modified):
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    ><textview      android:layout_width=" fill_parent "     android:layout_height=" Wrap_ Content "     android:text=" @string/hello "    /></linearlayout>

    • Values: All the text that needs to be displayed on the software. You can store multiple *.xml files, and you can store different types of data. such as Arrays.xml, Colors.xml, Dimens.xml, Styles.xml
1.6, Androidmanifest.xml

The total configuration file for the project, documenting the various components used in the application. This file lists the features that the application provides, in which you can specify the services that the application uses (such as telephone service, Internet service, SMS service, GPS service, and so on). In addition, when you add a new activity, you need to configure it in this file, only after the configuration, you can call this activity. Androidmanifest.xml will contain the following settings: Application permissions, activities, intent filters, and so on.

If you were born or learned like me, you would find that Androidmanifest.xml is similar to the Web. config file and can be interpreted in the same way as the Web. config file.

If you are not, you can understand--well known as XML is a data interchange format, Androidmanifest.xml is used to store some data, but this data when the configuration data about the Android project.

The androidmanifest.xml of the HelloWorld project are as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "      package=" helloworld.test "      android:versioncode=" 1 "      android:versionname=" 1.0 ">    < Application android:icon= "@drawable/icon" android:label= "@string/app_name" >        <activity android:name= ". HelloWorld "                  android:label=" @string/app_name ">            <intent-filter>                <action android:name=" Android.intent.action.MAIN "/>                <category android:name=" Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>    

As far as Androidmanifest.xml is now, the articles later in this series will be described in detail separately.1.7, Default.properties

Document the environmental information required in the project, such as the Android version. HelloWorld's Default.properties file code is shown below, and the comments in the code have explained Default.properties clearly:

# This file was automatically generated by Android tools.# does not modify this file--YOUR changes would be erased!# # this File must is checked in Version Control systems.# # To customize properties used by the Ant build system use,# "Build.prop  Erties ", and override values to adapt the script to your# project structure.# indicates whether a apk should be generated For each density.split.density=false# Project target.target=android-7

Directory structure for Android projects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.