欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Lua脚本自动生成APK包

程序员文章站 2022-04-29 08:44:03
上次用了纯bat写了个脚本生成apk包,感觉bat扩展性和语法差的令人发指,这次用lua重写了一个脚本 可以根据需要自行扩展了。 使用前tool path&n...

上次用了纯bat写了个脚本生成apk包,感觉bat扩展性和语法差的令人发指,这次用lua重写了一个脚本
可以根据需要自行扩展了。
使用前tool path 还有 target path的前两个还是需要自己设置下。
一些小的函数 jit_file copy_file 我就不贴了 比较简单,用来luajit 和 拷贝。

-- authors: sails鸢@oschina
-- date: 20th , august , 2014
-- note:
-- this is used for cocos2dx + lua
-- this is a script to making .apk file for android platform
-- make sure you have installed java, ant, android sdk, ndk, svn, jit
-- also plz check and rewrite following paths before you use this script
-- remarks:
-- the script will update your cocos engine directory and your lua script which probably is resources
-- then it should jit your lua files , use asmaker to encrypt your lua-jit files 
-- all files and resources will move to this folder proj.android/assets
-- finally it will make a .apk package with ant
require('support')
 
--tools paths
local java_home = 'c:\\program files\\java\\jdk1.8.0_05'
local ant_home = 'd:\\programsoftware\\apache-ant-1.9.4'
local android_home = '"d:\\programsoftware\\android sdk\\sdk"'
local ndk_home = 'd:\\programsoftware\\android-ndk-r9d-windows-x86_64\\android-ndk-r9d'
local svn_home = 'c:\\program files\\tortoisesvn\\bin\\'
 
--target paths
local engine_dir = 'd:\\engine'
local work_dir = 'd:\\engine\\projects\\xxxx\\proj.android'
local resources_dir = work_dir ..'\\..\\resources'
local assets_dir = work_dir ..'\\assets'
 
--function detect directory
local function dir_exist(dir)
  return os.execute(string.format('pushd "%s">nul 2>nul && popd', dir))
end
 
--remove old assets
if dir_exist(assets_dir) then
  rmdir(assets_dir)
end
 
--remove old apk
local old_apk , err = io.open(work_dir..'\\bin\\xxxx-release.apk')
if err == nil then
  old_apk:close()
  delfile(work_dir..'\\bin\\xxxx-release.apk')
end
 
--svn update
--check
--svn_up(engine_dir)
--svn_up(work_dir..'\\..')
 
--luajit 
--iter directory
local cmd = string.format("pushd %q &dir /b /s &popd" , resources_dir)
local file_list = io.popen(cmd)
for line in file_list:lines() do
  line_to = string.gsub(line, 'resources', 'resources_jit')
  if dir_exist(line) then
    check_mk_path(line_to)
  else
    if(string.find(line,'.lua$')) then
      jit_file(work_dir, line , line_to)
    else
      copy_file(line , line_to)
    end
  end
end
file_list:close()
 
--encryption with asmaker
local enc_cmd = work_dir..'\\asmaker.exe'..' -i '..work_dir..'\\..\\resources_jit'.." -o "..assets_dir..' -f .lua -e .exe'
local enc_re = run_one_cmd(enc_cmd)
if enc_re:find ("失败") then
  print("asmaker加密文件夹失败!",enc_re)
  os.exit(1)
end
 
--ndk build
local ndk_cmd = 'call '..ndk_home..'\\ndk-build'..' -c '..work_dir..' '..'ndk_module_path='..engine_dir..';'..engine_dir..'\\cocos2dx\\platform\\third_party\\android\\prebuilt'
local ndk_re = run_one_cmd(ndk_cmd)
if ndk_re:find ("error") then
  print("ndk build失败!",ndk_re)
  os.exit(1)
end
 
--android update
local and_cmd = 'call '..android_home..'\\tools\\android'..' update project -p '..work_dir
local and_re = run_one_cmd(and_cmd)
and_cmd = 'call "'..android_home..'\\tools\\android"'..' update lib-project -p '..engine_dir..'\\cocos2dx\\platform\\android\\java'
and_re = run_one_cmd(and_cmd)
 
--ant
local ant_cmd = 'pushd '..work_dir..'&call '..ant_home..'\\bin\\ant release'
local ant_re = run_one_cmd(ant_cmd)
if ant_re:find ("failed") then
  print("生成apk失败!",ant_re)
  os.exit(1)
end