Android.mk编译App源码(Android. MK compile app source code)

在Andriod源码环境编译APP主要考虑如何引入第三方jar包和arr包的问题,初次尝试,步步是坑,这里给出一个模板:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_PACKAGE_NAME := TestApp

LOCAL_CERTIFICATE := platform

LOCAL_MANIFEST_FILE := AndroidManifest.xml

LOCAL_PRIVILEGED_MODULE := false

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_STATIC_JAVA_AAR_LIBRARIES := appcompat \
        cardview

LOCAL_STATIC_JAVA_LIBRARIES := com.google.gson

LOCAL_DEX_PREOPT := false

LOCAL_AAPT_FLAGS := --auto-add-overlay \
        --extra-packages android.support.v7.appcompat:android.support.v7.cardview

include $(BUILD_PACKAGE)

include $(CLEAR_VARS)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := com.google.gson:libs/gson-2.8.0.jar \
        appcompat:libs/appcompat-v7-23.0.0.aar \
        cardview:libs/cardview-v7-23.0.0.aar

include $(BUILD_MULTI_PREBUILT)

  • LOCAL_PATH:用来获取当前所在的目录;
  • CLEAR_VARS:每编译一个目标都会包含它来清除局部变量,避免上一个构建目标的局部变量对这个目标造成影响;
  • LOCAL_MODULE_TAGS : 用来表明什么情形才编译这个目标,optional表示无论什么情况都编译;
  • LOCAL_PACKAGE_NAME : 表示APP的名字,和当前目录名称一致;
  • LOCAL_CERTIFICATE : 使用平台签名;
  • LOCAL_MANIFEST_FILE: 指定APP的清单文件;
  • LOCAL_PRIVILEGED_MODULE: 为true时APK会生成在/system/pri-app下,为false放在/system/app下;
  • LOCAL_SRC_FILES : 指定需要编译的java源文件位置;
  • LOCAL_STATIC_JAVA_AAR_LIBRARIES: 指定依赖的第三方aar包,这条很重要;这里只是指定名称(相当于别名),具体aar包的位置在最后定义;
  • LOCAL_STATIC_JAVA_LIBRARIES : 指定依赖的jar包;
  • LOCAL_DEX_PREOPT: 是否进行odex优化,odex优化会拆分apk,生成xx.odex文件,加快运行速度;不过拆分的apk不能直接安装,不便于调试,所以这里设为false;
  • LOCAL_AAPT_FLAGS: 指定AAPT的运行参数,AAPT是Android Asset Packaging Tool,即Android的打包工具。Android的样式、字符串等可以看做是层层覆盖的,当基础包中有这个资源,上层的包中也有这个资源,则使用上层包中的这个资源;当基础包中有这个资源,上层的包中没有这个资源,–auto-add-overlay会为上层包添加这个缺失的资源。AAPT默认会将资源索引合成一个R.java文件,如:com.test.R.java,但这样会导致其它aar包找不到资源,因为它本来要可能要引用的是com.aar.R.java,–extra-packages参数可以根据包名生成多个R.java;
  • include $(BUILD_PACKAGE):会引入编译APK所需要的规则,即如何编译一个APk。到这条为止,生成TestApp这个目标的规则已经定义好了,下面是它所需要的依赖,即jar包、arr包的定义;
  • CLEAR_VARS:同样,清除一下局部变量;
  • LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES: 指定jar包和aar包的别名和位置;
  • BUILD_MULTI_PREBUILT: 引入构建jar包和aar包的详细构建规则;
————————

Compiling app in Android source code environment mainly considers how to introduce third-party jar package and arr package. The first attempt is a pit step by step. Here is a template:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_PACKAGE_NAME := TestApp

LOCAL_CERTIFICATE := platform

LOCAL_MANIFEST_FILE := AndroidManifest.xml

LOCAL_PRIVILEGED_MODULE := false

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_STATIC_JAVA_AAR_LIBRARIES := appcompat \
        cardview

LOCAL_STATIC_JAVA_LIBRARIES := com.google.gson

LOCAL_DEX_PREOPT := false

LOCAL_AAPT_FLAGS := --auto-add-overlay \
        --extra-packages android.support.v7.appcompat:android.support.v7.cardview

include $(BUILD_PACKAGE)

include $(CLEAR_VARS)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := com.google.gson:libs/gson-2.8.0.jar \
        appcompat:libs/appcompat-v7-23.0.0.aar \
        cardview:libs/cardview-v7-23.0.0.aar

include $(BUILD_MULTI_PREBUILT)

  • LOCAL_ Path: used to obtain the current directory;
  • CLEAR_ Vars: each compiled target will contain it to clear the local variables to avoid the impact of the local variables of the previous construction target on this target;
  • LOCAL_ MODULE_ Tags: used to indicate when the target is compiled. Optional means that it is compiled in any case;
  • LOCAL_ PACKAGE_ Name: indicates the name of the app, which is consistent with the name of the current directory;
  • LOCAL_CERTIFICATE : 使用平台签名;
  • LOCAL_MANIFEST_FILE: 指定APP的清单文件;
  • LOCAL_PRIVILEGED_MODULE: 为true时APK会生成在/system/pri-app下,为false放在/system/app下;
  • LOCAL_SRC_FILES : 指定需要编译的java源文件位置;
  • LOCAL_ STATIC_ JAVA_ AAR_ Libraries: specify the dependent third-party AAR package, which is very important; Here is only the specified name (equivalent to alias), and the location of the specific AAR package is defined at the end;
  • LOCAL_STATIC_JAVA_LIBRARIES : 指定依赖的jar包;
  • LOCAL_ DEX_ Preview: whether to perform ODEX optimization. ODEX optimization will split APK and generate XX ODEX files to speed up the running speed; However, the split APK cannot be installed directly and is not convenient for debugging, so it is set to false here;
  • LOCAL_ AAPT_ Flags: specify the operating parameters of AAPT, which is the Android asset packaging tool, that is, the Android packaging tool. Android styles, strings, etc. can be seen as layers of coverage. When the resource is in the basic package and the resource is also in the upper package, the resource in the upper package is used; When there is this resource in the base package but not in the upper package, – auto add overlay will add this missing resource to the upper package. AAPT will synthesize the resource index into an r.java file by default, such as com.java test. R. Java, but this will cause other AAR packages not to find resources, because it might have referenced com aar. R. Java, — extra packages parameter can generate multiple r.java according to the package name;
  • Include $(build_package): it will introduce the rules required for compiling APK, that is, how to compile an APK. So far, the rule for generating testapp has been defined. Here are the dependencies it needs, that is, the definitions of jar package and arr package;
  • CLEAR_ Vars: similarly, clear the local variable;
  • LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES: 指定jar包和aar包的别名和位置;
  • BUILD_MULTI_PREBUILT: 引入构建jar包和aar包的详细构建规则;