Android 添加framework资源包

为Android系统添加一个新的资源包

概述

传统的Android系统只有一个framework-res.apk资源包,第三方厂商在进行rom定制时会直接修改framework res资源,达到适配目的。但是这种方式跟原生资源的耦合度过高,在系统迁移或者framework ui移植时需要进行人工merge,工作量巨大。通过为Android添加一个新的独立的资源包,可以将厂商定制资源独立出来,可移植、可维护性非常好。

实现

具体做法可以分为以下几个步骤:

1)修改build/core/clear_var.mk,添加一项:

1
LOCAL_USE_XOS_FRAMEWORK_RES:= true

2)修改build/core/package.mk

以下脚本可以加在$(R_file_stamp): $(framework_res_package_export_deps) 之前,然后修改$(R_file_stamp): $(framework_res_package_export_deps)$(LOCAL_INTERMEDIATE_TARGETS):
PRIVATE_AAPT_INCLUDES := $(framework_res_package_export)

1
2
3
4
5
6
7
8
9
ifeq ($(LOCAL_USE_XOS_FRAMEWORK_RES),true)
xos_framework_res_package_export :=
$(call intermediates-dir-for,APPS,xos-framework-res,,COMMON)/package-export.apk
xos_framework_res_package_export_deps :=
$(dir $(xos_framework_res_package_export))src/R.stamp
else
xos_framework_res_package_export :=
xos_framework_res_package_export_deps :=
endif
1
2
3
4
5
$(R_file_stamp): $(framework_res_package_export_deps) $(xos_framework_res_package_export_deps)
$(LOCAL_INTERMEDIATE_TARGETS):
PRIVATE_AAPT_INCLUDES := $(framework_res_package_export)
$(xos_framework_res_package_export)
endif # LOCAL_NO_STANDARD_LIBRARIES

3)修改build/target/product/core.mk

PRODUCT_PACKAGES增加xos-framework-res

4)添加一个新的资源包项目,这里在framework/base/core/res同级目录下建立一个新的文件夹xos_res用来存放我们的资源。把res目录下的AndroidManifest.xml和Android.mk拷贝过来,进行修改。

记得设置 LOCAL_USE_MY_FRAMEWORK_RES为false。

Android.mk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_PACKAGE_NAME := xos-framework-res
LOCAL_CERTIFICATE := platform

# Set LOCAL_USE_XOS_FRAMEWORK_RES as false
LOCAL_USE_XOS_FRAMEWORK_RES := false

# Tell aapt to create "extending (non-application)" resource IDs,
# since these resources will be used by many apps.
LOCAL_AAPT_FLAGS := -x

# Install this alongside the libraries.
LOCAL_MODULE_PATH := $(TARGET_OUT_JAVA_LIBRARIES)

# Create package-export.apk, which other packages can use to get
# PRODUCT-agnostic resource data like IDs and type definitions.
LOCAL_EXPORT_PACKAGE_RESOURCES := true

# Include resources generated by system RenderScript files.
framework_GENERATED_SOURCE_DIR := $(call intermediates-dir-for,JAVA_LIBRARIES,framework,,COMMON)/src
framework_RenderScript_STAMP_FILE := $(framework_GENERATED_SOURCE_DIR)/RenderScript.stamp
#LOCAL_RESOURCE_DIR := $(framework_GENERATED_SOURCE_DIR)/renderscript/res $(LOCAL_PATH)/res

include $(BUILD_PACKAGE)

# Make sure the system .rs files get compiled before building the package-export.apk.
#$(resource_export_package): $(framework_RenderScript_STAMP_FILE)

# define a global intermediate target that other module may depend on.

xos-framework-res-package-target: $(LOCAL_BUILT_MODULE)

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.c 大专栏  Android 添加framework资源包om/apk/res/android"
package="systems.xos.car.xsdk"
coreApp="true"
android:sharedUserId="android.uid.system"
android:sharedUserLabel="@null">

<application android:process="system"
android:persistent="true"
android:hasCode="false"
android:label="@null"
android:allowClearUserData="false"
android:killAfterRestore="false"
android:icon="@null">

</application>

</manifest>

资源的放置跟res下的类似,记得values目录下创建public.xml,public.xml对id的类型区分比较严格,attr必须是0x0x010000开头,drawable必须是0x0x020000开头,其他类型好像就没有限制,直接依次0x0x030000、0x0x040000开始即可。否则,编译过程中会出现segmentation fault错误。

1
2
3
4
5
6
7
8
<resources>
<public type="attr" name="cForeground" id="0x03010000" />
<public type="drawable" name="ic_filemanager" id="0x03020000" />
<public type="style" name="xosTheme" id="0x03030000" />
<public type="string" name="xos" id="0x03040000" />
<public type="dimen" name="xos_width" id="0x03050000" />
<public type="layout" name="xos_text" id="0x03060000" />
</resources>

5)以上只是解决了资源的编译环境问题,资源的查找过程也需要进行修改,修改 AssetManager.java,在init()后添加代码
addAssetPath("/system/framework/xos-framework-res.apk");

1
2
3
4
5
6
7
8
9
10
11
12
public () {
synchronized (this) {
if (DEBUG_REFS) {
mNumRefs = 0;
incRefsLocked(this.hashCode());
}
init();
addAssetPath("/system/framework/xos-framework-res.apk");
if (localLOGV) Log.v(TAG, "New asset manager: " + this);
ensureSystemAssets();
}
}

6)最后就是资源应用问题。应用程序在xml文件中引用xos-framework-res.apk中的资源时可以使用与原生资源类似的访问方式,首先声明xos xmlns :

xmlns:xos="http://schemas.android.com/apk/res/systems.xos.car.xsdk"
然后像@android:drawable @android:dimen这种引用方式改成@xos:drawable @xos:dimen即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xos="http://schemas.android.com/apk/res/systems.xos.car.xsdk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="@xos:dimen/xos_width"
android:orientation="horizontal" >

<ImageView android:id="@+id/file_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="40dp"
android:src="@xos:drawable/ic_filemanager" />

</LinearLayout>

XSDK 使用xos-res资源方式

Android.mk中引用资源路径:

1
2
3
framework_res_source_path := APPS/xos-framework-res_intermediates/src
LOCAL_INTERMEDIATE_SOURCES :=
$(framework_res_source_path)/systems/xos/car/xsdk/R.java

实际代码中使用:

1
2
3
4
5
6
import systems.xos.car.xsdk.R;

.
.
.
inflater.inflate(R.layout.menu_view, this, true);

跟在普通的Android项目中使用资源一样调用。

Android 添加framework资源包

上一篇:为Eclipse添加Java和Android SDK源代码


下一篇:Android 删除短信