android使用Eclipse打包jar文件

使用Eclipse 新建一個Android工程(正常建就可以)

然後在src下的某個包下,建一個包,比如叫utils包。

然後在utils下寫一個工具類。比如說以下代碼:

package util;

import java.util.List;

import android.text.TextUtils;
import android.util.Log;

public class LogUtil {
    /** 日誌輸出級別NONE */
    public static final int LEVEL_NONE = 0;
    /** 日誌輸出級別V */
    public static final int LEVEL_VERBOSE = 1;
    /** 日誌輸出級別D */
    public static final int LEVEL_DEBUG = 2;
    /** 日誌輸出級別I */
    public static final int LEVEL_INFO = 3;
    /** 日誌輸出級別W */
    public static final int LEVEL_WARN = 4;
    /** 日誌輸出級別E */
    public static final int LEVEL_ERROR = 5;

    /** 日誌輸出時的TAG */
    private static String mTag = "jiekuan";
    /** 是否允許輸出log */
    public static int mDebuggable = LEVEL_ERROR;

    /** 用於記時的變量 */
    private static long mTimestamp = 0;

    /** 以級別爲 d 的形式輸出LOG */
    public static void v(String msg) {
        if (mDebuggable >= LEVEL_VERBOSE) {
            if (!TextUtils.isEmpty(msg))
                Log.v(mTag, msg);
        }
    }

    /** 以級別爲 d 的形式輸出LOG */
    public static void d(String msg) {
        if (mDebuggable >= LEVEL_DEBUG) {
            if (!TextUtils.isEmpty(msg))
                Log.d(mTag, msg);
        }
    }

    /** 以級別爲 i 的形式輸出LOG */
    public static void i(String msg) {
        if (mDebuggable >= LEVEL_INFO) {
            if (!TextUtils.isEmpty(msg))
                Log.i(mTag, msg);
        }
    }

    /** 以級別爲 w 的形式輸出LOG */
    public static void w(String msg) {
        if (mDebuggable >= LEVEL_WARN) {
            if (!TextUtils.isEmpty(msg))
                Log.w(mTag, msg);
        }
    }

    /** 以級別爲 w 的形式輸出Throwable */
    public static void w(Throwable tr) {
        if (mDebuggable >= LEVEL_WARN) {
            Log.w(mTag, "", tr);
        }
    }

    /** 以級別爲 w 的形式輸出LOG信息和Throwable */
    public static void w(String msg, Throwable tr) {
        if (mDebuggable >= LEVEL_WARN && null != msg) {
            Log.w(mTag, msg, tr);
        }
    }

    /** 以級別爲 e 的形式輸出LOG */
    public static void e(String msg) {
        if (mDebuggable >= LEVEL_ERROR) {
            if (!TextUtils.isEmpty(msg))
                Log.e(mTag, msg);
        }
    }

    /** 以級別爲 e 的形式輸出Throwable */
    public static void e(Throwable tr) {
        if (mDebuggable >= LEVEL_ERROR) {
            Log.e(mTag, "", tr);
        }
    }

    /** 以級別爲 e 的形式輸出LOG信息和Throwable */
    public static void e(String msg, Throwable tr) {
        if (mDebuggable >= LEVEL_ERROR && null != msg) {
            Log.e(mTag, msg, tr);
        }
    }

    /**
     * 以級別爲 e 的形式輸出msg信息,附帶時間戳,用於輸出一個時間段起始點
     * @param msg 需要輸出的msg
     */
    public static void msgStartTime(String msg) {
        mTimestamp = System.currentTimeMillis();
        if (!TextUtils.isEmpty(msg)) {
            e("[Started:" + mTimestamp + "]" + msg);
        }
    }

    /** 以級別爲 e 的形式輸出msg信息,附帶時間戳,用於輸出一個時間段結束點*
     *  @param msg 需要輸出的msg
     */
    public static void elapsed(String msg) {
        long currentTime = System.currentTimeMillis();
        long elapsedTime = currentTime - mTimestamp;
        mTimestamp = currentTime;
        e("[Elapsed:" + elapsedTime + "]" + msg);
    }

    /**
     * 打印List集合
     * @param list
     */
    public static <T> void printList(List<T> list) {
        if (list == null || list.size() < 1) {
            return;
        }
        int size = list.size();
        i("---begin---");
        for (int i = 0; i < size; i++) {
            i(i + ":" + list.get(i).toString());
        }
        i("---end---");
    }

    /**
     * 打印數組
     * @param array
     */
    public static <T> void printArray(T[] array) {
        if (array == null || array.length < 1) {
            return;
        }
        int length = array.length;
        i("---begin---");
        for (int i = 0; i < length; i++) {
            i(i + ":" + array[i].toString());
        }
        i("---end---");
    }
}

然後保存此LogUtil.java這個文件。

在Eclipse菜單欄裏選擇File -> Export...java目錄下的jar file,如下圖:



然後選中,和取消選中以下內容:



然後,finish即可,然後到你的目錄下:D:\下找到剛纔導出的名爲library.jar即可。


然後在Android studio裏即可引入此jar文件:(注意我改了一下jar包的名字)





然後可以編譯和運行試下:




OK。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章