Famous Android (1)-build an Android development environment and a Hello World Program in Windows

Source: Internet
Author: User
[Index page]
[Download source code]

Famous Android (1)-build an Android development environment and a Hello World Program in Windows

Author: webabcd

Introduction
Build an Android development environment and write a simple sample program

  • Build an Android development environment in Windows
  • Description of the directory structure of the Android Project
  • Write a simple Hello World Program

1. Build an Android development environment in Windows
1. Install JDK (Java Development Kit)
Http://download.java.net/jdk6/

2. Install the Android SDK
Http://developer.android.com/sdk

3. Install Eclipse
Http://www.eclipse.org/

4. Open Eclipse and install its Android plug-in (ADT)
Open the menu "Help"-> "Install New Software", add the address http://dl-ssl.google.com/android/eclipse/ to "Availabe Software", and then Install ADT (Android Development Tools)

5. Create an Android Project
"New"-> Android Project, Project Name-Project name; Build Target-SDK version of the compiled Project; Application name-program Name; Package name-Package name; min SDK Version-Minimum SDK Version code supported by the Program (2 corresponds to 1.1, 3 corresponds to 1.5, 4 corresponds to 1.6)

6. Run the Android Project
Choose "Run"-> "Run deployments"-> New launch configuration, set the startup project name, select a startup project on the Android tab, and set the simulator on the Target tab.

7. Create/use a simulated SD card
Create an SD card and run the command mksdcard-l sdcard 512 M d: \ android \ sdcard. img.
Use the SD card in the simulator and enter the following parameters in the "Additional Emulator Command Line Options" box on the Target tab of the project configuration:-sdcard d: \ android \ sdcard. img

8. Configure the simulator
Run the following command: android create avd -- name android15 -- target 2. Or directly configure the simulator in the menu "Window"-> "Android AVD Manager ".

9. Browse the contents in the simulated SD card
Debug the program and select "File Explorer" in DDMS. The sdcard directory contains the contents of the simulated SD card.

10. view the log LogCat
Window-> Show View-> Other-> Android-> LogCat

11. Install/uninstall apk in the simulator
Run the following command to install apk: adb install name.apk; run the command to uninstall apk: adb uninstall packagename (Note: The parameter here is the package name to be uninstalled)

12. decompile the Android Program
Decompress the apk file and retrieve the classes. dex file. Run the following command: dexdump.exe-d classes. dex> dump.txt (which means the classes. dex dump and save the decompiled code to the specified text file)

13. Poor character is a solution to some errors
If an error similar to the following occurs
No classfiles specified
Conversion to Dalvik format failed with error 1
Solution: Project-> Clean
Android SDK Content Loader 60% (stuck in 60%)
Solution: Project-> remove the check box before Build Automatically.

14. view the SDK source code
First find a way to get the source code, such as this address http://www.digginmobile.com/android.asp, and then decompress it to the SDK root path under the sources folder can be

Ii. directory structure of the Android Project
1. src-used to place the source program
2. gen-automatically generates the R. java file to reference the resource file (that is, the data in the res directory)
3. assets-used to place original files. Android does not process the files in this directory, which is different from the res directory.
4. res/drawable-used to place resources such as images; res/layout-used to place xml files for layout; res/values-used to place some constant data
5. The AndroidManifest. xml-configuration file of the Android program, which is equivalent to the configuration file, and configures the application name, icon, Activity, Service, and cycler.

3. Hello World Program
1. res/layout/main. xml

Code

<? Xml version = "1.0" encoding = "UTF-8"?>
<! --
Set the ID by prefix @ + ID/
To reference string resources in a resource file, prefix the specified resource name with @ string/
-->
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ + id/layout"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/txt"
/>
</LinearLayout>

2. res/values/strings. xml

Code

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> layout directly calls the string in values </string>
<String name = "hello2"> call the string in values programmatically </string>
<String name = "app_name"> webabcd_hello </string>
</Resources>

3. Place an image file named icon.png In the res/drawable directory.

4. AndroidManifest. xml

Code

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. webabcd. hello"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". Main"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
<Uses-sdk android: minSdkVersion = "3"/>
</Manifest>

5. Main. java

Code

Package com. webabcd. hello;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. LinearLayout;
Import android. widget. TextView;

Public class Main extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

// Use the specified layout file as the content displayed by the Activity
SetContentView (R. layout. main );

// Dynamically Add a new control to the specified Container Control
TextView txt = new TextView (this );
Txt. setText ("dynamically add controls ");
// SetContentView (txt );
(LinearLayout) this. findViewById (R. id. layout). addView (txt );

// Reference the content in the resource file as the output content
TextView txt1 = (textviewdomainthis.findviewbyid(r.id.txt );
Txt1.setText (this. getString (R. string. hello2 ));
}
}

OK
[Download source code]

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.