首页 > 代码库 > android ant 打包全过程 包含library,so,jar,混淆,签名

android ant 打包全过程 包含library,so,jar,混淆,签名

1.先来一个基础的配置文件(build.properties)

##最后生成目录
#relase.abs.dir=C:\\Users\\Administrator\\Desktop
#
##工程绝对路径
#storeapp.abs=F:\\ZhangLeRepository\\trunk\\Andorid\\StoreApp
#photoview.abs=F:\\ZhangLeRepository\\trunk\\Andorid\\PhotoView
#andbase.abs=F:\\ZhangLeRepository\\trunk\\Andorid\\AndBase
#xutils.abs=F:\\ZhangLeRepository\\trunk\\Andorid\\xutils
#
##SDK相关工具变量
#sdk.home=F:\\adt\\sdk
#sdk.platform=android-19
#jdk.home=F:\\java
#
#
##签名配置
#keystore.path=???
#keystore.storepass=???
#keystore.keypass=???
#keystore.alias=???

3.上bulid.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Build an Android project. -->
<project name="StoreApp" basedir="." default="release" >
    <!-- 通用属性 -->
    <property file="build.properties" />

    <!-- 主工程绝对路径 -->
    <property name="storeapp.abs.src" value=http://www.mamicode.com/"${storeapp.abs}/src" />>

3.其中有个buildconfig的工具,这里共享源码,到处为可运行的jar即可

/**
 *  2014-6-11   上午11:07:22
 *  Created By niexiaoqiang
 */

package com.xiaoqiang.genbuildconfig;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;

/**
 * 生成BuildConfig文件
 * @author niexiaoqiang
 */
public class GenBuildConfig {
	public static final String ABDIR = "abdir";
	public static final String PACKAGE = "pakage";
	public static final String DEBUG = "debug";
	public static final String FILENAME = "BuildConfig.java";

	public static void main(String[] args) {
		int paramsLength = args.length;
		if (paramsLength % 2 == 0) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			for (int i = 0; i < paramsLength; i = i + 2) {
				params.put(args[i], args[i + 1]);
			}
			String content = getBuildConfigContent(params);
			//创建文件夹
			File dir = new File(params.get(ABDIR).toString());
			if (!dir.exists()) {
				dir.mkdirs();
			}
			createFile(content, dir, params.get(PACKAGE).toString());
		}
	}

	/**
	 * 创建文件
	 * @param content
	 * @param dir
	 * @param packageStr
	 */
	public static void createFile(String content, File dir, String packageStr) {
		File javaFileDir = new File(dir, packageStr.replace(".", "\\"));
		if (!javaFileDir.exists()) {
			javaFileDir.mkdirs();
		}
		File javaFile = new File(javaFileDir, FILENAME);
		javaFile.setWritable(true);
		try {
			FileWriter writer = new FileWriter(javaFile, false);
			writer.write(content);
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成文件内容
	 * @param params
	 * @return
	 */
	public static String getBuildConfigContent(HashMap<String, Object> params) {
		StringBuilder sb = new StringBuilder();
		sb.append("/**\r\n");
		sb.append(" * Create By com.xiaoqiang.genbuildconfig\r\n");
		sb.append(" */\r\n");
		sb.append("\r\n");
		sb.append("package " + params.get(PACKAGE) + ";\r\n");
		sb.append("\r\n");
		sb.append("public final class BuildConfig {\r\n");
		sb.append("  public final static boolean DEBUG = " + params.get(DEBUG) + ";\r\n");
		sb.append("}");
		return sb.toString();
	}

}