How to Use the android SDK to develop Android applications?

Source: Internet
Author: User

This is a root cause. Android is a new platform because Google has a powerful advantage and Java has been controlled by mobile phone vendors. Therefore, sun has very weak control over Java, so it is appropriate to learn Java.

I found a lot of explanations about android on the Internet. I found a good one and posted it to share it:

The tool chain and development tools provided by Android are relatively complete, so the development environment is relatively simple. I believe many of my friends have already set up the environment and compiled the helloactivity entry.Program. Here are a few questions:

1. What is the structure of the android file system? Where are the installed programs?
After the android source code is compiled, some files in out/target/product/generic:
Ramdisk. IMG, system. IMG, userdata. IMG, system, data, root
Here, system. IMG is packaged and compressed by system, and userdata. IMG is packaged and compressed by data.
Ramdisk. IMG is the file system of the simulator. After extracting ramdisk. IMG, you can see that the files in ramdisk. IMG are basically the same as those in the root folder.
The simulator loads ramdisk. IMG and decompress it To the memory. Then, the system. IMG and userdata. IMG are mounted to the system and data directories under ramdisk respectively. The compiled application is stored in system/app. The programs installed by the user are stored in data/app.

2. What tools does the android SDK and Android Source Code provide for us?
The android SDK provides many tools, such as ADB, ddms, emulator, and aapt. It also provides kernel-qemu, ramdisk. IMG, system. IMG, and userdata. IMG. Therefore, with the android SDK, we can run Android on the simulator.
Android source code can compile Android SDK, ADB, and other tools, Android file systems, and ADT plug-ins. That is to say, we can compile all Android-related things from Android source code.

3. Many tools and Android file systems (system. IMG, etc.), and we can use the "make SDK" to generate the android SDK. The Android SDK also includes tools and the android File System (system. IMG, etc.), and we also installed the android SDK during the original installation, So what tools and Android file systems should we use during development?
This question will be answered later.

4. We officially recommend using eclipse + ADT to develop applications. Our helloactivity program is also developed here. When we import the project under the android source code/packages/apps/into eclipse, the error that cannot find the package usually occurs. So how can we modify, compile, and debug the android source code? What tools does Google use to develop android?
This question will be answered later.

The following describes how to establish an android development environment and how to use development tools.

I. Installation of Android SDK and eclipse and establishment of Android Development Environment
Android tool chain is relatively complete and requires fewer external tools. For more information about the installation process, see the official documentation or <> and <>.
It should be noted that when "make" is used to compile the android source code, JDK 5 or JDK 6 can be used; when "make SDK" is used to compile the code, javadoc is used to generate the document, javadoc of JDK 5 must be used; otherwise, the compilation will fail. For this reason, we can install both jdk5and jdk6, and then point javadocand javadoc.1.gz to corresponding tools of jdk5. other tools still use jdk6. Of course, we can only install JDK 5 or only use JDK 5. For more information, see <>

2. Use eclipse to develop Android source code
For more information, see the official documentation.
Https://sites.google.com/a/android.com/opensource/using-eclipse

Next, we will summarize from the official documents how to use eclipse to develop the android source code.

1. Establish a basic Android Development Environment
Please refer to the official documentation or <>

2. Compile the android source code
Use make to compile the android source code in the root directory. Pay attention to some configurations. For more information, see <>

3. Copy the Eclipse project configuration file to the root directory of the android source code.
CP development/IDE/Eclipse /. classpath. /
chmod U + W. classpath # Make the copy writable
4. Modify the eclipse program configuration
1) and increase the eclipse memory Settings
change the eclipse. change the three values of INI (in the eclipse installation directory) to the following values:
-xms128m
-xmx512m
-XX: maxpermsize = 256 m
2), put the android-formatting.xml and Android. importorder import eclipse (optional)
android-formatting.xml ,. classpath and Android. importorder is placed in Development/IDE/E Clipse/
the android-formatting.xml is used to configure the Code style of the eclipse editor; Android. importorder is used to configure the sequence and structure of eclipse import.
Import android-formatting.xml in window-> preferences-> JAVA-> code style-> formatter
in window-> preferences-> JAVA-> code style-> organize imports import android. importorder
3) install the anyedit plug-in (optional)
download and import it to eclipseat at http://andrei.gmxhome.de/anyedit/

5. Import the android source code as a project to eclipse
check before importing. whether the files in classpath have corresponding files (folders) in the android source code; otherwise, the android source code will be damaged (usually multiple files/folders are added ),. you can delete unnecessary paths in classpath
Create a Java Project (not an android project; otherwise, the android source code is damaged), and select import from an existing project. The project name is arbitrary and the process is complete.
eclipse requires a build project during import, which is relatively slow. After the import, there are generally no errors.
4th questions are answered here

