當前位置:編程學習大全網 - 源碼下載 - 如何為Android系統添加壹個新的資源包

如何為Android系統添加壹個新的資源包

如何為Android系統添加壹個新的資源包

傳統的Android系統只有壹個framework-res.apk資源包,第三方廠商在進行rom定制時會直接修改framework res資源,達到美化目的。但是這種方式跟原生資源的耦合度過高,在系統遷移或者framework ui移植時需要進行人工merge,工作量巨大。通過為Android添加壹個新的獨立的資源包,可以將廠商定制資源獨立出來,可移植、可維護性非常好。

具體做法可以分為以下幾個步驟:

1. 修改build/core/clear_var.mk

LOCAL_USE_LETV_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)

如下所示:

view sourceprint?

1 ifeq ($(LOCAL_USE_LETV_FRAMEWORK_RES),true)

2 letv_framework_res_package_export := \

3 $(call intermediates-dir-for,APPS,letv-framework-res,,COMMON)/package-export.apk

4 letv_framework_res_package_export_deps := \

5 $(dir $(letv_framework_res_package_export))src/R.stamp

6 else

7 letv_framework_res_package_export :=

8 letv_framework_res_package_export_deps :=

9 endif # LOCAL_USE_LETV_FRAMEWORK_RES

view sourceprint?

1 $(R_file_stamp): $(framework_res_package_export_deps) $(letv_framework_res_package_export_deps)

2 $(LOCAL_INTERMEDIATE_TARGETS): \

3 PRIVATE_AAPT_INCLUDES := $(framework_res_package_export) \

4 $(letv_framework_res_package_export)

5 endif # LOCAL_NO_STANDARD_LIBRARIES

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

在PRODUCT_PACKAGES增加letv-framework-res

4. 添加壹個新的資源包項目,這裏在framework/base/core/res同級目錄下建立壹個新的文件夾letv_res用來存放我們的資源。把res目錄下的AndroidManifest.xml和Android.mk拷貝過來,進行修改。

記得設置LOCAL_USE_MY_FRAMEWORK_RES為false。

view sourceprint?

01 LOCAL_PATH:= $(call my-dir)

02 include $(CLEAR_VARS)

03

04 LOCAL_MODULE_TAGS := optional

05

06 LOCAL_PACKAGE_NAME := letv-framework-res

07 LOCAL_CERTIFICATE := platform

08

09 # Set LOCAL_USE_LETV_FRAMEWORK_RES as false

10 LOCAL_USE_LETV_FRAMEWORK_RES := false

11

12 # Tell aapt to create "extending (non-application)" resource IDs,

13 # since these resources will be used by many apps.

14 LOCAL_AAPT_FLAGS := -x

15

16 # Install this alongside the libraries.

17 LOCAL_MODULE_PATH := $(TARGET_OUT_JAVA_LIBRARIES)

18

19 # Create package-export.apk, which other packages can use to get

20 # PRODUCT-agnostic resource data like IDs and type definitions.

21 LOCAL_EXPORT_PACKAGE_RESOURCES := true

22

23 # Include resources generated by system RenderScript files.

24 framework_GENERATED_SOURCE_DIR := $(call intermediates-dir-for,JAVA_LIBRARIES,framework,,COMMON)/src

25 framework_RenderScript_STAMP_FILE := $(framework_GENERATED_SOURCE_DIR)/RenderScript.stamp

26 #LOCAL_RESOURCE_DIR := $(framework_GENERATED_SOURCE_DIR)/renderscript/res $(LOCAL_PATH)/res

27

28 include $(BUILD_PACKAGE)

29

30 # Make sure the system .rs files get compiled before building the package-export.apk.

31 #$(resource_export_package): $(framework_RenderScript_STAMP_FILE)

32

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

34 .PHONY: letv-framework-res-package-target

35 letv-framework-res-package-target: $(LOCAL_BUILT_MODULE)

view sourceprint?

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

02 <manifest xmlns:android=""

03 package="letv" coreApp="true" android:sharedUserId="android.uid.system"

04 android:sharedUserLabel="@null">

05

06 <application android:process="system"

07 android:persistent="true"

08 android:hasCode="false"

09 android:label="@null"

10 android:allowClearUserData="false"

11 android:killAfterRestore="false"

12 android:icon="@null">

13

14 </application>

15

16 </manifest>

資源的放置跟res下的類似,記得values目錄下創建public.xml,public.xml對id的類型區分比較嚴格,attr必須是0x0x010000開頭,drawable必須是0x0x020000開頭,其他類型好像就沒有限制,直接依次0x0x030000、0x0x040000開始即可。否則,編譯過程中會出現segmentation fault錯誤。

view sourceprint?

1 <resources>

2 <public type="attr" name="cForeground" id="0x03010000" />

3 <public type="drawable" name="ic_filemanager" id="0x03020000" />

4 <public type="style" name="LetvTheme" id="0x03030000" />

5 <public type="string" name="letv" id="0x03040000" />

6 <public type="dimen" name="letv_width" id="0x03050000" />

7 <public type="layout" name="letv_text" id="0x03060000" />

8 </resources>

5. 以上只是解決了資源的編譯環境問題,資源的查找過程也需要進行修改,修改 AssetManager.java,在init()後添加代碼 addAssetPath("/system/framework/letv-framework-res.apk");

view sourceprint?

01 public AssetManager() {

02 synchronized (this) {

03 if (DEBUG_REFS) {

04 mNumRefs = 0;

05 incRefsLocked(this.hashCode());

06 }

07 init();

08 addAssetPath("/system/framework/letv-framework-res.apk");

09 if (localLOGV) Log.v(TAG, "New asset manager: " + this);

10 ensureSystemAssets();

11 }

12 }

6. 最後就是資源應用問題。應用程序在xml文件中引用letv-framework-res.apk中的資源時可以使用與原生資源類似的訪問方式,首先聲明letv xmlns :

view sourceprint?

1 xmlns:letv=""

然後像@android:drawable @android:dimen這種引用方式改成@letv:drawable @letv:dimen即可。

view sourceprint?

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

02 <LinearLayout

03 xmlns:android=""

04 xmlns:letv=""

05 android:layout_width="fill_parent"

06 android:layout_height="wrap_content"

07 android:gravity="center"

08 android:paddingTop="@letv:dimen/letv_width"

09 android:orientation="horizontal" >

10

11 <ImageView android:id="@+id/file_icon"

12 android:layout_width="wrap_content"

13 android:layout_height="wrap_content"

14 android:layout_gravity="center"

15 android:layout_marginBottom="40dp"

16 android:src="@letv:drawable/ic_filemanager" />

17

18 </LinearLayout>

轉載,僅供參考。

  • 上一篇:vue3 在哪些方便做了性能提升?
  • 下一篇:unity目前可跨越什麽
  • copyright 2024編程學習大全網