axmol/tools/appveyor-scripts/setup_android.py

124 lines
3.5 KiB
Python

#!/usr/bin/env python
'''
aaa
'''
import os
import urllib
import zipfile
import platform
import sys
import subprocess
import tempfile
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
COCOS2D_X = os.path.abspath(os.path.join(DIR_PATH, "../.."))
# ROOT_DIR/cocos2d-x
ROOT_DIR = os.path.abspath(os.path.join(COCOS2D_X, ".."))
ANDROID_NDK = os.path.join(ROOT_DIR, "android-ndk-r16b")
ANDROID_SDK = os.path.join(ROOT_DIR, "android-sdk")
SDK_MANAGER = os.path.join(ROOT_DIR, "sdk_tools/tools/bin/sdkmanager")
SYSTEM = platform.system().lower()
if SYSTEM == "windows":
SDK_MANAGER = SDK_MANAGER + ".bat"
def run(command):
print("=" * 80)
print(command)
subprocess.check_call(command.split())
def run_with_yes(command):
print("=" * 80)
print("yes|" + command)
f = tempfile.TemporaryFile("w")
repeat_yes = 50
f.write("y\n" * repeat_yes)
f.seek(0)
subprocess.check_call(command.split(), stdin=f)
def unzip(zip_file, directory):
print("=" * 80)
print("Unzip: " + zip_file + " to " + directory)
if SYSTEM == "windows":
zipfile.ZipFile(zip_file).extractall(directory)
else:
# module zipfile ignore priviliges i.e. +x
cmd = "unzip -d " + directory + " " + zip_file
subprocess.check_output(cmd.split())
def download(url, zip_file):
print("=" * 80)
print("Download: " + url + ", file: " + zip_file)
urllib.urlretrieve(url, zip_file)
def install_android_ndk():
file_name = "android-ndk-r16b-" + SYSTEM + "-x86_64.zip"
url = "https://dl.google.com/android/repository/" + file_name
zip_file = os.path.abspath(os.path.join(ROOT_DIR, file_name))
download(url, zip_file)
unzip(zip_file, ROOT_DIR)
def install_android_sdk_tools():
file_name = "sdk-tools-{system}-3859397.zip".format(
system=platform.system().lower())
url = "https://dl.google.com/android/repository/" + file_name
zip_file = os.path.abspath(os.path.join(ROOT_DIR, file_name))
download(url, zip_file)
unzip(zip_file, os.path.join(ROOT_DIR, "sdk_tools"))
def install_android_sdk():
switches = " --verbose --sdk_root=" + ANDROID_SDK + " "
cmd1 = SDK_MANAGER + switches
packages = [
"platforms;android-15",
"build-tools;25.0.0",
"platform-tools",
"tools"
]
cmd = cmd1 + " ".join(packages)
run_with_yes(cmd)
def export_environment():
# gradle
# os.environ["ANDROID_HOME"] = ANDROID_SDK
# os.environ["ANDROID_NDK_HOME"] = ANDROID_NDK
# # cocos gen-libs
# os.environ["ANDROID_SDK_ROOT"] = ANDROID_SDK
# os.environ["NDK_ROOT"] = ANDROID_NDK
with open(os.path.join(ROOT_DIR, "environment.sh"), "a") as myfile:
myfile.write("export ANDROID_HOME=" + ANDROID_SDK + "\n")
myfile.write("export ANDROID_NDK_HOME=" + ANDROID_NDK + "\n")
myfile.write("export ANDROID_SDK_ROOT=" + ANDROID_SDK + "\n")
myfile.write("export NDK_ROOT=" + ANDROID_NDK + "\n")
with open(os.path.join(ROOT_DIR, "environment.ps1"), "a") as myfile:
myfile.write("$env:ANDROID_HOME=\"" + ANDROID_SDK + "\"\n")
myfile.write("$env:ANDROID_NDK_HOME=\"" + ANDROID_NDK + "\"\n")
myfile.write("$env:ANDROID_SDK_ROOT=\"" + ANDROID_SDK + "\"\n")
myfile.write("$env:NDK_ROOT=\"" + ANDROID_NDK + "\"\n")
def main():
run(sys.executable + " " + os.path.join(COCOS2D_X, "download-deps.py") + " --remove-download=False")
install_android_ndk()
install_android_sdk_tools()
install_android_sdk()
export_environment()
if __name__ == '__main__':
main()