Android Ant 批量多渠道打包实例

2024-04-06 04:08

本文主要是介绍Android Ant 批量多渠道打包实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android Ant 批量多渠道打包实例


关于批量打包,无需多言,这是每个国内Android开发者必须面对的一个问题。

下面,我就以开源项目「知乎小报」为例,详细说明如何使用ANT实现批量打渠道包。

1 Ant 安装

  • 下载ANT

请前往 http://ant.apache.org 下载。

  • 配置环境变量

设置环境变量后,在命令行下测试ant命令,如果出现以下内容,则说明配置成功:

cundongdeMacBook-Pro:~ cundong$ ant
Buildfile: build.xml does not exist!
Build failed
  • ant-contrib-1.0b3.jar下载

由于ant本身不支持迭代,因此我们需要用到一个第三方的库 ant-contrib来实现迭代功能。

下载ant-contrib,并将ant-contrib-1.0b3.jar文件拷贝至ANT安装目录。

下载地址:http://ant-contrib.sourceforge.net/

2 生成local.properties、build.xml文件

先介绍一下iZhihuPaper的工程依赖情况。

  • iZhihuPaper 依赖 actionbarpulltorefresh.extras.actionbarsherlock、Crouton、PhotoView

  • actionbarpulltorefresh.extras.actionbarsherlock 依赖 ActionBarPullToRefresh

  • ActionBarPullToRefresh 依赖 actionbarsherlock、SmoothProgressBar

  • Crouton、actionbarsherlock 依赖 SupportLib

  • PhotoView、SmoothProgressBar、SupportLib 无任何依赖

生成方式

我们需要为iZhihuPaper工程和他直接或者间接引用的所有工程(一共7个)都生成local.properties、build.xml文件。

命令格式:

android update project --target {target版本} --name {工程名字} --path {工程目录}

依次执行以下命令:

1.

android update project --target android-20 --name SupportLib --path /Users/cundong/Documents/github/SupportLib

2.

android update project --target android-20 --name PhotoView --path /Users/cundong/Documents/github/PhotoView

3.

android update project --target android-20 --name SmoothProgressBar --path /Users/cundong/Documents/github/SmoothProgressBar

4.

android update project --target android-20 --name Crouton --path /Users/cundong/Documents/github/Crouton

5.

android update project --target android-20 --name actionbarsherlock --path /Users/cundong/Documents/github/actionbarsherlock

6.

android update project --target android-20 --name ActionBarPullToRefresh --path /Users/cundong/Documents/github/ActionBarPullToRefresh

7.

android update project --target android-20 --name actionbarpulltorefresh.extras.actionbarsherlock --path /Users/cundong/Documents/github/actionbarpulltorefresh.extras.actionbarsherlock

8.

android update project --target android-20 --name iZhihuPaper --path /Users/cundong/Documents/github/iZhihuPaper

注意事项

  • BUILD SCUUCESS 如果执行命令后,出现如下所示:
cundongdeMacBook-Pro:~ cundong$ android update project --target android-20 --name SupportLib --path /Users/cundong/Documents/github/SupportLib
Updated project.properties
Updated project.properties
Added file /Users/cundong/Documents/github/SupportLib/build.xml
Updated file /Users/cundong/Documents/github/SupportLib/proguard-project.txt
It seems that there are sub-projects. If you want to update them
please use the --subprojects parameter.

则说明执行成功。

  • 常见BUILD FAILED问题

如果执行后,出现如下提示:

BUILD FAILED
/Users/cundong/Documents/github/iZhihuPaper/build.xml:44: The following error occurred while executing this line:
/Users/cundong/Documents/github/iZhihuPaper/build.xml:59: The following error occurred while executing this line:
/Applications/adt-bundle-mac-x86_64/sdk/tools/ant/build.xml:470: Invalid file: /Users/cundong/Documents/github/SmoothProgressBar/build.xml

则说明它所依赖的工程缺少project.properties、project.properties文件,请先参照步骤1,为其依赖的工程生成project.properties、project.properties文件。

如果遇到以下问题:

BUILD FAILED
/Applications/adt-bundle-mac-x86_64-20140624/sdk/tools/ant/build.xml:601: The following error occurred while executing this line:
/Applications/adt-bundle-mac-x86_64-20140624/sdk/tools/ant/build.xml:653: The following error occurred while executing this line:
/Applications/adt-bundle-mac-x86_64-20140624/sdk/tools/ant/build.xml:698: null returned: 1

