首页 > 技术文章 > Android学习笔记_71_Android 多个项目之间如何引用 项目怎样打jar包

lbangel 2014-04-22 18:16 原文

一、将整个项目作为资源文件

1、需要将被应用的项目设置为库项目。

2、将该项目的配置文件中的四大组件清空,例如下面代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.baidu.cn"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
    </application>

</manifest>

3、将四大组件的内容移动到主项目的配置文件中,将assets目录 libs目录下的文件也要移动过去。
4、在被引用的项目作为库项目导入到主项目中,通过 " 右键-》属性-》android-》add "就可以导入。如果被导入的项目也依赖其他库项目,那么该项目不需要导入其他库项目,将所以的库项目放到主项目中去。

5、注意事项:

  5.1、库项目和主项目需要最好放在同一个工作空间下,不用从其他工作空间引用,否则上面显示的库项目有个红叉。

  5.2、库项目不能在有自己定义的Application,可以将数据保存到单例类里面。

  5.3、启动activity时,还是在库项目中启动,这是主项目只需要传递上下文对象和基本参数信息即可。就不会出现找不到activity的错误。

二、将项目打成jar包

  将项目1的src(后台源码)目录下的文件打成jar包,重新建立一个项目2,然后将刚打成的jar包拷贝到项目2libs目录下,将项目1所有的资源文件(比如drawable,values,layout等资源目录)拷贝到项目2对应的目录下,将项目2作为资源文件就可以引入到其他项目中了,这样的好处是不对外暴露源码。

  有两种方式完成后台代码与前端文件映射:

  1、通过反射 

public class MResource {
    private static final String TAG = "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) {
                Log.i(TAG, ": "+classes[i].getName());
                if (classes[i].getName().split("\\$")[1].equals(className)) {
                    desireClass = classes[i];
                    break;
                }
            }
            Log.i(TAG, ": "+desireClass.getName());
            if (desireClass != null)
                id = desireClass.getField(name).getInt(desireClass);
            Log.i(TAG, "ID : "+desireClass.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }
}

 

  2、简单映射

  2.1、需要将项目2的R文件映射

public class ResourceMap {
    public static final int getAnim_alpha_show=R.anim.alpha_show;
    public static final int getAnim_translate_left_out=R.anim.translate_left_out;
    public static final int getAnim_translate_right_in=R.anim.translate_right_in;
    public static final int getArray_payment_moneys=R.array.payment_moneys;
    public static final int getArray_payment_moneys_text=R.array.payment_moneys_text;
    public static final int getArray_payment_types=R.array.payment_types;
    public static final int getColor_backgroundcolor=R.color.backgroundcolor;
    public static final int getColor_black=R.color.black;
    public static final int getColor_blue=R.color.blue;
    public static final int getColor_daoju_color=R.color.daoju_color;
    public static final int getColor_gray=R.color.gray;
}

  2.2、将项目2引入到项目1中,将类似R.color.gray替换成getColor_gray。可以通过搜索R.color.然后将其替换成ResourceMap.getColor_就可以了,同理drawable,anim,id,string都是一样。将项目1下的src(只打包src目录下的文件)打成jar包。

  2.3、将项目2作为资源文件。

  2.4、将R文件转换成ResourceMap的代码:其中RFile.java文件就是将项目2(与项目1的R文件是相同的,除非在项目2中增加其他额外信息)中的R.java文件内容拷贝其中。需要新建一个Java Project。

package com.example.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;

public class ParseRFile {

    private static String filePath;
    
    public static void main(String[] args) throws Exception {
        
        //public static int getId_imageView1 = R.id.imageView1;
        
        File file = new File("src/RFile.java");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("public class ResourceMap {\n");

        BufferedReader reader = new BufferedReader(new FileReader(file));
        String lineStr = null;
        System.out.println("start...");
        String className = null;
        while ((lineStr = reader.readLine()) != null) {
            lineStr = lineStr.trim();
            if (lineStr.contains("public static final class")) {
                className = lineStr.split(" ")[4];
            }
            if (lineStr.contains("public static final int")) {
                String[] words  = lineStr.split(" ");
                String filed =words[4].split("=")[0];
                stringBuffer.append("\tpublic static final int get"+firstWordUpper(className)+"_"+filed+"="+"R."+className+"."+filed+";\n");
            }
     
        }
        filePath = file.getAbsolutePath();
        stringBuffer.append("}");
        reader.close();
        
        createFile(stringBuffer.toString());
        
        System.out.println(filePath);
        System.out.println("end ...");
        
         
    } 
    
    /**
     * 首字母大写
     * @param word
     * @return
     */
    public static String firstWordUpper(String word){
        String first = word.substring(0, 1).toUpperCase();
        String subStr = word.substring(1,word.length());
        return first + subStr;
    }
    /**
     * 创建资源文件
     * @param source
     */
    public static void createFile(String source){
        try {
            File file = new File("D:/project2/ZJava/src/ResourceMap.java");
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            FileOutputStream out = new FileOutputStream(file);
            out.write(source.getBytes(), 0, source.length());
            out.close();
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

 

 

 

 

推荐阅读