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 文件就是加载定义文件:

<?xml version="1.0" encoding="utf-8"?>

<permissions>
 <library name="sample.framework"
          file="/vendor/sample/sample-framework.jar"/>
</permissions>

在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 文件,添加资源文件定义:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <add-resource type="drawable" name="xxxx" />
    <add-resource type="styleable" name="xxxx" />
    <add-resource type="attr" name="xxxx" />
    <add-resource type="id" name="xxxx" />
    <add-resource type="color" name="xxxx" />
    <add-resource type="dimen" name="xxxx" />
    <add-resource type="style" name="xxxx" />
</resources>

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <java-symbol type="drawable" name="xxxx" />
    <java-symbol type="styleable" name="xxxx" />
    <java-symbol type="attr" name="xxxx" />
    <java-symbol type="id" name="xxxx" />
    <java-symbol type="color" name="xxxx" />
    <java-symbol type="dimen" name="xxxx" />
    <java-symbol type="style" name="xxxx" />
</resources>

1.2.2. Add Public Resource

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <public type="attr" name="xxxx" />
</resources>

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <public type="attr" name="xxxx" id="0x01010000" />
</resources>

其中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-<PRODUCT_NAME>-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
# <uses-library> node in the AndroidManifest.xml file.
libraries=com.example.sample

# details for each library. format is:
# <library.name>=<name>.jar;<desc>
# where
# <library.name>: the name of the library defined in the property "libraries" above.
# <name>.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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容