网站建设资讯

NEWS

网站建设资讯

android的ant,“Android”

如何将android原生应用改成ant方式编译

android 程序打包成apk,如果在是命令行方式,一般都要经过如下步骤:

公司主营业务:成都网站建设、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出宣威免费做网站回馈大家。

1.用aapt命令生成R.java文件

2.用aidl命令生成相应java文件

3.用javac命令编译java源文件生成class文件

4.用dx.bat将class文件转换成classes.dex文件

5.用aapt命令生成资源包文件resources.ap_

6.用apkbuilder.bat打包资源和classes.dex文件,生成unsigned.apk

7.用jarsinger命令对apk认证,生成signed.apk

eclipase手动打包生成apk方式,只不过是eclipase代替我们执行了以上命令而已。

eclipse用起来虽然方便,为什么要使用Ant批量打包Android应用,对于我来说主要有以下两方面考虑:

1、我们在发布App的时候,可能需要发送到十几,甚至几十个不同的分发渠道,比如360手机市场,百度,应用宝等等,我们可能需要对各个渠道的下载量,用户存留和用户使用情况等数据进行分析,比如使用百度移动统计,友盟统计等。为了实现统计功能,我们需要在配置文件中添加一个数据元,来标识我们的应用要发布到哪一个渠道上,因此,若使用传统的方法,我们每发布一个渠道的版本,就需要修改清单文件中的数据元,然后再使用keystore进行签名和打包。若只有一两个分发渠道,工作量还是可以接受的,但是若我们的分发渠道打到几十个的时候,我们如果再手动的进行修改然后签名打包发布,那工作量就很可观了。因此,为解决这种需求,我们采用Ant来实现对Android应用的自动打包。

2、我们做产品的时候,肯定需要经常打不同环境的包,比如开发环境,测试环境,生产环境,这个时候你怎么办,如果用传统方法,你打开发环境包你要把你的服务端IP和图片服务器IP改成开发的,打包,然后打测试的包,你又要改成测试服务器IP和图片服务器IP,这样多麻烦,如果你把这服务端IP和图片服务器IP,配置到一个xml文件里,用ant打包方式实现自动替换,多方便。

使用ant脚本打包android项目,包含library项目,如何设置依赖项目?

设置方法如下:

1.生成R.java类文件:

Eclipse中会自动生成R.java,ant和命令行使用android SDK提供的aapt.ext程序生成R.java;

2.将.aidl文件生成.java类文件:

Eclipse中自动生成,ant和命令行使用android SDK提供的aidl.exe生成.java文件;

3.编译.java类文件生成class文件:

Eclipse中自动生成,ant和命令行使用jdk的javac编译java类文件生成class文件;

4.将class文件打包生成classes.dex文件:

Eclipse中自动生成,ant和命令行使用android SDK提供的dx.bat命令行脚本生成classes.dex文件;

5.打包资源文件:

Eclipse中自动生成,ant和命令行使用Android SDK提供的aapt.exe生成资源包文件;

6.生成未签名的apk安装文件:

Eclipse中自动生成debug签名文件存放在bin目录中,ant和命令行使用android SDK提供的apkbuilder.bat命令脚本生成未签名的apk安装文件;

7.对未签名的apk进行签名生成签名后的android文件就可以了。

怎样用Ant编译Android工程?

对于Android工程,在sdk中其实已经写好了一个Ant编译build配置模板,这样就使得在使用ant编译Android工程成为了一件很轻松的事情。

具体该怎样做呢?

整体来讲分为三大步:

1.准备环境,配置环境变量

2.更新工程,自动生成Ant编译配置文件

3.编译工程

如何使用Ant自动构建Android版本

技术分享:如何使用Ant自动构建Android版本

我来答

技术分享:如何使用Ant自动构建Android版本

我来答

热心网友

2018-07-23

Ant是一个很好的跨平台构建工具,特别是对于Java项目,这里使用它对Android工程进行自动化构建可以得到非常大的便利。一般来说对Android工程进行构建主要有以下几个步骤:

使用aapt来编译工程生成R.java;

使用aapt来对工程所需用的资源进行打包;

转换并编译AIDL文件;

编译Java源文件并生成.class;

转换并压缩.class文件成.dex文件;

使用aapt打包生成未签名的apk包;

使用jarsigner对apk包签名并得到最终的发布包。

android如何使用ant批量打包

