Android.bp入門指南之Android.mk轉換成Android.bp

    Android.bp入門指南之Android.mk轉換成Android.bp


前言

  在前面的篇章初識Android.bpAndroid.bp入門指南之語法初識咱們對Android.bp是啥和基本語法都有了一個初步層次的認識。Android的媽咪谷歌固然也考慮到了,從Android.mk切換到Android,mk須要必定的時間,從而爲咱們精心開發了一個工具androidmk,可是這個工具不是很完善,對於一些Android.mk中的宏開關就無從處理了。本篇今天就帶領大夥認識一下androidmk工具的使用。java

注意:本篇是以Android 9的工程來進行講解的。linux



一 初探androidmk

1.1 androidmk的源碼和位置

首先讓咱們從androidmk的框架和源碼以下來講起,咱們知道Android的最新編譯系統的文件架構以下:
在這裏插入圖片描述
android

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的源碼目錄位置以下:
在這裏插入圖片描述
web

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生成的位置以下:
在這裏插入圖片描述
shell

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的使用,以下:
在這裏插入圖片描述
api

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.bpbash


1.3 androidmk的實際使用

好了,前面說了這麼多,都是爲了實際運行,那麼咱們經過幾個實例來看看androidmk究竟有啥魔力。架構

1.3.1 生成App

讓咱們先來看看生成App的Android.mk文件以下:app

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裏面咱們帶上了宏編譯:
在這裏插入圖片描述

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,但願能吸引你,激發你的學習慾望和鬥志。在最後麻煩讀者朋友們若是本篇對你有幫助,關注和點贊一下,固然若是有錯誤和不足的地方也能夠拍磚。

本文分享 CSDN - IT先森。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索