Android將Activity打成jar包供第三方調用(解決資源文件不能打包的問題)
最近有一個需要,我們公司做了一個apk客戶端,然後其他的公司可以根據自己的需要來替換裏麵的資源圖片,文字等一些資源文件問題,我本來想這個簡單,用兩個工程直接替換裏麵的資源文件就行,老大說,這樣子不好,如果要改需要改兩個客戶端,而且還麻煩,叫我將所有的Activity打成Jar包的形式,這樣子我們改了裏麵的內容就直接發布Jar包出去,其他公司直接下載Jar來使用,這樣子他們自己公司也能更好的維護。
所以我就想直接將Activity打成Jar包,可是在使用的過程中發現這樣子根本行不通,因為如果Activity引用了布局文件的話,比如R.layout.XXX或者R.string.XXX,我們使用的時候會報資源ID未找到的異常,在官網上看到可以將另一個工程當做Libraryhttps://developer.android.com/tools/projects/projects-eclipse.html,可是這樣子需要將源碼給到人家,不能直接發布Jar包,貌似不是我要的那種情況,今天我教大家如果將Activity打成Jar包的形式
1.我們新建一個Android工程,取名為ActivityLibrary,這個就是等下我們需要打包成Jar的工程
注:MResource這個類很重要,主要是它的作用,利用反射根據資源名字獲取資源ID(其實係統也自帶了根據資源名字獲取資源ID的方法getResources().getIdentifier("main_activity", "layout", getPackageName());第一個參數是資源的名字,第二個參數是資源的類型,例如layout, string等,第三個是包名字)
- package com.example.activitylibrary;
- import android.content.Context;
- /**
- * 根據資源的名字獲取其ID值
- * @author mining
- *
- */
- public class MResource {
- public static int getIdByName(Context context, String className, String name) {
- String packageName = context.getPackageName();
- Class r = null;
- int id = 0;
- try {
- r = Class.forName(packageName + ".R");
- Class[] classes = r.getClasses();
- Class desireClass = null;
- for (int i = 0; i < classes.length; ++i) {
- if (classes[i].getName().split("\\$")[1].equals(className)) {
- desireClass = classes[i];
- break;
- }
- }
- if (desireClass != null)
- id = desireClass.getField(name).getInt(desireClass);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (NoSuchFieldException e) {
- e.printStackTrace();
- }
- return id;
- }
- }
當我們的資源Id是一個數組的時候,我們要用下麵的方法
- public static int[] getIdsByName(Context context, String className, String name) {
- String packageName = context.getPackageName();
- Class r = null;
- int[] ids = null;
- try {
- r = Class.forName(packageName + ".R");
- Class[] classes = r.getClasses();
- Class desireClass = null;
- for (int i = 0; i < classes.length; ++i) {
- if (classes[i].getName().split("\\$")[1].equals(className)) {
- desireClass = classes[i];
- break;
- }
- }
- if ((desireClass != null) && (desireClass.getField(name).get(desireClass) != null) && (desireClass.getField(name).get(desireClass).getClass().isArray()))
- ids = (int[])desireClass.getField(name).get(desireClass);
- }
- catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (NoSuchFieldException e) {
- e.printStackTrace();
- }
- return ids;
- }
LibraryActivity這裏麵比較簡單,一個Button,一個TextView,一個ImageView
- package com.example.activitylibrary;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class LibraryActivity extends Activity {
- String msg = "我是來自Jar中的Activity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(MResource.getIdByName(getApplication(), "layout", "activity_main"));
- TextView mTextView = (TextView) findViewById(MResource.getIdByName(getApplication(), "id", "textView1"));
- mTextView.setText(msg);
- Button mButton = (Button) findViewById(MResource.getIdByName(getApplication(), "id", "button1"));
- mButton.setText(msg);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- xmlns:tools="https://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/button1" />
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/textView1"
- android:layout_marginTop="28dp"
- android:src="@drawable/ic_launcher" />
- </RelativeLayout>
隻勾選src目錄,其他的都不勾選,如圖
通過上麵這幾步我們就將Android工程打包好了
3.我們來使用剛剛打包好的Activity,我們還需要剛剛那個工程的資源文件,因為我們剛剛隻打包了src,資源文件不能打包,因此我們需要自己拿出來,我們需要吧Library.jar加入到libs裏麵去,然後用到的資源文件,如果layout,string之類的拷貝到對應工程的地方去
這個工程一個MainActivity,裏麵一個按鈕,點擊按鈕跳轉到Library中的Activity中,比較簡單我直接把代碼貼上
- package com.example.androidlibraryinvoke;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button mButton = (Button) findViewById(R.id.button1);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setClassName(getApplication(), "com.example.activitylibrary.LibraryActivity");
- startActivity(intent);
- }
- });
- }
- }
代碼下載
最後更新:2017-04-03 12:55:36