AOSP Platform Library And SDK-Addon

目的是在Vendor中创建一个项目,用于扩展系统功能,希望在编入到系统中,并且代码能够像framework.jar一样被加载,资源能够像framework-res.apk一样被系统加载使用。
在vendor 目录中添加一个系统平台编译模块,将资源和代码分开在不同的任务中进行编译。在Aosp 中 device/sample/frameworks/PlatformLibrary/ 目录地址中可以看到示例。
以下以/vendor/sample/ 为例,进行编译 sample framework library.

1.1. Add Platform Library

1.1.1. Add Jar Module

创建Android.mk 文件,并且添加以下内容:

#######################################################
include $(CLEAR_VARS)

LOCAL_MODULE := sample-framework
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_TAGS := optional
LOCAL_PROPRIETARY_MODULE := true
LOCAL_JACK_ENABLED := disabled
LOCAL_STATIC_JAVA_LIBRARIES := sample-framework.static
LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_DX_FLAGS := --multi-dex
LOCAL_JACK_FLAGS := --multi-dex native

include $(BUILD_JAVA_LIBRARY)

#######################################################
include $(CLEAR_VARS)

LOCAL_MODULE := sample-framework.static
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_TAGS := optional
LOCAL_PROPRIETARY_MODULE := true
LOCAL_JACK_ENABLED := disabled
LOCAL_STATIC_JAVA_LIBRARIES := $(SAMPLE_FRAMEWORK_JAR_PACKAGES)

LOCAL_DX_FLAGS := --multi-dex
LOCAL_JACK_FLAGS := --multi-dex native

include $(BUILD_STATIC_JAVA_LIBRARY)

1.1.2. Add Permisson Module

创建一个sample-framework.xml 文件,这个文件会被拷贝到 /vendor/permission/ 文件夹中,当系统Zygote 加载class 信息时,permission 文件夹中的xml 文件就是加载定义文件:




 


在Android.mk 文件中添加permission 编译任务。

#######################################################
include $(CLEAR_VARS)

LOCAL_MODULE := sample-framework.xml
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_ETC)/permissions
LOCAL_SRC_FILES := sample-framework.xml

include $(BUILD_PREBUILT)

1.1.3. Add Doc Module

在Android.mk 文件中添加编译生成droidoc 文档任务

# ============================================================
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-subdir-java-files) $(call all-subdir-html-files)

LOCAL_MODULE:= platform_library
LOCAL_DROIDDOC_OPTIONS :=
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_DROIDDOC_USE_STANDARD_DOCLET := true

include $(BUILD_DROIDDOC)

1.2. Add Resource Module

系统资源文件都会编入framework-res.apk 中,而系统Resource初始化加载System资源也只会加载framework-res.apk中,所以需要将Vendor 中定义的资源编入到framework-res.apk中,而系统也提供了编译时overlay机制,
而系统编译时overlay机制只能替换或修改 core/res/res/ 资源包的信息(包括--auto-add-overlay),所以我们需要另外的定义文件进行辅助。
Overlay 配置需要在AndroidProduct.mk 文件中配置 overlay即可。
PRODUCT_PACKAGE_OVERLAYS += vendor/sample/overlay

1.2.1. Add Non-Public Resource

创建add-res.xml 文件,添加资源文件定义:



    
    
    
    
    
    
    

创建symbols.xml 文件, 添加java文件调用定义:



    
    
    
    
    
    
    

1.2.2. Add Public Resource

在上面定义的资源信息都是private 资源,非公开化,无法使用com.android.internal.R 直接获取,如果需要公开资源,还需要创建公开资源定义,创建public.xml:



  

为了保证公开资源ID 的唯一性,还需要手动指定资源的ID信息:



  

其中ID 值的定义如下 0xAABBCCC:
AA: Package ID,相当于是一个命名空间,限定资源的来源。Android系统当前定义了两个资源命令空间,其中系统资源命令空间Package ID等于0x01,另外应用程序资源命令空间Package ID等于0x7f,位于[0x01, 0x7f]之间的Package ID都是合法的。
BB: Type ID,是指资源的类型ID。资源的类型有animator、anim、color、drawable、layout、menu、raw、string和xml等等若干种,每一种都会被赋予一个ID
CCCC: Entry ID,指每一个资源在其所属的资源类型中所出现的次序。注意,不同类型的资源的Entry ID有可能是相同的,但是由于它们的类型不同,我们仍然可以通过其资源ID来区别开来。

1.3. How To Use Platform Library

现在 sample-framework 已经编入到系统中,哪开发怎么应用使用呢? 当然我们可以直接导出sample-framework.jar 然后发布给开发使用,而这里我想介绍的是另外一种发布方式
sdk-addon: 这是一种将额外的platform library 配置到sdk 中,这样就可以直接发布sdk就可以。

