Android Studio Sub-module Automation build Combat

Source: Internet
Author: User
Tags unpack shebang

Android Studio Sub-module Automation build Combat

@author ASCE1885 's Github book Weibo CSDN

Recently, using Android Studio+gradle as an Infrastructure SDK project, the framework primarily implements the basic capabilities that each app needs, such as network requests, image caching, JSON parsing, logging, and so on.

As we all know, Androidstudio should try to use modules to partition the module, not only to achieve the purpose of decoupling the module, but also when necessary to easily implement sub-module packaging, especially in the SDK project. So what is sub-module packaging? Is that we can automatically provide the full version of the SDK, part of the feature version, and the minimum functional version, depending on the needs of third-party users.

Our project structure is as follows, each function is independent of a module:

Since our modules are all purely code and do not contain resource files, they are not available in the form of an AAR package but in the form of a jar package. By the way, the generated AAR package default path is:

build/output/aar/

The jar package can be found in the following path:

build/intermediates/bundles/debug/classes.jarbuild/intermediates/bundles/release/classes.jar
Merging of JAR Packages

As you can see from Project Engineering, our project contains multiple module, and the final compilation of each basic function module is a classes.jar. So project will eventually generate a bunch of jar packages, and when it's released, we're going to provide a separate jar package, so we need to merge the jar packages. Unfortunately, Android Studio does not provide such a feature, so you can only rely on your own script to invoke the jar command to implement, open the command line terminal, enter the jar to print out the use of the jar, as follows:

guhaoxindemacbook-pro:~ guhaoxin$ jar usage: jar {ctxui}[vfm0me] [jar-file] [manifest-file] [entry-point] [-c dir] files ... Options include:-C Create a new archive file-T list archive directories-x Extract the specified (or all) files from the file-u update an existing archive file-v generate verbose output in standard output-F Specify the archive file name-m contains inventory information from the specified manifest file-E is a standalone application bundled into an executable jar fileSpecify the application entry point- 0 storage only; no use case any ZIP compression- M do not create a manifest file for the entry-I generate index information for the specified jar file- C change to the specified directory and include the files in itIf there is any catalog file, it is recursively processed. The manifest file name, the archive file name, and the entry point name are specified in the same order as the ' m ', ' f ', and ' e ' tags.           Example 1: Archive two class files into an archive named Classes.jar: Jar CVF Classes.jar Foo.class bar.class Example 2: Using the existing manifest file ' Mymanifest ' and Archive all files in the foo/directory to ' Classes.jar ': Jar CVFM classes.jar mymanifest-c foo/.

With the jar command, two main functions are implemented:

    • Extract the class files from all jar packages to a directory
    • Re-compress all class files to a separate jar package

Since the jar command cannot specify the final output directory, we need to first CD to a temporary directory for the extracted class file, then unpack all the jar packages in turn, and the extract command looks like this:

jar -xvf ../../hfasynchttp/build/intermediates/bundles/debug/classes.jar

When all the jar packages are unpacked, then the compression command is executed, so that a separate jar package is available:

jar -cvfM AndroidHyperion_${version}_debug.jar .
Sub-module Automation construction

The automated build consists of local build and Jenkins build two parts, the local build is mainly used to develop its own debugging use, Jenkins build is mainly used for testing, product pickup and run monkey use.

Local Build

The local build script file is located in the build_local.sh under the project root, and the main features of the script are:

    • Call the Gradlew command to perform a gradle compilation to generate the jar packages for each module
    • Unpack the jar packages generated by each module and repackage the extracted class files into a separate jar package
    • Sub-module packaging functionality is controlled by defining a Boolean variable value
    • Output Directory is

The contents of the build_local.sh file are as follows:

