Android source code compilation and Development Summary

Source: Internet
Author: User
Document directory
  • Start with a question
  • The following describes how to establish an android development environment and how to use development tools.
  • From the official documents and practices, we can summarize the following points:

 

From http://blog.csdn.net/zhangchiytu/archive/2011/04/08/6310488.aspx

(This article describes how to develop the android source code, including the source code of each platform, instead of the source code released by Google. I use msm3167.

The SDK is not required and the ADT is not required. It can be generated through the compilation of the entire project. To debug the source code in eclipse, such as phone, MMS, and contact ......

Since I have never touched on Java, Eclipse, ADT..., I have read more than N Articles on the Internet. In this article, I posted them for sharing and added them,

Configure the troubshooting. If you are new to Android, you may not be able to configure these settings at half past one, so be prepared.

Official Website: http://source.android.com/source/using-eclipse.html

Start with a question

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?
Android
The SDK provides many tools, such as ADB, ddms, emulator, and aapt, and provides kernel-qemu, ramdisk. IMG,
System. IMG, 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. After the android source code "make" is used, many tools and Android file systems (such as system. IMG) will be generated.
Android SDK, Android
The SDK also includes tools and Android file systems (such as system. IMG ).
SDK, which tools and Android file system should we use during development?
This question will be answered later.

4. officially recommended
Eclipse + ADT is used to develop applications. Our helloactivity program is also developed here. When the android source code/Packages
When a project under/apps/is imported into eclipse, the package cannot be found. So how can we modify, compile, and debug the android source code? What kind of work does Google use?
For Android development?
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 <Android simulator installation in Ubuntu 8.10> and <Android source code compilation>.
This
When using make to compile the android source code, we can use JDK 5 or JDK 6;
Javadoc is used to generate documents during SDK compilation. javadoc must use javadoc of jdk5. otherwise, the compilation will fail. Therefore, we can

And jdk6are installed, then only javadocand javadoc.1.gz point to the corresponding jdk5 tools, other tools are still using jdk6. Of course, we can only install JDK 5
Or use only JDK 5. For more information, see <Android source code compilation>

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 <Android simulator installation in Ubuntu 8.10>

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 <Android source code compilation>

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 configuration of the eclipse Program
1) Increase eclipse memory settings
Change the three values of eclipse. ini (under the eclipse installation directory) to the following values:
-Xms128m
-Xmx512m
-XX: maxpermsize = 256 m
2), the Android-formatting.xml and Android. importorder into eclipse (optional)
The Android-formatting.xml,. classpath, and Android. importorder are all placed in the development/IDE/Eclipse/
Android-formatting.xml is used to configure the code style of the eclipse editor; Android. importorder is used to configure the order and structure of the eclipse import.
Import Android-formatting.xml in window-> preferences-> JAVA-> code style-> formatter
Import Android. importorder to WINDOW> preferences> JAVA> code style> organize imports.
3) install the anyedit plug-in (optional)
Download and import data to eclipseat at http://andrei.gmxhome.de/anyedit/

5. Import the android source code as a project into 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 ),. the redundant paths in classpath can be deleted.
Create a Java Project (not an android project; otherwise, the android source code will be damaged). Select www.linuxidc.com to import the existing project. The project name is arbitrary and complete.
It is slow to build a project in eclipse during import. After the import, there are generally no errors.
4th questions are answered here

Note:

1. incorrect steps for creating a new Java project will also cause parsing errors. I cannot select to import an existing project from eclipse in the new Java project. Therefore, follow these steps to create and import the project first:

New: New Java Project

Enter the project name.

If the default location is not used, specify the location as the Code directory, that is

Uncheck "use default location", select the project root directory for location (this must be selected, otherwise the package list will have an error), click Finish, it takes some time to start building the workspace .....

Import: file-> Import-> select, General select exiting projects into workspace,-> next select the source code root directory, and finish.

All associated source codes are listed in the package explorer of Eclipse.

2. If there is an error in parsing, debugging will fail. The biggest problem I encountered is that there is an error in the problem window in eclipse. The following is a solution.

Delete the following two rows of. classpath

<Classpathentry kind = "lib" Path = "Out/target/common/obj/java_libraries/Google-common_intermediates/javalib. Jar"/>
<Classpathentry kind = "lib" Path = "Out/target/common/obj/java_libraries/gsf-client_intermediates/javalib. Jar"/>
Tian Jia

<Classpathentry kind = "lib" Path = "Out/target/common/obj/java_libraries/android-common_intermediates/javalib. Jar"/>

At this time, an error occurs in eventlogtags in the calendarprovider and contactprovider, and then modify the following:

