Android apps are built on the application framework, so Android programming is the application-oriented Framework API programming---not much different from writing plain Java SE, just adding some APIs.
1. Develop your first Android app with Eclipse
Develop Android app three steps: A, create an Android project. B. Define the application's user interface in the XML layout file. C. Write the business implementation in Java code.
Develop a simple HelloWorld application
1, eclipse->file->new->android Application project.
2. Enter the application name, project name, package name, choose the minimum version of the program to run, the highest version, and what version to compile the application, next. Note: The package name is important, and Android apps can be used as a unique identifier for Android apps.
3. If the Create custom launcher icon is checked in the next window, a Setup window will pop up on the boot icon below, next.
4, if the Create activity is not checked, then finish, if you tick the next, the Activity window to create.
5. Set activity type, next, then finish.
6, in the Res->layout directory of the project has a file activity_main.xml, used to define the Android app user interface
In the interface graphical layout corresponding code file, namely the Activity_main.xml file, whose root element is Relativelayout, represents a relative layout.
In the Android user interface design, there are various interface layout elements, whose common properties are:
Android:id-This property specifies the unique identity of the control, which can be obtained by Findviewbyid ("id") in a Java program to obtain the specified Android interface component.
android:layout_width-Specifies the width of the interface component. If the property is Match_parent, it has the same width as its parent container. property is Wrap_content, its width depends on its own content-it can wrap its contents.
Android:layout_height: Specifies the height of the interface component. Its property value is similar to the width.
Android uses XML documents that are specifically responsible for user UI settings, while Java programs specialize in business implementations, which can reduce the coupling of the program.
You can approximate the Activity_main.xml file as an HTML page: they all define the user interface through markup language, except that the HTML interface uses HTML tags, while the Activity_main.xml file uses Android tags.
7, the SRC Directory of Android project is the source code of Android project, have Java file.
The Java file: A, set this activity to use the interface layout defined by the Activity_main.xml file as the user interface. Setcontentview (R.layout.activity_main);
Running Android apps with ADT
1), use the AVD Manager provided by Android or run the specified AVD device directly using the emulator command.
2), select the Android project you want to run, and run as->android application.
Android Application Structure analysis
A. Create an android project; b. Use the Main.xml file to define the user interface; c. Open the Java source code to write the business implementation.
Use the command to create an Android app
Android Create Project-n helloworld-t 8-p helloworld-k org.crazyit.helloworld-a Helloworld
The-n option specifies the name of the project to be created, the-t option specifies the project's target for the Android platform, and the-p option specifies the save path for the item; the-K option specifies the package name for the project;-a option specifies the activity's name.
The created project must contain RES directories, SRC directories, and androidmanifest.xml files that are required for Android projects.
Other directories and files are optional.
Res directory holds various resource files of Android project, such as layout file, and values store various XML format source files, such as String resource file: Strings.xml, color resource file: colors.xml; Size resource file: Dimens.xml. DRAWABLE-LDPI, drawable-mdpi, drawable-hdpi, drawable-xhdpi Four subdirectories are used for low-resolution, medium-resolution, high-resolution, ultra-high-resolution 4 image files.
SRC directory is just a normal directory where Java source files are saved
The Androidmanifest.xml file is a system manifest file for Android projects that controls overall properties such as the name, icon, and access rights of Android apps, and in addition to the activity, service, The 4 major components of ContentProvider and broadcastreceive need to be configured in this file.
There is also a build.xml file, which is an ant build file provided by Android for the project. With this file, developers can build and install Android projects through Ant.
At this point, the build environment built with adt-bundle-windows-x86_64-20140702, without the ant command, download the plugin and copy it into the plugins inside eclipse.
After rebooting, the ant is installed
, but there is no build.xml when building the project, the following settings build.xml
Configuring the ANT Environment
Download ant:http://ant.apache.org/bindownload.cgi
On Windows, you should select the ZIP package and unzip the zip archive into a directory.
Open the System environment variable, click New on the System variable column, enter the variable name "Ant_home", the variable value is ANT's root directory, such as "D:\Android\apache-ant-1.9.0", take care not to enclose double quotation marks.
Locate the path variable in the system variable, click Edit, add "%ant_home%\bin" at the end of the variable value, take care not to enclose the double quotation mark, and use ";" Separated from the previous variable values.
Open a CMD window, enter "Ant", if the message shows that the environment configuration of ant is successful, if: ' Ant ' is not an internal or external command, it is not a running program or a batch file. Check to see if there is a problem with the path.
Configuring the Ant in Eclipse
Before using ant in Eclipse, for the ant build.xml file to be able to install the formatted format for indentation and highlighting, and to be able to code hints, first of all, a simple set up.
Open Windows-preferences, expand General,editors, select File Associations, click Add ..., enter Build.xml in the Add File Type dialog box, click OK.
Next in the File type: column Check Build.xml, in the associated editor: bar check ant editor, click the Default,build.xml icon to become a small ant, configuration completed. Http://www.cnblogs.com/sw926/p/3238512.html
Eclipse automatically generates ant's Build.xml configuration file, and the resulting method is very discreet.
Select the project you want to generate the Build.xml file in, right-click. Export-> General, Ant Buildfiles.
Click Next, and then click Finish.
The build is complete.
I hope you can try it. Finally no more silly to write their own build.xml.
Clipse using ANT compilation hint class not found:javac1.8
Ran the Batch_build.xml Class not found:javac1.8 error, and then went online to check that the ant version is too low, less than 1.9, and java1.8 compatible, because my Java operating environment is 1.8, the ant version is also is 1.8, so you only have to upgrade the ant version. The upgrade ant version method is:
1. Enter http://ant.apache.org/manualdownload.cgi to download the new version of Ant and unzip it after downloading.
2. Go to the plug-in path under your eclipse to find the ant plugin, as mine is d:/android/eclipse/plugins/org.apache.ant_1.8.4.v201303080030, open the path, Replace the files extracted with the new ant version into this directory, but keep the Meta-inf directory and the Plugin.properties file.
3. You can see the version number of the ant by running ant-version after you enter the location of the plugin via CMD, and the new version number will be displayed if the update succeeds.
4. Restart Eclipse and then pack with ant to be compatible with jdk1.8.
Auto-generated R.java files
It is automatically generated by the AAPT tool based on the resource file in the application and can be understood as a resource dictionary in Android apps.
Each class of resources corresponds to an inner class of the R class. For example, all interface layout resources correspond to layout inner classes; All string resources correspond to string inner classes; All identifier resources correspond to ID inner classes.
Each specific resource item corresponds to a public static final int Type field of the inner class.
Res Directory Description
This directory contains all the resources used by Android apps, aapt tools to scan these resources, and generate corresponding resource list classes for them: R.java.
Android projects allow the resources in this resource file to be used in Java code, XML code, respectively.
1. Using Resources in Java code
Java code can refer to the "HelloWorld" string constant through R.string.app_name.
2. Using Resources in XML files
Format:@< the name of the class name >/< resource item for the internal class that corresponds to the resource >
To access the above app_name, use @string/app_name;
For identifiers: Their format:@+id/< designator code >
For example, use the following code to assign an identifier to a component: Android:id= "@+id/ok",
The next reference in the file
If the component is obtained in Java, it is implemented by invoking the Findviewbyid () method of the activity.
Reference format in XML: @id/< identifier code >
Androidmanifest.xml manifest file
The information is as follows:
The package name of the application, which is the unique identity of the app.
The components that the application contains, such as activity, service, broadcastreceive, ContentProvider, and so on.
The minimum version that is compatible with the application.
Permission declarations required by the application to use the system
The permission declarations required by other programs to access the program.
Regardless of the eclipse's ADT tool Korean Andriod.bat command, the Android project they created has a androidmanifest.xml file. With development, the document will make some changes.
Two of the above resources are used:
Android:label= "@string/app_name" indicates that the label for the app uses the App_name string value in/res/value/strings.xml.
android:icon= "@drawable/ic_launcher" indicates that the app uses a picture with the icon as the main file in the/res/drawable-l/m/hdpi directory.
Application Permission Description
An Android app may require permissions to invoke the functionality of the Android system, and an Android app can be called by other apps. Therefore it also needs to declare the permissions that are required to invoke itself.
1. Declare the permissions required to run the app itself
You can declare permissions for the program itself by adding elements <uses-permission.../> child elements for <manifest.../>.
such as: <uses-permission android:name= "Android.permission.SEND_SMS" > Allow the app itself to send SMS permission.
Start your first Android app