Android Development: Program directory structure detailed

Source: Internet
Author: User

Overview of the directory structure of the HelloWorld program

We can see in the folder, the directory of the HelloWorld program mainly includes: Src folder, gen folder, Android folder, assets, res folder, Androidmanifest.xml, Default.properties. Expand the HelloWorld project on the left side of Eclipse to see a directory structure like this:

The following sections describe the above levels of directory structure.

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:

Java code

    1. package helloworld.test;
    2. import android.app.Activity;
    3. import android.os.Bundle;
    4. publicclass HelloWorld extends Activity {
    5. /** called when the activity is first created. */
    6. @Override
    7. publicvoid OnCreate (Bundle Savedinstancestate) {
    8. super . OnCreate (savedinstancestate );
    9. Setcontentview (r.layout.main);
    10. }
    11. }

You know: We created a simple HelloWorld project, and the system generated a Helloworld.java file for us. He imported two classes of android.app.Activity and Android.os.bundle,helloworld classes that inherit from activity and override 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, Publicvoid onCreate (bundle savedinstancestate) {...} This notation is correct if you write Publicvoid 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 O Ncreate 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 Setcontentview (View) to 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): Initialize your activities (activity), such as the completion of some drawing. Most importantly, in this method you will typically call the Setcontentview (int) method with layout resources (Resource) to define your UI, and use the Findviewbyid (int) Retrieve widgets (widgets) that you need to interact with programmatically in your UI. Setcontentview specifies which file to specify the layout (main.xml), the interface can be displayed, and then we do the operation, our operation will be wrapped as an intention, and then this intention to the relevant activity to be processed.

(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 save the data).

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.

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:

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.

3.Android 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:

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

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

4.assets

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

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/html Code

  1. <? xmlversion xmlversion = "1.0" encoding = "Utf-8" ?>
  2. < linearlayoutxmlns:android linearlayoutxmlns:android = "Http://schemas.android.com/apk/res/android"
  3. android:orientation = "Vertical"
  4. Android:layout_width = "Fill_parent"
  5. Android:layout_height = "Fill_parent"
  6. >
  7. < TextView
  8. Android:layout_width = "Fill_parent"
  9. Android:layout_height = "Wrap_content"
  10. Android:text = "@string/hello"
  11. />
  12. </ 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

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/html Code

  1. <? xmlversion xmlversion = "1.0" encoding = "Utf-8" ?>
  2. < manifestxmlns:android manifestxmlns:android = "Http://schemas.android.com/apk/res/android"
  3. Package = "Helloworld.test"
  4. Android:versioncode = "1"
  5. Android:versionname = "1.0" >
  6. Applicationandroid:icon = "@drawable/icon" Android : Label = "@string/app_name" >
  7. < Activityandroid:name Activityandroid:name = ". HelloWorld "
  8. Android:label = "@string/app_name" >
  9. < Intent-filter >
  10. < Actionandroid:name Actionandroid:name = "Android.intent.action.MAIN" />
  11. < Categoryandroid:name Categoryandroid:name = "Android.intent.category.LAUNCHER" />
  12. </ Intent-filter >
  13. </ Activity >
  14. </ Application >
  15. </ Manifest >

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

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.
# do not modify the This file--YOUR changes would be erased!
#
# This file must being checked in Version Control systems.
#
# to customize properties used by the Ant build system use,
# "Build.properties", and override values to adapt the script to your
# Project Structure.
# indicates whether an APK should being generated for each DENSITY.
SPLIT.DENSITY=FALSE
# Project target.

When we do Android development, we will find that most of the directory structure of Android programs, that is, mainly including Src folder, Gen folder, Android folder, assets, res folder, Androidmanifest.xml, Default.properties and other directories, directory structure with the above-mentioned similar.

Reprint Address: http://mobile.51cto.com/aprogram-395268_1.htm

Related Article

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.