ANDROID.MK File Details

Source: Internet
Author: User

Source: Android.mk File detailed

From the ignorance of makefile began, tossing for one weeks, finally to ANDROID.MK have a comprehensive understanding. After understanding the standard makefile, found that android.mk is actually the real makefile packaging, made a very simple thing for users. Using it to compile the program, whether it is a dynamic library, executable binaries, or jar libraries, APK package, just follow a simple idea to do three big strides: clear the old variable, set a new variable, call the compiler function.

After understanding, found that makefile grammar is not a problem, there are many tutorials and masters. It is not a problem to compile a module, such as how to clear variables, call compilation functions, etc. For beginners, it may be more important to understand how Android allows a script to be freed from the makefile syntax, simply by following the three big steps above to compile any module.

Take AlarmClock for example:

Clear Old variables

Local_path:= $ (call My-dir)

Include $ (clear_vars)

Set a new variable

Local_src_files: = $ (call all-subdir-java-files)

Local_package_name: = AlarmClock

Calling the Compile function

Include $ (build_package)

Here's a brief explanation of these three steps:

1, the old variable is cleared because all the variables in ANDROID.MK are global, and the compile function calls these variables at compile time. To prevent the compilation function from using the variables set when compiling other modules, it is a good practice to clear all variables every time you start compiling a new module.

2, set a new variable is to compile the use of the source address, package name and other settings.

3, call the compiler function is to include a fixed MK file, the Mk file will be based on the set of variables to extract the Target,command needed to compile the module and other information and execute a fixed compilation command.

See ANDROID.MK, the deepest feeling or the Android packaging idea, reminds me of a lot of things I have not thought about before. Oh...

I will have some personal understanding of the recording of Android step in the evolution of this encapsulation idea. I sincerely welcome the criticism and correction.

Makefile this thing, to the simplest point, is this pattern:

Goal: Trust files

Execute command

The syntax details are not taken into account here, for an incomplete example:

AlarmClock.apk:AlarmClock.java

Javac Alarmclock.java

Java Alarmclock.class

To generate alarmclock.apk, you need to alarmclock.java this trust file and then execute the next two lines of two commands. It would be pretty tiring if we were to compile the Android system simply and rudely. The android.mk skillfully abstracted them. Let's simulate this now:

This is done by defining three variable Target,source,command, which represent the compilation target, the trust file, and the Compile command, respectively:

$ (target): $ (source)

$ (command)

The values for these three variables are:

Target: = alarmclock.apk

Source: = Alarmclock.java

COMMND: =/

Javac alarmclock.java/

Java Alarmclock.class

In this case, when compiling each apk, just assign a value to target, source, and command, and then you can write it again:

$ (target): $ (source)

$ (command)

Well, it's a bit of a problem, that's a little more abstract. Define a variable name so that it holds the name of the module to be compiled:

Name: = AlarmClock

Then change the previous three variables:

Target: = $ (name). apk

Source: = $ (name). java

COMMND: =/

Javac $ (name). java/

Java $ (name). class

What do I need to do now to compile a module contacts?

Name: = Contacts

$ (target): $ (source)

$ (command)

Three lines of code, it's simple, right? But the next two lines still have to be written each time, that is again abstract. Put the next two lines into a build_apk.mk file. Then define a variable:

Build_packages: = build_apk.mk

All finished, this time to compile a module contacts as long as this can be done:

Name: = Contacts

Include $ (build_packages)

One that's not complex enough, that's a little more:

Name: = Contacts

Include $ (build_packages)

Name: = Phone

Include $ (build_packages)

Name: = Email

Include $ (build_packages)

================ Strong Divider Line =========

Now let's refine the process above and systematize it.

Define a CLEAR_VARS.MK that is specifically used to clear variables as follows:

Name: =

Target: =

Source: =

Command: =

Then define a variable clear_vars: = Clear_vars.mk. The above compilation script becomes this:

Include $ (clear_vars)

Name: = Contacts

Include $ (build_packages)

Include $ (clear_vars)

Name: = Phone

Include $ (build_packages)

Include $ (clear_vars)

Name: = Email

Include $ (build_packages)

This time, clear the old variables, set new variables, call the compiler function three big strides all have.

OK, now we go into the Android source build/core/directory. First look at the main.mk,config.mk of the two files.

Main.mk inside is a specific script, which controls the compilation module here.

The following file invocations are defined in CONFIG.MK:

Clear_vars:= $ (Build_system)/clear_vars.mk

Build_host_static_library:= $ (Build_system)/host_static_library.mk

When compiling a module, each include is actually a function call, or a call to a Mk file, or a call to a variable or function. The MK files that are abstracted as separate function modules are all in the Build/core/directory, and the function definitions are all in definitions.mk.

Everything starts hard, and with the ideas above, it's easy to look at these scripts. Bon Voyage, Oh ...

ANDROID.MK file Details (GO)

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.