Android.bp入门指南之Android.mk转换成Android.bp

引言
  在前面的篇章初识Android.bp和Android.bp入门指南之语法初识我们对Android.bp是啥和基本语法都有了一个初步层次的认识。Android的妈咪谷歌当然也考虑到了,从Android.mk切换到Android,mk需要一定的时间,从而为我们精心开发了一个工具androidmk,但是这个工具不是很完善,对于一些Android.mk中的宏开关就无从处理了。本篇今天就带领大伙认识一下androidmk工具的使用。

注意:本篇是以Android 9的工程来进行讲解的。

一 初探androidmk
1.1 androidmk的源码和位置
首先让我们从androidmk的框架和源码如下来说起,我们知道Android的最新编译系统的文件架构如下:

Android.bp入门指南之Android.mk转换成Android.bp
xxx@Ubuntu16-Model:~/ssd/qcom_64/msm8953-9$ ls -l build
total 16
drwxr-xr-x 14 xxx pd 4096 Feb 17 14:18 blueprint
lrwxrwxrwx  1 xxx pd   25 Feb 20 11:17 buildspec.mk.default -> make/buildspec.mk.default
lrwxrwxrwx  1 xxx pd   17 Feb 20 11:17 CleanSpec.mk -> make/CleanSpec.mk
lrwxrwxrwx  1 xxx pd    9 Feb 20 11:17 core -> make/core
lrwxrwxrwx  1 xxx pd   16 Feb 20 11:17 envsetup.sh -> make/envsetup.sh
drwxr-xr-x  5 xxx pd 4096 Feb 17 14:18 kati
drwxr-xr-x  6 xxx pd 4096 Feb 20 15:05 make
drwxr-xr-x 21 xxx pd 4096 Feb 20 15:08 soong
lrwxrwxrwx  1 xxx pd   11 Feb 20 11:17 target -> make/target
lrwxrwxrwx  1 xxx pd   10 Feb 20 11:17 tools -> make/tools

而我们的androidmk的源码目录位置如下:

 Android.bp入门指南之Android.mk转换成Android.bp
xxx@Ubuntu16-Model:~/ssd/qcom_64/msm8953-9$ ls -l build/soong/androidmk/
total 12
-rw-r--r-- 1 xxx pd 1302 Feb 17 14:18 Android.bp
drwxr-xr-x 3 xxx pd 4096 Feb 17 14:18 cmd
drwxr-xr-x 2 xxx pd 4096 Feb 17 14:18 parser
编译Android源码以后,最好androidmk生成的位置如下:

Android.bp入门指南之Android.mk转换成Android.bp
xxx@Ubuntu16-Model:~/ssd/qcom_64/msm8953-9$ find .  -name androidmk -type f
./out/soong/host/linux-x86/bin/androidmk

1.2 androidmk的用法
在前面的章节,我们降解了androidmk的源码位置和编译完成之后的执行文件的路径,在这个章节我们将要讲解androidmk的使用,如下:

Android.bp入门指南之Android.mk转换成Android.bp
xxx@Ubuntu16-Model:~/ssd/qcom_64/msm8953-9$ androidmk 
usage: androidmk [flags] <inputFile>

 

androidmk parses <inputFile> as an Android.mk file and attempts to output an analogous Android.bp file (to standard out)
xxx@Ubuntu16-Model:~/ssd/qcom_64/msm8953-9$

用一句通俗的话来讲就是androidmk Android.mk > Android.bp


1.3 androidmk的实际使用
好了,前面说了这么多,都是为了实际运行,那么我们通过几个实例来看看androidmk究竟有啥魔力。

1.3.1 生成App
让我们先来看看生成App的Android.mk文件如下:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_JAVA_LIBRARIES := services
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SRC_FILES += \
    src/android/bluetooth/IXxxBtPrinter.aidl \

#LOCAL_SDK_VERSION := current
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_PACKAGE_NAME := XxxBtPrinter
#LOCAL_PROGUARD_FLAG_FILES := proguard.flags

#add this for generate and print barcode
LOCAL_STATIC_JAVA_LIBRARIES := zxing_api
LOCAL_DEX_PREOPT := false
LOCAL_CERTIFICATE := platform

include $(BUILD_PACKAGE)

# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := zxing_api:libs/core_3.2.0.jar
include $(BUILD_MULTI_PREBUILT)



执行转换命令:


//注意生成的不一定要叫做Android.bp可以叫做其它的
androidmk  Android.mk  > Android.bp


让我们先来看看生成Android.bp内容如下:

android_app {

    libs: ["services"],
    srcs: ["src/**/*.java"] + ["src/android/bluetooth/IXxxBtPrinter.aidl"],

    //LOCAL_SDK_VERSION := current
    // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_PRIVATE_PLATFORM_APIS
    // LOCAL_PRIVATE_PLATFORM_APIS := true

    name: "XxxBtPrinter",
    //LOCAL_PROGUARD_FLAG_FILES := proguard.flags

    //add this for generate and print barcode
    static_libs: ["zxing_api"],
    dex_preopt: {
        enabled: false,
    },  
    certificate: "platform",

}

// Use the folloing include to make our test apk.

// ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES
// LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := zxing_api:libs/core_3.2.0.jar

// ANDROIDMK TRANSLATION ERROR: unsupported include
// include $(BUILD_MULTI_PREBUILT)

这里可以看到预编译的转换是没有成功的,所以androidmk还是有一些不是很智能的,需要手动去修改。

1.3.2 生成可执行文件
先让我们看看生成可执行文件的Android.mk的定义:


LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := nativetest
LOCAL_SRC_FILES := apitest.c
LOCAL_CFLAGS += -Wno-error=implicit-function-declaration

