Skip to content

Instantly share code, notes, and snippets.

@sys1yagi
Forked from shikajiro/fabfile.py
Last active November 14, 2016 00:51
Show Gist options
  • Save sys1yagi/1f395190061a8d48db5e30ae160b4b99 to your computer and use it in GitHub Desktop.
Save sys1yagi/1f395190061a8d48db5e30ae160b4b99 to your computer and use it in GitHub Desktop.
Script to perform the build of Android on another machine
# Script Name : fabfile.py
# Author : shikajiro
# Created : 2016-11-03
# Last Modified : 2016-11-03
# Version : 1.0.0
# Modifications :
# Description :
# Script to perform the build of Android on another machine
# By performing such a build of severely time on another machine,
# it is possible to ensure the CPU resources of the development machine,
# it is possible to prevent the "can not do anything during the build" state
#
# Install
# 1. this file create to project root dir.
# 2. please setting. env.hosts, module, flavor, debug.
# 3. run this
# $ fab build
# fabric install
# sudo env ARCHFLAGS="-arch i386 -arch x86_64" pip install fabric
from __future__ import with_statement
import re
from fabric.operations import *
from fabric.api import *
from fabric.contrib.project import rsync_project
# build machine setting
env.user = # set ssh user name
env.hosts = # set vm instance IP
env.key_filename = # set ssh key file path ex: ~/.ssh/gcloud-ssh-key
# env.port = 22
# env.password = hogehoge
###
# build Android setting
###
# target module and flavor
module = "mobile"
flavor = "dev"
# debug build : True
# release build : False
debug = True
# App Package Name
package = "com.example.android.app"
# LAUNCHER Activity
launch_activity = "com.example.android.app.activity.MainActivity"
# remote machine build dir
tmp_dir = "~/"
def build():
"""
other machine build and start activity.
1.sync file. local machine -> remote machine
2.gradle assemble.
3.get apk.
4.install apk, and start App.
:return:
"""
gradle_cmd = "./gradlew assemble" + flavor.capitalize()
if debug:
gradle_cmd += "Debug"
package_name = package
if debug:
package_name += ".debug"
if debug:
apk_name_format = "{0}-{1}-debug.apk"
else:
apk_name_format = "{0}-{1}.apk"
apk_name = apk_name_format.format(module, flavor)
build_dir = tmp_dir + package
# files written to '.gitignore' is not sync.
with open(".gitignore", "r") as f:
excludes = [line.replace("\n", "") for line in f if not re.match(r"[#\n]", line)]
ignore_file = "local.properties" # must exclude
if ignore_file not in excludes:
excludes.append(ignore_file)
excludes.append(".git")
start_time = time.time()
# sync to remote machine
rsync_project(remote_dir=build_dir,
local_dir=".",
exclude=excludes,
delete=True)
rsync_time = time.time()
print "rsync time {0}".format(rsync_time - start_time)
# run gradlew
with cd(build_dir):
run(gradle_cmd)
assemble_time = time.time()
print "assemble time {0}".format(assemble_time - rsync_time)
# get apk
local("mkdir -p " + module + "/build/outputs/apk/")
get(remote_path=build_dir + "/" + module + "/build/outputs/apk/" + apk_name,
local_path=module + "/build/outputs/apk/")
apk_get_time = time.time()
print "apk_get time {0}".format(apk_get_time - assemble_time)
print "build total time {0}".format(apk_get_time - start_time)
# start App
local("adb push ./" + module + "/build/outputs/apk/" + apk_name + " /data/local/tmp/" + package_name)
local("adb shell pm install -r '/data/local/tmp/" + package_name + "'")
local("adb shell am start -n '" + package_name + "/" + launch_activity + "' -a android.intent.action.MAIN -c android.intent.category.LAUNCHER")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment