Write Android.mk / Android.bp to reference the third-party jar package, aar package, so library

I. Introduction

        After Android10, in all project projects, it is officially recommended to use Android.bp to compile and build. Projects previously built using Android.mk will gradually need to be changed to Android.bp as the versions are iteratively upgraded. Both syntaxes need to be understood. And use it skillfully. The author has previously written an introduction to the statements of Android.mk, which introduces the meaning of each code. Write Android.mk and compile the Android studio project into the AOSP source code.

Later, I also summarized the basic usage of Android.bp:  The syntax and usage of Android.bp    are relatively basic, and they are recorded after being used in the project.

This article is about writing Android.mk and Android.bp files in the project project, introducing third-party jar packages, aar packages, so libraries, and introducing privileged whitelist permission xml files.

2. Android.mk preparation

1. Introduce jar package

For example, the libs in our current directory have the okhttp-3.4.11.jar package, and I want to reference it

Requires two steps

The first step is to declare the directory where our jar package is located. Mine is the AndroidStudio project directory, so the path of the jar package is app/libs/okhttp-3.4.11.jar

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := okhttp3:app/libs/okhttp-3.4.11.jar

The meaning of this line of code can be roughly understood as follows: declare a variable okhttp3, and its value is app/libs/okhttp-3.4.11.jar

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := okhttp3:app/libs/okhttp-3.4.11.jar \
                                        okio:app/libs/okio-1.9.0.jar
include $(BUILD_MULTI_PREBUILT)  #这里是多个jar包预编译

Step 2. Reference the variables in the jar package we declared and reference the okhttp3 we declared above.

LOCAL_STATIC_JAVA_LIBRARIES := okhttp3

LOCAL_STATIC_JAVA_LIBRARIES := okhttp3 \
                               okio

2. Introduce aar package

Step 1: Declare the location of the aar package first

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := \
   lottie-2.8.0:app/libs/lottie-2.8.0.aar
include $(BUILD_MULTI_PREBUILT)

Step 2: Reference the declared aar variable lottie-2.8.0

LOCAL_STATIC_JAVA_AAR_LIBRARIES := lottie-2.8.0

Step 3: Add referenced aar package resources

LOCAL_AAPT_FLAGS := \
  --auto-add-overlay \
  --extra-packages com.airbnb.lottie

3. Import the so library

Suppose there are armeabi-v7a and arm64-v8a directories in the lib directory in our current directory, which contain libaes-jni.so respectively. If we want to package these so libraries when compiling apk, what should we do in the mk file? What about configuration?

The first step is to configure the following content directly in the mk file and pre-compile it

include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
#后缀
LOCAL_MODULE_SUFFIX := .so
#模块名
LOCAL_MODULE := libaes-jni
#编译到system/libs/目录下
LOCAL_MODULE_CLASS := SHARED_LIBRARIES

#另外一种写法,根据平台arm  arm64 去选择编译对应的so文件
#ifeq ($(strip $(TARGET_ARCH)),arm64)
#LOCAL_SRC_FILES :=app/libs/arm64-v8a/libaes-jni.so
#else ifeq ($(strip $(TARGET_ARCH)),arm)
#LOCAL_SRC_FILES :=app/libs/armeabi-v7a/libaes-jni.so
#endif

#arm arm64都编译
LOCAL_SRC_FILES_arm :=app/libs/armeabi-v7a/libaes-jni.so
LOCAL_SRC_FILES_arm64 :=app/libs/arm64-v8a/libaes-jni.so
LOCAL_MODULE_TARGET_ARCHS:= arm arm64
LOCAL_MULTILIB := both
include $(BUILD_PREBUILT)

Step 2: Reference the target so library

Add the following between include $(CLEAR_VARS) and include $(BUILD_PACKAGE)

LOCAL_REQUIRED_MODULES := libaes-jni


#如果你写这句话 把这个libaes-jni.so 同时也打包 放在out/target/产品名路径/你的apk具体路径下/lib/#目录中
#LOCAL_JNI_SHARED_LIBRARIES := libaes-jni

Remark:

LOCAL_MODULE_CLASS: (prebuilt) identifies the last location where the compiled module is placed. If not specified, it will not be placed in the system.
LOCAL_MODULE_CLASS := ETC is placed in the system/etc directory
LOCAL_MODULE_CLASS := EXECUTABLES is placed in the /system/bin directory
LOCAL_MODULE_CLASS := SHARED_LIBRARIES is placed in the /system/lib directory LOCAL_MODULE_CLASS :
= JAVA_LIBRARIES
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_MODULE_CLASS : = APPS placed in /system/app Table of contents
 

The LOCAL_JNI_SHARED_LIBRARIES variable is mainly used in JNI compilation. If you want to reference the shared library *.so in JNI in your Java code, this variable is the name of the shared library.
Then one thing you need to pay attention to is: define this variable in Android.mk in the root directory of your Project to reference the shared library *.so in JNI you want to use. It defines the name of the so library file to be included. If the program does not use jni, there is no need


LOCAL_JNI_SHARED_LIBRARIES := libxxx In this way, when compiling, NDK will automatically package this libxxx into apk; put it in the out/target/product name path/your apk specific path/lib/ directory

LOCAL_REQUIRED_MODULES specifies the modules that the module depends on (when the module is installed, the modules it depends on will be installed simultaneously)

4. Introduce privileged whitelist permission xml

com.test.mtk.xml content:

<?xml version="1.0" encoding="utf-8"?>
<permissions>
    <privapp-permissions package="com.test.mtk">
        <permission name="android.permission.MASTER_CLEAR" />
    </privapp-permissions>
</permissions>

The first step: pre-compilation processing


######预编译priv-app 权限,输出路径为system/etc/permissions###########
# Permissions pre-grant
include $(CLEAR_VARS)
LOCAL_MODULE := com.test.mtk.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)

Step 2: Specify the modules that the app depends on for running


######编译priv-app 权限到apk中###########
LOCAL_REQUIRED_MODULES := com.test.mtk.xml

Guess you like

Origin blog.csdn.net/u012514113/article/details/133362683