Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

動手實作:用獨立APK皮膚包為Android應用換膚

編輯:關於 Android 程式設計

幾乎每個Android專案走到後期都會撞上同一道難題:功能要持續擴充,卻又不想頻繁改動主程式碼。這背後考驗的其實是程式的靈活度。受Linux平台的安全機制與Dalvik虛擬機的特殊設計影響,再加上層層權限隔閡,想在行動平台上打造一套像PC那樣自由擴充的架構,難度高出不少。

其實這類問題在传统軟體領域早有成熟解法——外掛(Plugin)機制。從瀏覽器到我們天天在用的Eclipse,再到許多優秀的桌面軟體,都靠外掛實現功能擴充:升級時只需替換對應外掛,而不必重新打包整個應用,系統耦合度因此大幅降低。

把這個思路搬到Android上,做法就是將一個龐大的APK拆解成「主程式APK + 多個小型APK」的組合。

      

手機QQ的換膚正是典型案例:

QQ的皮膚本身就是一個沒有界面的APK,其中的資源命名與主程式完全一致;透過主程式與皮膚程式共享進程,主程式得以存取皮膚包內的資源,執行時再由程式碼明確指定要載入哪一套皮膚。缺點是每個Activity都得額外寫入判斷使用哪種皮膚的邏輯。

      

本範例的最終效果如下:

接下來拆解具體思路。

       在Android裡,預設情況下每個APK都是彼此獨立的:基本上每個應用跑在自己的Dalvik虛擬機上,各自擁有一個UID,再搭配Linux本身的權限機制,讓APK之間很難直接互通。但如果把多個APK當作同一個應用來整合,無論數量多少,它們都能併入同一個Dalvik虛擬機——最直觀的驗證方式,就是在shell下列出進程,會發現幾個APK同時載入後只存在一個進程。

         要達到這個效果,可以在清單檔中加入以下設定:

 android:sharedUserId="com.tony.test" 

      
         android:sharedUserId的作用是共用同一個UID,也就是說,凡是這個屬性值相同的工程都會共用UID,權限隔閡於是被打破,Dalvik也會融合為一個。不妨親手做個實驗:寫幾個工程,分別在「有這個屬性」與「沒有這個屬性」的情況下同時執行,再列出當前進程,對比結果一目了然。

        
下面用程式碼來說明,整個範例分為兩部分:主程式Re_Skin與皮膚程式Re_Skin1。

先來做主程式的程式碼:

1. 清單檔AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.tony.reskin" android:versionCode="1" android:versionName="1.0" <span >android:sharedUserId="com.tony.skin"</span>> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="com.tony.reskin.Re_SkinActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

2. 版面配置檔:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="Set" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> 

3. Re_SkinActivity(換膚核心邏輯所在的類別);

 package com.tony.reskin; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class Re_SkinActivity extends Activity { private LinearLayout layout; private Button btnSet; <span >private Context friendContext;</span> /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSet = (Button)findViewById(R.id.button1); layout = (LinearLayout)findViewById(R.id.layout); layout.setBackgroundResource(R.drawable.bg); try { <span >friendContext = createPackageContext("com.tony.reskin1", Context.CONTEXT_IGNORE_SECURITY);</span> } catch (NameNotFoundException e) { e.printStackTrace(); } btnSet.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Handler().post(new Runnable() { @Override public void run() { layout.setBackgroundDrawable(<span >friendContext.getResources().getDrawable(R.drawable.bg</span>)); } }); } }); } } 

皮膚應用本身不需要任何界面。

只要皮膚應用裡的資源命名與主程式保持一致即可。

清單檔:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.tony.reskin1" android:versionCode="1" android:versionName="1.0" <span >android:sharedUserId="com.tony.skin"</span>> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Re_Skin1Activity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved