android 自动化编译,批量打包

android 自动化编译,批量打包

  1. ant 配置
  2. android update 命令
  3. 使用 AntDemo 的 文件
  4. 文件编码问题
  5. python 脚本

1.ant 配置
http://www.cnblogs.com/avenwu/p/3515973.html 。电脑已经安装过cocos, ant 已经配置好了。

2.(我的android项目引用了 user library) 打开终端,
cd 到android 主工程,执行命令

android update project --path . --name MainProject --target 4 --subprojects

–path :表示路径
–name :表示项目名称 可以随便命名
–target :SDK 版本,具体数字可以 执行 android list targets 命令查看
–subprojects : 表示附带Lib 工程

再 cd 到库工程 , 执行命令

android update project --path . --name LibProject --target 4

参数解释同上

  1. https://github.com/sinkcup/AntDemo 下载工程文件。 下载custom_rules.xml,放到项目目录中,然后执行:
    ant auto-debug -Dversion=time
    即可自动打包,生成的包在./bin/中。

具体操作可以查看 https://github.com/sinkcup/AntDemo 的说明。

  1. 保证所有的 源文件的编码格式 是 utf-8

  2. 如果需要批量编译 N 多个渠道包,每个渠道包的包名和渠道号都不同。具体需求是配置一个csv文件,每一行表示一个APK包,每行数据包括,游戏名,包名,渠道号等等, 然后根据每一行数据生产一个APK包。

    使用python 来实现这些业务逻辑。

    5.1 读取csv

def startBuild():
    clear()
    with open('config.csv') as csvfile:
       reader = csv.DictReader(csvfile)
       for row in reader:
           #print row['Name'], row['Package'],row['APPID'],row['APPKey'],row['Channel']
           doBuildAPK(row)

    csvfile.close() 

5.2 修改游戏名等
新建一个模板文件 strings.xml

     <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">APPName</string>
    <string name="company">COMPANY</string>
    <string name="tel">TEL</string>

</resources>

然后使用python 替换掉相应的字符(如APPName 游戏名),再覆盖到工程中相应的文件。
#修改游戏的名字

def  changeName(name,company,tel):
      input = open('./buildConfig/strings.xml', 'r')
      inputStr=input.read().replace('APPName',name)
      inputStr=inputStr.replace('COMPANY',company);
      inputStr=inputStr.replace('TEL',tel);

      wf = open('./res/values/strings.xml', 'w')
      wf.write(inputStr)
      wf.close()

      input.close

附上完整的 python 脚本

#!/usr/bin/env python
# coding: utf-8

import csv
import sys
import os, os.path

#修改游戏的名字
def  changeName(name,company,tel):
      input = open('./buildConfig/strings.xml', 'r')
      inputStr=input.read().replace('APPName',name)
      inputStr=inputStr.replace('COMPANY',company);
      inputStr=inputStr.replace('TEL',tel);

      wf = open('./res/values/strings.xml', 'w')
      wf.write(inputStr)
      wf.close()

      input.close

#修改计费代码   
def  changePayCodeFile(AppID,AppKey):
      input = open('./buildConfig/paycode.properties', 'r')

      fileStr=input.read().replace('ID',AppID)
      fileStr=fileStr.replace('KEY',AppKey)

     # print fileStr

      index=1
      while index <= 15 :
          rCode='CODE'+ str( index )
          newCode=AppID+'%02d'%index
          fileStr=fileStr.replace(rCode,newCode)

          index=index+1

      wf = open('./assets/paycode.properties', 'w')
      wf.write(fileStr)
      wf.close()

      input.close

def  changePackageName(package):
      input = open('./buildConfig/AndroidManifest.xml', 'r')
      inputStr=input.read().replace('00000PACAKAGENAME00000000',package) 

      wf = open('./AndroidManifest.xml', 'w')
      wf.write(inputStr)
      wf.close() 
      input.close


#修改关于信息
def changeInfo(Company,Tel):
     pass

def changeProject(info):
    # changePackageName( info['Package'] )
     changeName(info['Name'], info['Company'] ,info['Tel'])
     changePayCodeFile( info['APPID'] , info['APPKey']  )

def doBuildAPK(info):

    cmd='rm ./bin.*apk'
    os.system(cmd)

    changeProject(info)
    cmd='ant auto-release -DUMENG_CHANNEL=%s -Dpackage=%s -Dversion=time'%( info['Channel'] ,info['Package'])
    os.system(cmd) 

    cmd='mv ./bin/*.apk ./target/%s_%s_%s.apk'%(info['Name'],info['Company'],info['Package'])
    os.system(cmd) 

##清空缓存
def clear():
    cmd='rm ./bin/*.*'
    os.system(cmd)
    cmd='rm ./gen/*.*'
    os.system(cmd)

    cmd='rm ../AoTeManLib/bin/*.*'
    os.system(cmd)

    cmd='rm ../AoTeManLib/gen/*.*'
    os.system(cmd)


def startBuild():
    clear()
    with open('config.csv') as csvfile:
       reader = csv.DictReader(csvfile)
       for row in reader:
           #print row['Name'], row['Package'],row['APPID'],row['APPKey'],row['Channel']
           doBuildAPK(row)

    csvfile.close() 

startBuild()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值