1. Expand packages/providers/calendarprovider/src
2. Expand the package: COM. Android. providers. Calendar
3. Right-click the package com. Android. providers. Calendar
4. Select "New File"
5. Click "Advanced>" in the dialog box to see "link to file in the file system" checkbox,
6. Check checkbox. Then "Browse..." is available.
7.
Click "Browse..." and select eventlogtags. Java
(E.g., out/target/common/obj/apps/calendarprovider_intermediates/src/COM/Android/providers/calendar/eventlogtags. Java)
8. Click OK.
9. Click Finish (in "New File ")
Modifying com. Android. providers. Contacts in the same way

In this way, Eclipse only has warning to parse the entire source code.

6. debug the android program on Eclipse.
To prevent other versions of Android tools and Android file systems from affecting compilation and debugging, you need to remove the paths of Android tools 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. In the following method, we do not need to add the path of the android tool and the android File System to. bashrc.
Run:
CD Android source code directory

. Build/envsetup. Sh # After the environment variable is set, mmm and other commands will be added. You can enter help to view
Lunch 1 # maps emulator and other tools with the paths of files such as ramdisk. IMG, you can directly call emulator and other tools, and solve 3rd Problems
Emulator &
Ddms &
Note: first start ddms and then start eclipse, so that there will be no port conflict in eclipse.
Configure the debugging type and port in Eclipse:
Run-> debug
Deployments-> remote Java
Double-click application, and set "Host:" to localhost, "port:" To 8800, "connection
Type "is standard (socket attach)
Then "Apply"
Note: The port set above should be the same as the port set in ddms (view path
Path file --> preference, select debugger on the left and port of selected on the right
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 (
An application can be used to run debug in eclipse (in the Java view, click open at right-top ).
Perspective, select debug to enter the debug window ).
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 ). We can also directly go to the android Source
Run "make" in the root directory of the Code.
Module name to compile the module (the module name can be found in the. mk file ). After the module is compiled, it will be generated in the out/target/product/generic/system/APP
The corresponding. APK package. However, the. APK compiled with mm/mmmwill not be packaged into system. IMG. We need to manually use make Snod
The system folder is packaged as system. 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 kernel (such as alarmclock.apk) from the out/target/product/generic/system/app, and then make snod,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. Then, generate alarmclock.apk under/system/app.
4. Install alarmclock.apk to the android file system through adb. there are two installation methods:
A. Run ADB install xxx/alarmclock.apk
B. Push xxx/alarmclock.apk/data/APP through ADB
Two
You can install alarmclock in/data/app.
Automatically display the inventory in the main menu (only the activity package in alarmclock.apk is required)
Contains the Android. Intent. Category. launcher attribute), but method A is generated in/data/app.
Com.android.alarmclock.apk. The method B is alarmclock.apk. If the method is used
Alarmclock, you must first ADB uninstall it, and the B method is not used. The B method is recommended. Similarly, you can use the ADB
Uninstall or ADB shell RM xxx/xxx.apk. We recommend that you use the delete method to uninstall the SDK.

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:
<Classpathentry kind = "src" Path = "packages/apps/helloworld/src"/>
R. Java

Is automatically generated during compilation. In fact, all projects that use "Resources" will use R. java. These R. Java are placed under out/target/common/R. In the source code

When the makefull code is recorded in the root directory, the r.javaworkflow generated for each worker will not be compiled when it has been compiled and generated with a .apk file. Therefore, if the newly added

If the 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 "Resources"), and then make

You can see the R. Java of your project in the package path corresponding to out/target/common/R. Refresh out/target/common/R in eclipse
Child project, and then add its package where you use the r class, such
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.

**************************************** **************************************** ******************************

7. Supplement Android source code compilation (compilation module)

An application in android can be compiled separately. After compilation, system. IMG must be regenerated.

Run the following command in the source code directory:

. Build/envsetup. Sh (. There is a space behind it)

There are more commands:

-Croot: Changes directory to the top of the tree.

-M: makes from the top of the tree.

-Mm: builds all of the modules in the current directory.

-Mmm: builds all of the modules in the supplied directories.

-Cgrep: greps on all local C/C ++ files.

-Jgrep: greps on all local java files.

-Resgrep: greps on all local Res/*. XML files.

-Godir: Go to the directory containing a file.

You can add-help to view usage

We can use Mmm to compile the module of the specified directory, for example, compile the contact:

Mmm packages/apps/contacts/

Generate two files after editing:

Out/target/product/generic/data/APP/contactstests.apk

Out/target/product/generic/system/APP/contacts.apk

You can use make Snod to regenerate system. IMG.

Run the simulator again.

8. My situation

When I modify the source code in eclipse, use step 1 Mmm

 

Packages/apps/contacts/and then make Snod before emulator. emulator cannot run.

 

 

Next, let's take a look at the instructions in the official Android documentation.

cd /path/to/android/root 

. build/envsetup.sh lunch 1    make       emulator

Successful

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.