LOCAL_SHARED_LIBRARIES :=  libcutils liblog     \   
libutils \
libxxxdevice \
libxxxapi \
libxxxcfg \
libxxxspdev \
libxxxlog\
libxxxadapter


LOCAL_C_INCLUDES +=  xxxdroid/external/xxx/device/include    \   
    xxxdroid/external/xxx/lib/libxxxapiclient  \
    xxxdroid/external/xxx/lib/libspdev \
    xxxdroid/external/xxx/lib/libxxxlog 

LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_SHARED_LIBRARIES += libcutils
include $(BUILD_EXECUTABLE)

转换后的Android.bp文件如下:

cc_binary {

    name: "nativetest",
    srcs: ["apitest.c"],
    cflags: ["-Wno-error=implicit-function-declaration"],

    shared_libs: [
        "libcutils",
        "liblog",
        "libutils",
        "libxxxdevice",
        "libxxxapi",
        "libxxxcfg",
        "libxxxspdev",
        "libxxxlog",
        "libxxxadapter",
    ] + ["libcutils"],
    include_dirs: [
        "xxxdroid/external/xxx/device/include",
        "xxxdroid/external/xxx/lib/libxxxapiclient",
        "xxxdroid/external/xxx/lib/libspdev",
        "xxxdroid/external/xxx/lib/libxxxlog",
    ],  

    local_include_dirs: ["."],

}

1.3.3 生成动态库
先让我们看看生成动态库的的Android.mk的定义:

LOCAL_PATH := $(call my-dir)

src_files := xxxmisc.c

src_files += default_misc.c

# Share Library
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += xxxdroid/external/systool/include

LOCAL_SRC_FILES := \
    ${src_files}

LOCAL_MODULE := libxxxmisc
LOCAL_SHARED_LIBRARIES := liblog libcutils
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)

# Static Library
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += xxxdroid/external/systool/include

LOCAL_SRC_FILES := \
    ${src_files}

LOCAL_MODULE := libxxxmisc
LOCAL_STATIC_LIBRARIES := liblog libcutils
LOCAL_MODULE_TAGS := optional
include $(BUILD_STATIC_LIBRARY)

转换后的Android.bp文件如下:

src_files = ["xxxmisc.c"]

src_files += ["default_misc.c"]

// Share Library
cc_library_shared {
    include_dirs: ["xxxdroid/external/systool/include"],

    srcs: src_files,

    name: "libxxxmisc",
    shared_libs: [
        "liblog",
        "libcutils",
    ],  

}

// Static Library
cc_library_static {
    include_dirs: ["xxxdroid/external/systool/include"],

    srcs: src_files,

    name: "libxxxmisc",
    static_libs: [
        "liblog",
        "libcutils",
    ],  

}


1.3.4 带宏开关转换失败
在这个Android.mk里面我们带上了宏编译:

Android.bp入门指南之Android.mk转换成Android.bp
include $(CLEAR_VARS)

 

LOCAL_MODULE_TAGS := optional

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

ifeq (1,$(strip $(shell expr $(PLATFORM_SDK_VERSION) \>= 28)))
LOCAL_SRC_FILES += compat/android-v28-later/XXUtilCompat.java
else
LOCAL_SRC_FILES += compat/default/XXUtilCompat.java
endif

LOCAL_SRC_FILES += \
    src/com/xxx/aidl/XxxSystemUtil.aidl \
    src/com/xxx/aidl/PhysicalKey.aidl \
    src/android/bluetooth/IXxxBtPrinter.aidl 

    
    
LOCAL_STATIC_JAVA_LIBRARIES := litepal


LOCAL_PROGUARD_FLAG_FILES := proguard.cfg
#LOCAL_SDK_VERSION := current
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_PACKAGE_NAME := XxxManager

LOCAL_CERTIFICATE := platform

执行androidmk命令进行转换:

android_app {

    libs: ["services"],
    srcs: ["src/**/*.java"] + ["compat/android-v28-later/SPUtilCompat.java"] + ["compat/default/SPUtilCompat.java"] + [ // ANDROIDMK            TRANSLATION ERROR: unsupported conditional
    // ifeq (1,$(strip $(shell expr $(PLATFORM_SDK_VERSION) \>= 28)))

        // ANDROIDMK TRANSLATION ERROR: else from unsupported contitional
        // else

        // ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
        // endif
        "src/com/xxx/aidl/XxxSystemUtil.aidl",

        //LOCAL_SDK_VERSION := current
        "src/com/xxx/aidl/PhysicalKey.aidl",
        // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_PRIVATE_PLATFORM_APIS
        "src/android/bluetooth/IXxxBtPrinter.aidl",
        // LOCAL_PRIVATE_PLATFORM_APIS := true
    ],  

    //the jni callback error!!!!! zhuxy
    static_libs: ["litepal"],

    optimize: {
        proguard_flag_files: ["proguard.cfg"],

        enabled: false,
    },  
    name: "XxxManager",

可以看出androidmk对宏的处理是不对的,这个就需要手动处理了,后续章节会重点说到这个的

结语
修行至此,恭喜读者你已经对Android.mk转换成Android.bp有一定了解了,行走江湖是不可能的了,还需多多磨练才行,在后续篇章中让我们一起学习。这里的androidmk不是万能的,有些情况是不行的,譬如有宏或者预编译的情况。

写在最后
  各位读者看官朋友们,Android.mk转换成Android.bp,希望能吸引你,激发你的学习欲望和斗志。在最后麻烦读者朋友们如果本篇对你有帮助,关注和点赞一下,当然如果有错误和不足的地方也可以拍砖。
 

上一篇:cmkae集成spdlog和json,其他项目同理


下一篇:TCP/IP 和 HTTP 的区别和联系