#!/bin/sh#使用Gradle编译各个module./gradlew Clean./gradlew Build--stacktrace--debug#进入输出目录CDOutput#清空输出目录RM-RF *#创建输出子目录mkdir tempmkdir Debugmkdir Release#定义sdk版本号version="1.0.0"#定义模块是否打包标识is_include_hfasynchttp=trueis_include_bitmapfun=trueis_include_hfjson=trueIs_include_hflogger=true#省略其他...#解压所有debug版本的jar包到temp目录中CDTempif $is _include_hfasynchttp; ThenJAR-XVF. /.. /hfasynchttp/build/intermediates/bundles/debug/classes.jarfiif $is _include_bitmapfun; ThenJAR-XVF. /.. /hfbitmapfun/build/intermediates/bundles/debug/classes.jarfiif $is _include_hfjson; ThenJAR-XVF. /.. /hfjson/build/intermediates/bundles/debug/classes.jarfiif $is _include_hflogger; ThenJAR-XVF. /.. /hflogger/build/intermediates/bundles/debug/classes.jarfi#压缩所有debug版本的class文件到一个独立的jar包中JAR-CVFM Androidhyperion_${version}_debug.jar.#拷贝文件MV Androidhyperion_${version}_debug.jar. /debug#清空temp目录RM-RF *#解压所有release版本的jar包到temp目录中if $is _include_hfasynchttp; ThenJAR-XVF. /.. /hfasynchttp/build/intermediates/bundles/release/classes.jarfiif $is _include_bitmapfun; ThenJAR-XVF. /.. /hfbitmapfun/build/intermediates/bundles/release/classes.jarfiif $is _include_hfjson; ThenJAR-XVF. /.. /hfjson/build/intermediates/bundles/release/classes.jarfiif $is _include_hflogger; ThenJAR-XVF. /.. /hflogger/build/intermediates/bundles/release/classes.jarfi#压缩所有release版本的class文件到一个jar包中JAR-CVFM Androidhyperion_${version}_release.jar.#拷贝文件MV Androidhyperion_${version}_release.jar. /release#删除temp目录CD.. RM-RF Temp
Jenkins Build

The Jenkins compilation script file is located at the build_jenkins.sh of the project root, and the main features of the script are:

    • Call the Gradlew command to perform gradle compilation, generate each Moudle jar package
    • Unpack the jar packages generated by each module and repackage the extracted class files into a separate jar package
    • The Sub-module packaging function is controlled by parameterized construction parameters configured on Jenkins
    • Output Directory is

As you can see, the only difference from the local build is that the parametric build parameters of the sub-modules are defined on Jenkins, not in the local script, and for the sake of completeness, we will stick the complete script file:

#!/bin/sh./gradlew Clean./gradlew Build--stacktrace--debug#进入输出目录CDOutput#清空输出目录RM-RF *#创建输出子目录mkdir tempmkdir Debugmkdir ReleaseCDTemp#解压所有debug版本的jar包if $is _include_hfasynchttp; ThenJAR-XVF. /.. /hfasynchttp/build/intermediates/bundles/debug/classes.jarfiif $is _include_bitmapfun; ThenJAR-XVF. /.. /hfbitmapfun/build/intermediates/bundles/debug/classes.jarfiif $is _include_hfjson; ThenJAR-XVF. /.. /hfjson/build/intermediates/bundles/debug/classes.jarfiif $is _include_hflogger; ThenJAR-XVF. /.. /hflogger/build/intermediates/bundles/debug/classes.jarfi#压缩所有debug版本的class文件到一个jar包中JAR-CVFM Androidhyperion_${version}_debug.jar.#移动生成的jar包到debug目录MV Androidhyperion_${version}_debug.jar. /debug#清空temp目录RM-RF *#解压所有release版本的jar包if $is _include_hfasynchttp; ThenJAR-XVF. /.. /hfasynchttp/build/intermediates/bundles/release/classes.jarfiif $is _include_bitmapfun; ThenJAR-XVF. /.. /hfbitmapfun/build/intermediates/bundles/release/classes.jarfiif $is _include_hfjson; ThenJAR-XVF. /.. /hfjson/build/intermediates/bundles/release/classes.jarfiif $is _include_hflogger; ThenJAR-XVF. /.. /hflogger/build/intermediates/bundles/release/classes.jarfi#压缩所有release版本的class文件到一个jar包中JAR-CVFM Androidhyperion_${version}_release.jar.#移动生成的jar包到release目录MV Androidhyperion_${version}_release.jar. /release#删除temp目录CD.. RM-RF Temp
Local and Jenkins parametric build parameter definitions
type name default description
String version 1.0.0 hyperion SDK version number
Boolean is_inclu De_hfasynchttp true package hfasynchttp
Bo Olean is_include_bitmapfun true packaging HFB Itmapfun
Boolean is_include_hfjson true
Boolean is_include_hflogger true package Hflogger

Android Studio Sub-module Automation build actual combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.