则需要手动删除该工程的gen、bin目录。

配置 local.properties

配置local.properties文件,增加ant.dir、target.dir:

sdk.dir=/Applications/adt-bundle-mac-x86_64/sdk
ant.dir=/Applications/apache-ant-1.9.4
target.dir=/Users/cundong/Documents/ZhihuPaperRelease

ant.dir为ant安装目录,target.dir为批量打包的apk存储目录。

详细例子可参考:ZhihuPaper/local.properties

3 添加 ant.properties文件

1.将签名文件(*.keystore)拷贝到工程的目录。

2.在根目录下新建ant.properties文件。

key.store=android.keystore
key.alias=android
key.store.password=Cundong123456!@#
key.alias.password=Cundong123456!@#
market_channels=Wandoujia,360
app_name=ZhihuPaper
app_version=2.1

说明: key.store为签名文件; key.alias为签名文件别名; key.store.password、key.alias.password为密码; market_channels为我们需要生成的所有渠道列表,使用“,”分开;app_name为生成apk的文件名; app_version为生成apk的版本号;

详细例子可参考:ZhihuPaper/ant.properties

配置build.xml

为了实现批量打出多个渠道包,我们必须手动对刚刚生成的build.xml文件进行修改。

  • 引入ant.properties文件。

    <property file="ant.properties" />
  • 支持循环执行

    <!-- 支持循环执行 -->  <taskdef resource="net/sf/antcontrib/antcontrib.properties" >  <classpath>  <pathelement location="${ant.dir}/lib/ant-contrib-1.0b3.jar" />  </classpath>  </taskdef>  <echo>Run ant-contrib-1.0b3.jar ok</echo>  
  • 配置循环打包代码
    <target name="deploy">   <foreach target="edit_and_build" list="${market_channels}" param="channel" delimiter=",">   </foreach>   </target>  <target name="edit_and_build">   <echo>Run '${channel}' apk</echo>  <replaceregexpencoding="utf-8"file="AndroidManifest.xml"flags="s"match='android:name="UMENG_CHANNEL".+android:value="([^"]+)"'replace='android:name="UMENG_CHANNEL" android:value="${channel}"'/><property name="out.final.file"  location="${target.dir}/${app_version}/${app_name} V${app_version}(${channel}).apk" /> <antcall target="clean" />  <antcall target="release" />  </target>   

配置后,会读取ant.properties中market_channels中配置项,得到一个渠道号数组,对这个数据进行迭代,替换AndroidMainfext.xml文件中的android:name="UMENG_CHANNEL"。

每替换好一个,将输出到"out.final.file"。

${target.dir},即为local.properties文件中配置的target.dir=/Users/cundong/Documents/ZhihuPaperRelease ${app_name},即为ant.properties文件中配置的app_name=ZhihuPaper ${app_version},即为ant.properties文件中配置的app_version=2.1 ${channel},即为当前循环的渠道号

请务必保证${target.dir}/${app_version}目录真是存在并且有写权限。

当前例子中为:/Users/cundong/Documents/ZhihuPaperRelease/2.1,如果这么目录不存在,则会提示报错信息。

详细例子可参考:ZhihuPaper/build.xml

4 配置proguard-project.txt文件

proguard-project.txt,即混淆时的配置文件。

  • 引用的第三方jar包,不要混淆;
  • 自己写的控件,即需要配置在layout文件中的Widget,不要混淆;
  • Android的基础组件,不要混淆。

  • 需要在project.properties中配置:proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

详细例子可参考: ZhihuPaper/proguard-project.txt

5 打包

在iZhihuPaper中创建一个批处理文件,Mac为.sh文件,Window为.bat文件:

cd /Users/cundong/Documents/github/iZhihuPaper
ant deploy
pause

调用这个批处理文件,即可进行批量打混淆后的渠道包。

原文地址:https://github.com/cundong/blog/blob/master/Android%20Ant%20%E6%89%B9%E9%87%8F%E5%A4%9A%E6%B8%A0%E9%81%93%E6%89%93%E5%8C%85%E5%AE%9E%E4%BE%8B.md

这篇关于Android Ant 批量多渠道打包实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/878959

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想