6. debug the android program on Eclipse.
to prevent other versions of Android and Android file systems from affecting compilation and debugging, you need to remove the paths of Android and Android file systems from the environment variables:
Vim ~ /. Bashrc
check whether the path of the android tool and Android file system is added to the PATH variable. If yes, comment it out. Through the following method, we do not need.
execution:
Cd Android source code directory
. build/envsetup. sh # After environment variables are set, mmm and other commands will be added. You can enter help to view
lunch 1 # Run emulator and other tools and ramdisk. you can directly call tools such as emulator by matching the paths of files such as IMG, and solve 3rd problems
emulator &
ddms &
note, start ddms first, and then start eclipse, so that there is no port conflict in eclipse
and then configure the debugging type and port in Eclipse:
double-click Run-> debug deployments-> remote Java application, and set "Host:" to localhost, "port:" To 8800, "con Nection type "is standard (socket attach)
then" Apply "
note: the port set above must be consistent with the port set in ddms, the ADT plug-in uses port 8700, so the port set above is 8800. If the VM cannot be connected, note that you must select a process (corresponding to an application) in ddms before executing debug in eclipse.
During eclipse debugging, you can set breakpoints and perform one-step debugging. It is estimated that the Google team developed and debugged Android applications in this way.

7. Compile the android Source Code
Run:
Cd Android source code directory
. build/envsetup. sh
then there will be more commands such as mm/mmm. mm/Mmm is used to compile modules (including C, C ++, and Java programs ). You can also run the "make Module name" command in the root directory of the android source code to compile the module (the module name can be found in the. mk file ). After the module is compiled, the corresponding .apk package is generated under out/target/product/generic/system/app. However, the. APK compiled with mm/mmmwill not be packaged into the system. in IMG, We need to manually package the system folder as system through make Snod. IMG, but you have to re-run the simulator, which is also very troublesome. For our developers, we can do this:
1. Remove the modified snapshot Library (such as alarmclock.apk) from/system/app, and then make snod. in this example, system.imghas no alarmclock.apk.
2) run the simulator and you will not be able to see alarmclock
3) modify the source code of alarmclock and compile it with mm/mmm, generate alarmclock.apk under/system/app< br> 4. Use adbto install alarmclock.apk to the android file system. There are two installation methods:
A. Use ADB install xxx/alarmclock.apk
B. Use ADB to push xxx/alarmclock.apk/data/APP
to install alarmclock to/data. /appnext, androidwill automatically display the category in the main menu (only the activity in alarmclock.apk contains android. intent. category. launcher attribute), but method A generates com.android.alarmclock.apk in/data/app, and method B generates alarmclo Ck.apk. When using method A, if alarmclock has been installed, you must first ADB uninstall it, and method B does not need. The B method is recommended. Similarly, you can use ADB uninstall or ADB shell RM xxx/xxx.apk to uninstall the SDK. You are also recommended to use the delete method.

8. How to develop your own projects
The previous section mainly describes how to develop the original Android project on Eclipse. For our own projects, we can do this:
1) Create an android project.
The advantage of building an android project is that you can make full use of the ADT function.
2) import the required package
3) Compile, run, and debug
4) Add the app to the corresponding Android source code Directory, which is usually stored in packages/apps.
When we observe the original project of packages/apps, we will find that their code is "clean", without folders such as assets and bin automatically generated by ADT and R. java, of course not. classpath and. project
5) write the makefile XXX. mk.
The dedicated makefile XXX. mk provided by the android source code is in a simple format.
6) Add the newly added project to the android project of eclipse
You can add a path in eclipse or in. classpath. For example, add the following content to. classpath:

R. Java is automatically generated during compilation. In fact, all projects that use "Resources" will use R. java. These R. java files are placed under out/target/common/R. During the makefull code under the source code root directory, modules that have been compiled and generated with a .apk file will not be compiled. Therefore, if the newly added project has been compiled (using mm/mmm), we need to change the "resource" of the project first (the "resource" must be changed because R. java is generated by a "resource"), and then make, you will see the r of your project in the package path corresponding to out/target/common/R. java. Refresh the out/target/common/R sub-project in eclipse, and then add its package to the place where you use the r class, as shown in
Import com. Android. example. Test. Inside. helloworld. R.
In fact, this error has no effect on us, because we compile it in shell.
7) Compile the newly added project in the android source code directory.
You can use the mm/Mmm Or make Module name.
8) Use a version control tool (SVN, git, or other) to upload the project to the server.

From the official documents and practices, we can summarize the following points:
1. You can use eclipse to edit Java programs and check for errors (mainly including class library inclusion and syntax). However, you cannot compile and run the android source code on Eclipse, still make (or mm or MMM) in Shell)
2. Some eclipse configuration files are provided in the android source code folder,
. Claapath: the configuration file of the eclipse project, so that we can directly import the file and Java package corresponding to the android source code to the project.
Android-formatting.xml and Android. importorder: This is very important, mainly used to regulate our encoding style, it is easier to make our code style consistent
3. When importing the android source code as a project into eclipse, pay attention to the following two points:
1) the newly created project must be a Java project, not an android project. Otherwise, the android source code will be damaged (generally multiple files/folders are added)
2) check whether the file in. classpath has a corresponding file (folder) in the android source code before import. Otherwise, the android source code will be damaged (generally add more files/folders)

In general:
1. Use eclipse to edit code and check for errors
2. If you do not compile or run the android source code program on Eclipse, you can only compile the android source code through make (or mm or MMM) on the command line.
3. You can debug the android source code program on Eclipse (Principle: Eclipse uses the ddms server to debug on emulator), and can debug and breakpoint in one step.
4. The program to be debugged removes it from/system/APP/and installs it under data/app, which is more convenient.
5. It is easier to install and uninstall programs by using ADB push and ADB shell RM.

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.