1. 程式人生 > >Android外掛化架構設計之載入資原始檔

Android外掛化架構設計之載入資原始檔

開篇介紹

現在專案比較大 資源比較多,但是若希望動態來載入資原始檔,可以有以下幾種方式:
1. 通過下載資原始檔zip包然後解壓來載入
2. 通過外掛開發
本文通過外掛開發來實現載入外掛中的資原始檔.

程式演示

程式演示
也可以開啟連結 效果演示

開啟後顯示2個動畫,上面的動畫是載入的本地動畫,下面的動畫是從外掛裡面載入的。

程式碼介紹

如圖所示:
程式結構
工程app作為宿主程式,plugin作為外掛程式,資原始檔也在plugin裡面,需要實現的是啟動app來載入外掛plugin裡面的資原始檔

這裡實現思路大致畫一下,需要在主程式裡面載入外掛程式的資原始檔,我們需要拿到外掛程式裡面的Context, 獲取資原始檔也就是AssetManager,這裡需要用到java的反射機制
思路

這裡給出核心程式碼部分
PluginResources 類如下

package com.cayden.plugin;

import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import java.io.File;
import java.lang.reflect.Method;

/**
 * Created by cuiran
 * Time  17/3/14 17:37
 * Email 
[email protected]
* Description */
public class PluginResources extends Resources { public PluginResources(AssetManager assets, DisplayMetrics metrics, Configuration config) { super(assets, metrics, config); } public static PluginResources getPluginResources(Resources resources,AssetManager assets){ PluginResources pluginResources=new
PluginResources(assets,resources.getDisplayMetrics(),resources.getConfiguration()); return pluginResources; } public static AssetManager getPluginAssetManager(File apk) throws ClassNotFoundException{ //需要通過反射獲取AssetManager Class<?> forName= Class.forName("android.content.res.AssetManager"); Method[] methods= forName.getDeclaredMethods(); for(Method method:methods){ if(method.getName().equals("addAssetPath")){ try{ AssetManager assetManager=AssetManager.class.newInstance(); method.invoke(assetManager,apk.getAbsolutePath()); return assetManager; }catch (InstantiationException e){ e.printStackTrace(); }catch (Exception e){ e.printStackTrace(); } } } return null; } }

啟動MainActivity類如下

package com.cayden.plugin;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;

import dalvik.system.DexClassLoader;


public class MainActivity extends Activity implements View.OnClickListener{
    private static final String TAG=MainActivity.class.getSimpleName();
    private ImageView imageSource,imageCloud;
    private final static String SOURCE_TAG="source";
    private final static String CLOUD_TAG="cloud";
    private final static String CLOUD_ANIM="animation1";

    private final static String PLUGIN_NAME="plugin.apk";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();

    }

    private void initView(){
        imageSource=(ImageView)findViewById(R.id.imageSource);

        imageCloud=(ImageView)findViewById(R.id.imageCloud);

        imageSource.setTag(SOURCE_TAG);
        imageCloud.setTag(CLOUD_TAG);

        imageSource.setOnClickListener(this);
        imageCloud.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if(view.getTag().equals(SOURCE_TAG)){

            handleAnim(view);

        }else{

            //外掛動畫
            //是否載入
            String fileName=PLUGIN_NAME;
            String filePath=this.getCacheDir()+ File.separator+fileName;
            String packageName="com.cayden.pluginb";
            File apkFile=new File(filePath);
            if(apkFile.exists()){
               Drawable background= view.getBackground();
                if(background instanceof AnimationDrawable){
                    //執行動畫
                    handleAnim(view);
                }else{
                    //執行外掛
                    try{
                        AssetManager assetManager=PluginResources.getPluginAssetManager(apkFile);
                        PluginResources resources=  PluginResources.getPluginResources(getResources(),assetManager);

                        //反射檔案
                        DexClassLoader classLoader=new DexClassLoader(apkFile.getAbsolutePath(),this.getDir(fileName, Context.MODE_PRIVATE).getAbsolutePath(),null,this.getClassLoader());

                        Class<?> loadClass= classLoader.loadClass(packageName+".R$drawable");
                        Field[] fields=  loadClass.getDeclaredFields();
                        for(Field field :fields){
                            if(field.getName().equals(CLOUD_ANIM)){
                                int animId=field.getInt(R.drawable.class);
                                Drawable drawable=resources.getDrawable(animId);
                                ((ImageView) view).setBackgroundDrawable(drawable);
                                handleAnim(view);
                            }
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }else{
                //需要從服務端下載,測試就放入assets目錄下
                try{
                    InputStream is=this.getAssets().open(fileName);
                    FileOutputStream os=new FileOutputStream(filePath);
                    int len=0;
                    byte [] buffer=new byte[1024];
                    while((len=is.read(buffer))!=-1){
                        os.write(buffer,0,len);
                    }
                    os.close();
                    is.close();
                    Log.d(TAG,"file ok");
                    Toast.makeText(this,"file ok",Toast.LENGTH_SHORT).show();

                }catch (Exception e){
                    e.printStackTrace();
                }
            }



        }
    }

    private void handleAnim(View v){
        AnimationDrawable background=(AnimationDrawable)v.getBackground();
        if(background!=null){
            if(background.isRunning() ){
                background.stop();
            }else{
                background.stop();
                background.start();
            }
        }
    }

}

為了演示方便 我們把plugin.apk放在主程式app工程的assets目錄下。