如何通过命令行安装Android模拟器皮肤文件夹?

时间:2018-08-15 01:25:01

标签: android python emulation

我正在通过Python脚本自动执行Android SDK安装,但我不知道外观文件夹的来源。

当通过AVD Manager UI创建模拟器(从Android Studio启动时)时,似乎是在SDK位置中创建了该文件夹:

ll ~/Library/android/sdk
total 24
drwxr-xr-x  17 user  staff   544 Aug 14 20:33 ./
drwxr-xr-x   6 user  staff   192 Dec  7  2017 ../
-rw-r--r--   1 user  staff    16 Aug 14 19:35 .knownPackages
drwxr-xr-x   7 user  staff   224 Jul 17 14:56 build-tools/
drwxr-xr-x  51 user  staff  1632 Jan 11  2018 docs/
drwxr-xr-x  18 user  staff   576 Jul 22 01:26 emulator/
drwxr-xr-x   6 user  staff   192 Oct  9  2017 extras/
drwxr-xr-x   3 user  staff    96 Jan  2  2018 fonts/ # <- Where is it coming from?
drwxr-xr-x   8 user  staff   256 Jul 25 18:28 licenses/
drwxr-xr-x   3 user  staff    96 Oct  9  2017 patcher/
drwxr-xr-x  19 user  staff   608 Jun  6 17:30 platform-tools/
drwxr-xr-x   9 user  staff   288 Aug 12 19:55 platforms/
drwxr-xr-x  26 user  staff   832 Apr 25 10:53 skins/ # <- Where is it coming from?
drwxr-xr-x   8 user  staff   256 Jan 11  2018 sources/
drwxr-xr-x   8 user  staff   256 Aug  3 15:24 system-images/
drwxr-xr-x  14 user  staff   448 Oct  9  2017 tools/

问题:

  1. 字体 皮肤 文件夹来自哪里?
  2. 是否可以从URL下载它们?
  3. 是否有任何 avdmanager 命令强制安装?

这是我的python脚本,用于自动化安装:

#!/usr/bin/env python

import os
import urllib


# Android resource file
androidrc = os.path.join(os.environ['HOME'], '.androidrc')
if not os.path.exists(androidrc):
    with open(androidrc, 'w'):
        pass
# Bash resource file (~/.bashrc)
bashrc = os.path.join(os.environ['HOME'], '.bashrc')
# Environment variables needed for android
environment_variables = [
    ('ANDROID_TEMP_DIR', '/tmp/android'),
    ('ANDROID_HOME', '$HOME/Library/Android/sdk'),
    ('ANDROID_SDK_HOME', '$HOME'),
    ('ANDROID_TOOLS', '$ANDROID_HOME/tools'),
    ('ANDROID_PLATFORM_TOOLS', '$ANDROID_HOME/platform-tools')]


def humansize(nbytes):
    suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
    i = 0
    while nbytes >= 1024 and i < len(suffixes) - 1:
        nbytes /= 1024.
        i += 1
    f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
    return '%s %s' % (f, suffixes[i])


def add_to_bashrc():
    with open(bashrc, 'r') as bashrc_r:
        if androidrc not in bashrc_r.read():
            print("Including %s to %s" % (androidrc, bashrc))
            with open(bashrc, 'a') as bashrc_a:
                bashrc_a.write('''
# Adding environment for android
# include %s if it exists
if [ -f "%s" ]; then
    . "%s"
fi
''' % (androidrc, androidrc, androidrc))


def set_environment_variables():
    print("Creating/updating the %s resource file" % androidrc)
    with open(androidrc, 'w') as new_file:
        value = '''
# Setting environment for android
%s

export PATH=$ANDROID_PLATFORM_TOOLS:$ANDROID_TOOLS:$ANDROID_TOOLS/bin:$PATH
''' % '\n'.join(["export %s=%s" % (env[0], env[1]) for env in environment_variables])
        new_file.write(value)
    # Show content
    os.system('cat %s' % androidrc)


def install_android_sdk_tools():
    android_temp_dir = os.environ['ANDROID_TEMP_DIR']
    zip_file = os.path.join(android_temp_dir, "sdk-tools.zip")
    # Create android temp dir if doesn't exist
    if not os.path.exists(android_temp_dir):
        os.makedirs(android_temp_dir)
    if not os.path.exists(zip_file):
        # Download SDK tools zip file
        url = "https://dl.google.com/android/repository/sdk-tools-darwin-4333796.zip"
        print("Downloading sdk tools from: \n\t%s" % url)
        f = urllib.urlopen(url)
        with open(zip_file, "wb") as zipFile:
            zipFile.write(f.read())
        print("SDK tools downloaded to: \n\t%s [%s]" % (zip_file, humansize(os.path.getsize(zip_file))))
    # create android home if doesn't exists
    android_home = os.environ['ANDROID_HOME']
    if not os.path.exists(android_home):
        os.makedirs(android_home)
    # unzip SDK tools zip file into android home
    os.system('unzip -o %s -d %s' % (zip_file, android_home))
    print("\n%s content:\n" % android_home)
    os.system('ls -la %s' % android_home)

def install_android_platform_tools():
    print("\nInstalling 'platform-tools'...\n")
    os.system('echo "y\n" | sdkmanager "platform-tools"')


def install_android_platforms():
    print("\nInstalling platforms 23 to 28...\n")
    for i in range(23, 28):
        print("\nInstalling platforms;android-%s" % i)
        os.system('sdkmanager "platforms;android-%s"' % i)


def install_android_system_images():
    print("Installing system images 23 to 28...\n")
    for i in range(23, 28):
        print("\nInstalling system-images;android-%s;google_apis;x86" % i)
        os.system('echo "y\n" | sdkmanager --install "system-images;android-%s;google_apis;x86"' % i)


def install_emulator_skins():
    print("\nInstalling emulator...\n")
    os.system('sdkmanager "emulator"')
    print("\nInstalling skins...\n")
    android_temp_dir = os.environ['ANDROID_TEMP_DIR']
    # For now, I am downloading a pre-zipped file from storage URL
    # This is not the desired behavior, but is working for now
    skins_url = "https://www.example.com/skins.zip"
    print("Downloading skins from: \n\t%s" % skins_url)
    f = urllib.urlopen(skins_url)
    zip_file = os.path.join(android_temp_dir, "skins.zip")
    with open(zip_file, "wb") as zipFile:
        zipFile.write(f.read())
    print("Skins downloaded to: \n\t%s [%s]" % (zip_file, humansize(os.path.getsize(zip_file))))
    android_home = os.environ['ANDROID_HOME']
    # unzip skin zip file into android home
    os.system('unzip -o %s -d %s' % (zip_file, android_home))


def accept_licenses():
    print("\nAccepting all licenses...\n")
    os.system('echo "yes\n" | sdkmanager --licenses')

if __name__ == "__main__":
    # 1. Set environment variables
    set_environment_variables()
    add_to_bashrc()
    # 2. Install SDK tools
    install_android_sdk_tools()
    # 3. Install platform-tools
    install_android_platform_tools()
    # 4. Install android platforms
    install_android_platforms()
    # 5. Install system images
    install_android_system_images()
    # 6. Install skins
    install_emulator_skins() # <- This is where I need help
    # 7. Accept all licenses
    accept_licenses()

如何从Google的存储库中获取皮肤?

0 个答案:

没有答案