Preface:

This weekend, I learned Python and wrote a package script that can be easily packaged and installed. Since I just started to use Python, I don’t know how to write object-oriented and process-oriented Python. After a process, I can write more easily after further study.

This article code in: LearnTechDemo 00Something directory auto. Py and auto. Config two files.

1. Main functions:

  1. Automatically pull code from Gitgit pullgit clone

    If the script is executed for the first time during this process, write to the root directorylocal.propertiesFile to set the SDK path.
  2. Execute pack commandgradle cleangradle assembleDebug
  3. Automatic installation to the machineadb installadb shell am start -nAutomatically open

The main function of the script is these three, after reading a general understanding, as long as the execution, can automatically run the latest APK mobile phone and open.

There are some other handy features:

  1. Create the AndroidApp in the specified directory, and create the Apk and SourceCode directories in this directory. Apk directory: Copy the Apk files in the build directory after packaging. Git Clone project code to SourceCode directory

  2. Add the auto-. config configuration file to configure some basic paths. Use Python’s ConfigParser package to read the configuration file.

    • Current main configuration:

      Root_SDK_Dir: The absolute path to the SDK, for local.properties

      git_clone_address: Git address of the project

      git_branch_name: The name of the remote branch to pull

      assembleRelease: Boolean value, whether to release the package

      base_file_dir: In which directory to create the AndroidApp folder

      create_dir_name: is the name of the AndroidApp folder where you can set this change
    • These two can be left aloneoptional:

      create_code_dir_name: The name of the SourceCode folder

      create_apk_dir_name: The name of the Apk folder
  3. Parse the manifest.xml file for PackageName and LauncherActivityName. The XML is parsed using ElementTree, and the name of the LauncherActivity is recursively obtained to open the app.

  4. Python2 and python3 are compatible. Python2 runs on the Mac, and python3 runs on Windows.

2. Usage

  1. Place the auto-. py and auto-. config files in the same level of directory.

  2. Then configure the auto. Config file: Root_SDK_Dir is the absolute path to your SDK. Git_clone_address is your Git address. Git_branch_name Is the remote branch name to pull. Base_file_dir is an absolute path, and create_dir_name will generate a directory in base_file_dir to store the project code and the generated APK files

Once configured, you can run the script from the Python command line and wait for it to be packaged and run to the machine.

3. Main code introduction

Git pull code

Os. chdir(code_dir) print(' enter SourceCode:  ' + os.getcwd()) print('\n') print('=============================================') print('git clone or git pull') print('=============================================') if not os.listdir(code_dir): os.system('git clone ' + git_clone_address +' ' + code_dir) else: os.system('git pull')Copy the code

2. Gradle packaging


def createLocalPropertiesFile(sourceDir,fileName,root_sdk_dir):
    if not os.path.exists(sourceDir):
        return

    fileDir = sourceDir + '/' + fileName

    if os.path.exists(fileDir):
        return

    f = open(fileDir,'w');
    f.write('sdk.dir=' + root_sdk_dir)
    f.close()

createLocalPropertiesFile(code_dir,'local.properties',Root_SDK_Dir)


print('\n')
print('=============================================')
print('gradle clean')
print('=============================================')
os.system('gradle clean');

print('\n')
print('=============================================')
print('gradle assembleDebug, generate apk')
print('=============================================')
os.system('gradle assembleDebug')Copy the code

3. Install the apk


print('\n')
print('=============================================')
print('install apk')
print('=============================================')

def getFileName(sourceDir):
    if not os.path.exists(sourceDir):
        return;

    for filename in os.listdir(sourceDir):
        if '.apk' in filename:
            return filename



apkName = getFileName(apk_dir)
apkPath = apk_dir + '/' + apkName
print(apkPath)

os.system('adb install -r ' + apkPath)Copy the code

4. Open the app

def findLauncherActivityName(ele,targetString): for childElem in ele: if len(list(childElem)) == 0: if cmp(childElem.get('{http://schemas.android.com/apk/res/android}name'),"'" + targetString +"'"): return True else: return False else: return findLauncherActivityName(childElem,targetString) def getLauncherActivity(xmlFileDir): tree = ET.ElementTree(file=xmlFileDir) root = tree.getroot() packageName = root.get('package') for elem in tree.iter(tag  = 'activity'): if findLauncherActivityName(elem,'android.intent.action.MAIN'): if(findLauncherActivityName(elem,'android.intent.category.LAUNCHER')): return packageName,elem.get('{http://schemas.android.com/apk/res/android}name') PackageName,LauncherActivity = getLauncherActivity(code_dir + '/app/src/main/AndroidManifest.xml') os.system('adb shell am force-stop ' + PackageName) if PackageName in LauncherActivity: os.system('adb shell am start -n '+ PackageName +'/' + LauncherActivity) else: os.system('adb shell am start -n '+ PackageName +'/' + PackageName + LauncherActivity)Copy the code

4. The drawback

This script requires a lot of environment configuration, especially Gradle configuration. During the packaging process, you need a series of tools, BuildTools, Gradle plungin, etc., not suitable for people outside of development, there is so much to learn.

But this script is a good project for learning Python and getting your hands dirty.

conclusion

Find Python easy to learn and convenient. The absence of semicolons and curly braces looks really neat, and strings can be easily quoted. Well, in the process, it seems like Jenkins can do this kind of thing very easily, you can look into it.

Well, I hope it’s helpful. Please leave an issue or message if there is a Bug or better implementation in the process of use.