什么是Android.mk文件?

什么是Android.mk文件?

NDK工具下载地址 -http://developer.android.com/tools/sdk/ndk/index.html -;
– windows版本NDK:android-ndk-r9c-windows-x86.zip (32位),android-ndk-r9c-windows-x86_64.zip (64位) 该版本是用在windows上的Cygwin下, 不能直接在windows上直接运行;
– linux版本NDK :android-ndk-r9c-linux-x86.tar.bz2(32位) , android-ndk-r9c-linux-x86_64.tar.bz2 (64位) , 该版本直接在linux下执行即可;
解压 后: android-ndk-r9c 目录下打开documentation.html 文档,选择左边Android.mk目录。

Android.mk file syntax specification

Android.mk文件语法规范

Introduction:

简介:

    This document describes the syntax of Android.mk build file written to describe your C and C++ source files to the Android NDK. To understand what follows, it is assumed that you have read the OVERVIEW file that explains their role and usage.

本文档介绍了书写Android.mk编译文件的语法,用来在Android NDK中描述你的C和C++源文件。为了理解下面的内容,你需要阅读OVERVIEW部分来了解它们的作用和用法。

Overview:

概述:

    An Android.mk file is written to describe your sources to the build system. More specifically:
    The file is really a tiny GNU Makefile fragment that will be parsed one or more times by the build system. As such, you should try to minimize the variables you declare there and do not assume that anything is not defined during parsing.

一个Android.mk文件是用来描述编译系统的源码的。更具体地说:
该文件是GNU Makefile的一小部分,在编译系统时将被解析一次或多次。因此,您应该尽量减少声明中的变量,不要假设在解析过程中没有定义任何参数。

    The file syntax is designed to allow you to group your sources into 'modules'. A module is one of the following:

文件语法的目的是让你把源代码组织成模块。模块是下面的其中之一:

    A static library.
    静态库。
    A shared library.
    共享库、动态库。
    A standalone executable.
    独立的可执行文件。

    Only shared libraries will be installed/copied to your application package. Static libraries can be used to generate shared libraries though.
    You can define one or more modules in each Android.mk file, and you can use the same source file in several modules.

只有共享库可以被安装或复制到你的应用程序包。静态库可以用于生成共享库。
你可以在每个Android.mk文件中定义一个或多个模块,而且你可以在不同的模块中使用相同的源文件。

    The build system handles many details for you. For example, you don't need to list header files or explicit dependencies between generated files in your Android.mk. The NDK build system will compute these automatically for you.
    This also means that, when updating to newer releases of the NDK, you should be able to benefit from new toolchain/platform support without having to touch your Android.mk files.
    Note that the syntax is very close to the one used in Android.mk files distributed with the full open-source Android platform sources. While the build system implementation that uses them is different, this is an intentional design decision made to allow reuse of 'external' libraries' source code easier for application developers.

编译系统为你处理许多细节。例如,你不需要在Android.mk文件中列出头文件,或者指明生成文件的差异。NDK编译系统将自动为你处理这些。
这也就意味着,当更新到NDK新发布的版本,你可以获取到新工具/平台的支持,而不需要改动你的Android.mk文件。
注意语法非常接近一个用于Android.mk文件分布在完全开源的Android平台。虽然使用它们的编译系统实现不同,但这是一种有意设计的决策,允许应用程序开发人员更容易地重用外部库的源代码。

Content

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello-jni

LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

LOCAL_PATH := $(call my-dir)

    An Android.mk file must begin with the definition of the LOCAL_PATH variable. It is used to locate source files in the development tree. In this example, the macro function 'my-dir', provided by the build system, is used to return the path of the current directory (i.e. the directory containing the Android.mk file itself).

一个Android.mk文件必须以定义local_path变量开始,它是用来在具体的开发环境中定位源文件。在这个例子中,宏函数my-dir,由编译系统提供,用于返回当前目录的路径(即Android.mk文件所在的路径)。

include $(CLEAR_VARS)

    The CLEAR_VARS variable is provided by the build system and points to a special GNU Makefile that will clear many LOCAL_XXX variables for you (e.g. LOCAL_MODULE, LOCAL_SRC_FILES, LOCAL_STATIC_LIBRARIES, etc...), with the exception of LOCAL_PATH. This is needed because all build control files are parsed in a single GNU Make execution context where all variables are global.

clear_vars变量是由编译系统提供的,指向一个特殊的GNU Makefile,将为你清除很多local_xxx变量(如local_module,local_src_files,local_static_libraries,等等),但local_path例外。这是必需的,因为所有编译控制文件都是在一个GNU make执行上下文中解析的,所有变量都是全局变量。

LOCAL_MODULE := hello-jni

    The LOCAL_MODULE variable must be defined to identify each module you describe in your Android.mk. The name must be unique and not contain any spaces. Note that the build system will automatically add proper prefix and suffix to the corresponding generated file. In other words, a shared library module named 'foo' will generate 'libfoo.so'.
    IMPORTANT NOTE: If you name your module 'libfoo', the build system will not add another 'lib' prefix and will generate libfoo.so as well. This is to support Android.mk files that originate from the Android platform sources, would you need to use these.

local_module变量必须被定义,来指定你的Android.mk中描述的每一个模块。该名称必须是唯一的,而且不能包含任何空格。注意编译系统将自动在生成相应的文件中添加合适的前缀和后缀。换句话说,一个名为‘foo’的动态库模块将生成libfoo.so。
注意:如果你命名模块为libfoo,编译系统将不会添加另一个lib前缀,还是生成libfoo.so。这是为了支持你要用到的Android平台源码的Android.mk文件。

LOCAL_SRC_FILES := hello-jni.c

    The LOCAL_SRC_FILES variables must contain a list of C and/or C++ source files that will be built and assembled into a module. Note that you should not list header and included files here, because the build system will compute dependencies automatically for you; just list the source files that will be passed directly to a compiler, and you should be good.
    Note that the default extension for C++ source files is '.cpp'. It is however possible to specify a different one by defining the variable LOCAL_CPP_EXTENSION. Don't forget the initial dot (i.e. '.cxx' will work, but not 'cxx').

local_src_files变量必须包含一个会被编译并组织成模块的C或C++源文件列表。请注意,不要在这里列出头文件和包含的文件,因为编译系统将自动为您添加依赖项;只要列出会直接传递给编译器的源文件就ok了。

注意,C++源文件的默认扩展名是‘.cpp’,不过,可以通过定义变量local_cpp_extension指定一个不同的对象。别忘了开始的那一个点“.”(即 “.CXX”可行,但“CXX”是不行的)。

include $(BUILD_SHARED_LIBRARY)

    The BUILD_SHARED_LIBRARY is a variable provided by the build system that points to a GNU Makefile script that is in charge of collecting all the information you defined in LOCAL_XXX variables since the latest 'include $(CLEAR_VARS)' and determine what to build, and how to do it exactly. There is also BUILD_STATIC_LIBRARY to generate a static library.

build_shared_library是由编译系统提供的变量,指向一个GNU Makefile脚本,用来收集你所定义的“LOCAL_XXX”变量,自从最近的’include $(CLEAR_VARS)’决定如何明确的编译,同时,BUILD_STATIC_LIBRARY 会生成静态库。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值