ps :后期熟悉ant的话,可以使用纯ant脚本或者使用另一种更好的自动化打包工具(maven)关键代码如下:package com.cn.ant; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.Project; import org.apache.tools.ant.ProjectHelper; public class AntTest { private Project project; public void init(String _buildFile, String _baseDir) throws Exception { project = new Project(); project.init(); DefaultLogger consoleLogger = new DefaultLogger(); consoleLogger.setErrorPrintStream(System.err); consoleLogger.setOutputPrintStream(System.out); consoleLogger.setMessageOutputLevel(Project.MSG_INFO); project.addBuildListener(consoleLogger); // Set the base directory. If none is given, "." is used. if (_baseDir == null) _baseDir = new String("."); project.setBasedir(_baseDir); if (_buildFile == null) _buildFile = new String(projectBasePath + File.separator + "build.xml"); //ProjectHelper.getProjectHelper().parse(project, new File(_buildFile)); // 关键代码 ProjectHelper.configureProject(project, new File(_buildFile)); } public void runTarget(String _target) throws Exception { // Test if the project exists if (project == null) throw new Exception( "No target can be launched because the project has not been initialized. Please call the 'init' method first !"); // If no target is specified, run the default one. if (_target == null) _target = project.getDefaultTarget(); // Run the target project.executeTarget(_target); } // private final static ArrayListString flagList = new ArrayListString(); //也可以使用集合,不过需要手动添加项 private final static String[] flagList = new String[]{"htc","moto","oppo"};//此处初始化市场标识 private final static String projectBasePath = ""; //项目的根目录,需要配置 private final static String copyApkPath = ""; //要改名后拷贝的目录,需要配置 private final static String placeHolder = ""; //占位符,需要配置 public static void main(String args[]) { //flagList.add("htc"); //flagList.add("moto"); //flagList.add("oppo"); try { System.out.println("---------ant批量自动化打包开始----------"); for(String flag : flagList){ //先修改manifest文件:读取临时文件中的@market@修改为市场标识,然后写入manifest.xml中 String tempFilePath = projectBasePath + File.separator + "AndroidManifest.xml.temp"; String filePath = projectBasePath + File.separator + "AndroidManifest.xml"; write(filePath,read(tempFilePath, flag)); //执行打包命令 AntTest mytest = new AntTest(); mytest.init( projectBasePath + File.separator + "build.xml", projectBasePath); mytest.runTarget("clean"); mytest.runTarget("release"); //打完包后执行重命名加拷贝操作 //String flag = "htc"; File file = new File(projectBasePath + File.separator +"bin"+File.pathSeparator+"MainActivity-release.apk");//bin目录下签名的apk文件 file.renameTo(new File(copyApkPath + File.separator + "MainActivity_"+flag+".apk")); } System.out.println("---------ant批量自动化打包结束----------"); } catch (Exception e) { e.printStackTrace(); System.out.println("---------ant批量自动化打包中发生异常----------"); } } public static String read(String filePath,String replaceStr) { BufferedReader br = null; String line = null; StringBuffer buf = new StringBuffer(); try { // 根据文件路径创建缓冲输入流 br = new BufferedReader(new FileReader(filePath)); // 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中 while ((line = br.readLine()) != null) { // 此处根据实际需要修改某些行的内容 if (line.contains(placeHolder)) { line = line.replace(placeHolder, replaceStr); buf.append(line); } else { buf.append(line); } buf.append(System.getProperty("line.separator")); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭流 if (br != null) { try { br.close(); } catch (IOException e) { br = null; } } } return buf.toString(); } /** * 将内容回写到文件中 * * @param filePath * @param content */ public static void write(String filePath, String content) { BufferedWriter bw = null; try { // 根据文件路径创建缓冲输出流 bw = new BufferedWriter(new FileWriter(filePath)); // 将内容写入文件中 bw.write(content); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭流 if (bw != null) { try { bw.close(); } catch (IOException e) { bw = null; } } } } }

android 如何优化ant 批量打包

1.配置JAVA的环境变量

很多Java程序员由于使用Eclipse不配置Java环境变量也能正常运行代码。但是如果想使用Ant命令批量打包本步骤必不可少。

2. 下载Ant(这里的Ant不是eclipse和android SDk里面自带的ant)

3. 解压Ant并配置环境变量

a) 解压Ant,比如解压到D:\ant

b) 我的电脑-属性-高级-环境变量

c) 系统变量新建ANT_HOME,变量值为d:\ant

d) 系统变量新建或修改PATH:将%ANT_HOME%\bin;%ANT_HOME%\lib添加到环境变量的PATH中 (注意以上

路径均用反斜杠)

4. 验证ant配置是否正确

在控制台输入Cmd 回车, ant 回车,如果出现:

Buildfile: build.xml does not exist!

Build failed

恭喜你已经ant配置成功!!

