AndroidのBuild工具之Ant动手实践

本文分享了使用Ant脚本解决Eclipse打包Android项目时遇到的问题。介绍了如何通过自动生成和自定义build.xml文件实现自动化的打包流程,并解决了包体积不一致及签名问题。
好久没有写博客了,没半年也应该有几个月了。在工作上的项目遇到过很多问题或者说积累了不少经验,曾经都蛮想发到博客留个纪念什么的,不求可以为别人获得点经验技巧,只求在多年后遇到同样的问题可以找到个记录。但是,也许是懒终归是懒,而且上班时间写博客有点不好吧(下班后就容易忘记)。

由于用Eclipse导出签名包经常失败,或因内存不足或者其他什么搞不懂的原因,项目也确实算得上是有点庞大了,光library就有七八个,好不容易守着打包界面代码都不能敲一行看着打包对话框结束(通常就意味着导出apk成功了),最后又缺斤少两。正常情况下是大概有6.8M体积,占用空间7M多,但打不出来的包就会少那么一点有的时候5.8M有的时候6.8M,这样的包运行起来是会找不到类的。最恼人的时候,打了一天包都难打出一个标准无误的apk。就算打出来,这个过程也是非常久的,而且是占着Eclipse前台,在此期间你不能做别的事,所以就特别讨厌,然后就尝试学学ant,此前也用过Maven,在android上用起来还是有点不方便。

 

在网上看教程也看得忙累的,倒是找到一片博客照着做,最终也成功了(忘了是哪篇博客地址,不好意识),期间也遇到过几个问题,但是现在都忘了。

一.自动调用sdk目录下的build.xml来打包

如果在主工程build必须每一个library也update出来自己build.xml,而且library的目录最好不能有中文,要不然会出错。

用cmd进入library的目录,然后执行这条命令就可以为你的项目自动生成一个build.xml文件。

android update project --path

再然后就在你的主工程的build.xml执行ant release就可以胜利打包了。

如果要打签名包,就要在ant.properties写好keystore信息就可以自动生成签名包了。

<?xml version="1.0" encoding="UTF-8"?>
<project name="MainActivity" 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" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>

    <!-- 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 the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

    <!--
        Import per project custom build rules if present at the root of the project.
        This is the place to put custom intermediary targets such as:
            -pre-build
            -pre-compile
            -post-compile (This is typically used for code obfuscation.
                           Compiled code location: ${out.classes.absolute.dir}
                           If this is not done in place, override ${out.dex.input.absolute.dir})
            -post-package
            -post-build
            -pre-clean
    -->
    <import file="custom_rules.xml" optional="true" />

    <!-- 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 -->
    <import file="${sdk.dir}/tools/ant/build.xml" />

</project>

android update project --path命令执行后生成build.xml

key.store = C:/Users/Bvin/Desktop/test.key
key.alias = test
key.store.password = 101817
key.alias.password = 101817
java.encoding = UTF-8

ant.properties用以记录key.store信息。

-release-unaligned.apk未zip对齐的apk

-release-unsigned.apk未签名的apk

-release.apk对齐了也签了名的apk

如果混淆了在prouard有个mapping.xml文件保存了每次生成后的混淆对应文件

二:自定义build.xml实现扩展功能

第一种方法有局限性,用的是SDK目录下的build.xml文件所以也有很多不必要的文件,也不能重新对apk命名和复制到某个目录

前几天一直想做这个事,就是把bin目录下的release文件重名并赋值到指定目录,这样我就可以叫测试去我共享的目录去测试,而不是每次都是我用QQ发给他网速又慢。

如上图做个测试,在build.xml引入custom_rules.xml,在custom_rules定义一个target放在release后面,我就以为在build.xml执行release后会自动出来mytest的输出。

结果令我失望了。今天刚好看了一下《Ant权威指南》一书(PDF版,轻喷),里面说到"依赖关系指定了在当前目标执行前,Ant必须执行的目标",这下我就豁然开朗了,别的目标依赖release,但执行release目标并不会执行依赖它的目标,应该是执行mytest,这样ant就会先调用release然后执行mytest。

按照这个想法直接执行custom_rules.xml的mytest结果报错了,稍微百度一下然后想想应该是缺少了什么引用,然后就反过来import这个build.xml文件,这样就可以果然可以了

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default = "">

    <!-- 申明sdk.dir -->
    <property file="local.properties" />
    
    <!-- 申明keystore --> 
    <property file="ant.properties" />

     <property environment="env" />
        <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>
    
    <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 the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />
    
    <target name="mytest" depends="release">
        <echo>finally suc</echo>
    </target>
    <!-- depends = "-release-sign" -->
    <target name="copyApk">
        
        <tstamp>
            <format property= "nowtime" pattern = "yyyyMMdd_HH.mm"/>
        </tstamp>
        <echo level="info">copyApk...${ant.properties.name}-${nowtime}</echo>
        <copyfile src = "${ant.project}/bin/${ant.project.name}-release.apk"
            dest = "C:/Users/Bvin/Desktop/${ant.project.name}-${nowtime}.apk"
            forceoverwrite = "true" />
        <echo level="info">copy dir:"C:/Users/Bvin/Desktop/${ant.project.name}-${nowtime}.apk"</echo>
    </target>
    
    <import file="${sdk.dir}/tools/ant/build.xml" />
</project>

最后胜利在打包然后输出了finally suc

引入build.xml这样ant也把build.xml里面的target全部加载出来了

内容概要:本文围绕“光伏-储能-数据中心”系统的容量优化配置展开研究,旨在通过构建数学模型并结合Matlab编程实现,对园区内光伏发电、储能设备与数据中心算力负荷之间的多能互补关系进行协同优化。研究综合考虑可再生能源出力的波动性、储能系统的充放电特性以及数据中心的动态用电需求与算力调度特性,建立以最小化综合成本、降低碳排放强度、提升能源自给率等为目标的多目标优化模型,并采用先进的智能优化算法(如粒子群、灰狼、鲸鱼等)进行求解,从而确定光伏装机容量与储能系统配置的最优方案。文中提供了完整的Matlab代码实现流程,涵盖数据预处理、模型构建、算法求解与结果可视化全过程,属于综合能源系统与信息基础设施深度融合的前沿交叉课题。; 适合人群:具备电力系统、能源工程、自动化或计算机等相关专业背景,熟悉Matlab编程与基本优化算法原理,从事新能源利用、绿色数据中心、综合能源系统规划与低碳调度等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握光伏-储能-数据中心耦合系统的建模思路与多能流协同机制;② 学习基于Matlab的多目标容量优化配置方法与智能算法应用技巧;③ 为工业园区、数字经济园区及绿色数据中心的能源系统规划、节能减排与可持续运行提供科学决策依据和技术解决方案。; 阅读建议:建议结合所提供的Matlab代码,深入理解目标函数设计、约束条件设定及算法实现细节,可通过调整负荷参数、改变气候数据、引入新的优化目标或约束条件等方式进行拓展性实验,以深化对系统特性的认知并提升实际科研与工程应用能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值