1.3.1. Add SDK-Addon Product

1.3.1.1. 创建Android.mk 文件

include $(call all-subdir-makefiles)

1.3.1.2. 创建AndroidProducts.mk 文件

PRODUCT_MAKEFILES := $(LOCAL_DIR)/sample_sdk_addon.mk

1.3.1.3. 创建 basic_sdk_addon.mk

# List of apps and optional libraries to put in the add-on system image
PRODUCT_PACKAGES := \
    sample-framework

# The name of this add-on (for the SDK)
PRODUCT_SDK_ADDON_NAME := sample_framework_sdk_addon

# Copy the following files for this add-on's SDK
PRODUCT_SDK_ADDON_COPY_FILES := \
    $(LOCAL_PATH)/manifest.ini:manifest.ini \
    $(LOCAL_PATH)/manifest.ini:manifest.ini

# Copy the jar files for the libraries (APIs) exposed in this add-on's SDK
PRODUCT_SDK_ADDON_COPY_MODULES := \
    com.example.sample:libs/com.example.sample.jar

PRODUCT_SDK_ADDON_STUB_DEFS := $(LOCAL_PATH)/sample_framework_sdk_addon_stub_defs.txt

# Define the name of the documentation to generate for this add-on's SDK
PRODUCT_SDK_ADDON_DOC_MODULES := \
    com.example.sample_doc

# This add-on extends the default sdk product.
$(call inherit-product, $(SRC_TARGET_DIR)/product/sdk.mk)

# The name of this add-on (for the build system)
# Use 'make PRODUCT--sdk_addon' to build the add-on, 
# so in this case, we would run 'make PRODUCT-samle_framework_sdk_addon-sdk_addon'
PRODUCT_NAME := sample_framework_sdk_addon
PRODUCT_MODEL := Sampel Framework SDK Addon Image

1.3.1.4. 创建 sample_framework_sdk_addon_stub_defs.txt

+com.example.sample.*

1.3.1.5. 创建 manifest.ini

 
# SDK Add-on Manifest
# File encoding is UTF-8

# Name and vendor of the add-on.
# Add-ons are uniquely identified by a string composed of name, vendor and api.
# 2 add-ons with the same identifier cannot be installed in the same SDK
# and only the add-on with the highest rev number will be installed.
# Name and vendor are used to create folder names, so they shouldn't contain
# any special characters. Also, the character ':' is forbidden.
# Mandatory character set: a-z A-Z 0-9 _.-
name=Sample Framework Add-On
vendor=Gibsson
description=Sample Framework Add-on

# version of the Android platform on which this add-on is built.
api=27

# revision of the add-on. This must be a strict integer.
revision=1

# list of libraries, separated by a semi-colon.
# This must be the name of the libraries, as required by the
#  node in the AndroidManifest.xml file.
libraries=com.example.sample

# details for each library. format is:
# =.jar;
# where
# : the name of the library defined in the property "libraries" above.
# .jar: the jar file containing the library API. This is to be located in
#             the add-on folder in libs/
com.example.sample=com.example.sample.jar;Basic Service

# default skin name. Optional. Only useful if the add-on has its own skin, or
# if it wishes to override the default skin of the base platform.
# This should be the name of the skin in the skins/ folder of the add-on.
#skin=WVGAMedDpi

# USB Vendor ID
# This 16-bit integer allows adb to detect new devices, by extending the list
# of USB Vendor IDs it knows. After installing an add-on the command
# 'android update' adb' must be run to update a file that adb reads during
# start-up.
#usb-vendor=0x0000

1.3.1.6. 创建 hardware.ini

# Custom hardware options for the add-on.
# Properties defined here impact all AVD targetting this add-on.
# Each skin can also override those values with its own hardware.ini file.

1.3.1.7. make PRODUCT-samle_framework_sdk_addon-sdk_addon

* No rule to make target images/source.properties', needed byout/host/linux-x86/obj/SDK_ADDON/sample_library_intermediates/sample_library-eng.xxx-linux-x86-img'

1.3.1.8. Error Solution

1. Create file source.properties under device/sample/sdk_addon
2. Add one line in sample_addon.mk:
    PRODUCT_SDK_ADDON_SYS_IMG_SOURCE_PROP := $(LOCAL_PATH)/source.properties

1.3.2. Build addon.Xml

development/build/tools/mk_sdk_repo_xml.sh  out/host/linux-x86/sdk_addon/addon.xml \
prebuilts/devtools/repository/sdk-addon-01.xsd  add-on  any \
out/host/linux-x86/sdk_addon/sample_library-eng.xxx-linux-x86.zip:sample_library-eng.xxx-linux-x86.zip

1.4. How To Import SDK-Addon In AndroidStudio

SDK-Addon.png

你可能感兴趣的:(AOSP Platform Library And SDK-Addon)