Ant批量打包的基本思想是,每次打包后自动替换渠道号,然后再次打包从而实现多渠道打包的目的。

但是Ant不支持循环,怎样循环打包? 扩展包Ant-contrib能轻松解决这个问题.可以翻墙的1.0b3.jar放到Ant的lib文件夹即可.

5. 编写build.xml

将以下带有颜色的字体(包括路径,项目名称)都改成正确的名称

?xml version="1.0" encoding="UTF-8"?

project name="ThumbPlay" default="help"

!-- The local.properties file is created and updated by the 'android' tool.

It contains the path to the SDK. It should *NOT* be checked into

Version Control Systems. --

property file="local.properties" /

!-- The ant.properties file can be created by you. It is only edited by the

'android' tool to add properties to it.

This is the place to change some Ant specific build properties.

Here are some properties you may want to change/update:

source.dir

The name of the source directory. Default is 'src'.

out.dir

The name of the output directory. Default is 'bin'.

For other overridable properties, look at the beginning of the rules

files in the SDK, at tools/ant/build.xml

Properties related to the SDK location or the project target should

be updated using the 'android' tool with the 'update' action.

This file is an integral part of the build system for your

application and should be checked into Version Control Systems.

--

property file="ant.properties" /

!-- The project.properties file is created and updated by the 'android'

tool, as well as ADT.

This contains project specific properties such as project target, and library

dependencies. Lower level build properties are stored in ant.properties

(or in .classpath for Eclipse projects).

This file is an integral part of the build system for your

application and should be checked into Version Control Systems. --

loadproperties srcFile="project.properties" /

!-- quick check on sdk.dir --

fail

message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"

unless="sdk.dir"

/

!-- extension targets. Uncomment the ones where you want to do custom work

in between standard targets --

!--

target name="-pre-build"

/target

target name="-pre-compile"

/target

target name="-post-compile"

/target

--

!-- Import the actual build file.

To customize existing targets, there are two options:

- Customize only one target:

- copy/paste the target into this file, *before* the

import task.

- customize it to your needs.

- Customize the whole content of build.xml

- copy/paste the content of the rules files (minus the top node)

into this file, replacing the import task.

- customize to your needs.

***********************

****** IMPORTANT ******

***********************

In all cases you must update the value of version-tag below to read 'custom' instead of an integer,

in order to avoid having your file be overridden by tools such as "android update project"

--

!-- version-tag: 1 --

taskdef resource="net/sf/antcontrib/antcontrib.properties"

classpath

pathelement location="D:/androidDev/batch-package-tool/ant1.8.3/lib/ant-contrib-1.0b3.jar"/

/classpath

/taskdef

import file="${sdk.dir}/tools/ant/build.xml" /

target name="deploy"

foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=","

/foreach

/target

target name="modify_manifest"

!--replaceregexp file="AndroidManifest.xml" encoding="utf-8" match="android:value="(.*)"" replace=""/--

replaceregexp flags="g" byline="false"

regexp pattern="android:name="UMENG_CHANNEL" android:value="(.*)"" /

substitution expression="android:name="UMENG_CHANNEL" android:value="${channel}"" /

fileset dir="" includes="AndroidManifest.xml" /

/replaceregexp

!--property name="out.release.file" value="${out.absolute.dir}/${channel}.apk"/--

antcall target="release"/

copy tofile="${gos.path}/ThumbPlay_${channel}.apk"

fileset dir="${out.absolute.dir}/" includes="ThumbPlay-release.apk" /

/copy

delete includeEmptyDirs="true"

fileset dir="${out.absolute.dir}" includes="**/*"/

/delete

echo message="==========================="/

/target

/project

6. 配置local.properties

sdk.dir=D:\\androidDev\\android-sdk 改成你的SDK所在的目录,注意转义字符

7. 配置ant.properties

# the config file for batch package.

application.package=com.leyou.thumb (你的apk文件的包名)

ant.project.name=ThumbPlay (你的apk文件的工程名)

java.encoding=utf-8

out.absolute.dir=C:/compile

gos.path=Z:/app-version/test (打好的渠道包要放到的目的位置)

key.store=D:/androidApk/thumbplay/thumbplay.keystore (keystore文件路径)

key.store.password=wushenshiji999 (keystore文件路径)

key.alias=muzhigame (keystore文件别名)

key.alias.password=wushenshiji999 (keystore文件别名密码)

app_version=1.0.4 (要打的渠道包的版本名称)

market_channels=guanwang,shuihu,wushen,shenhua,huawei (渠道名称,要以逗号分隔,必须在一行内)


网页标题:android的ant,“Android”
当前链接:http://cdweb.net/article/dsdspgs.html