Android Studio automated building practices, androidstudio

Source: Internet
Author: User

Android Studio automated building practices, androidstudio
Automated building of modules in Android Studio

@ Author ASCE1885 Github short book Weibo CSDN

Recently, Android Studio + Gradle is used as a basic framework SDK project. This framework mainly implements the basic capabilities required by each app, such as network requests, image caching, json parsing, and logging.

As we all know, modules should be divided as much as possible in AndroidStudio, which can achieve the purpose of Module decoupling and easily package modules when necessary, especially in SDK projects. So what is modular packaging? That is, we can automatically provide full SDK versions, some functional versions, and the minimum functional versions based on the needs of third-party users.

The project structure is as follows. Each function is independent of one Module:

Because our modules are pure code and do not contain resource files, we provide them in the form of a jar package instead of an aar package. By the way, the default path of the generated aar package 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
Jar package Merging

From the project, we can see that our project contains multiple modules. The module of each basic function is compiled to generate a classes. jar. Therefore, the project will eventually generate a bunch of jar packages. When it is released, we need to provide a separate jar package. Therefore, we need to merge the jar packages. Unfortunately, Android Studio does not provide such a function. Therefore, you can only write a script to call the jar command. Open the command line terminal and input the jar command to print out the jar usage, 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 the archive directory-x extract the specified (or all) from the archive) file-u update existing archive file-v generate detailed output in standard output-f specify archive file name-m contain configuration information in the specified inventory file-e is bundled to the executable jar File the independent application specified application entry point-0 is stored only; no usage, any ZIP compression-M does not create a checklist file-I generates index information for the specified jar file-C changes to the specified directory and contains the files if there is any directory file, then it is recursively processed. The specified order of the configuration file name, archive file name, and entry point name is the same as that of the 'M', 'F', and 'E' tags. Example 1: Archive two class files to a file named classes. jar file: jar cvf classes. jar Foo. class Bar. class Example 2: Use the existing listing file 'mymanifest 'and archive all the files in the foo/directory to 'classes. jar ': jar cvfm classes. jar mymanifest-C foo /.

The jar command mainly implements two functions:

  • Decompress the class files of all jar packages to a directory.
  • Decompress all the class files to a separate jar package.

Because the jar command cannot specify the final output directory, we need to first cd a temporary directory for storing the decompressed class file, and then decompress all the jar packages in sequence, the decompress command is as follows:

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

After all the jar packages are decompressed, run the compression command to obtain a separate jar package:

jar -cvfM AndroidHyperion_${version}_debug.jar .
Automated construction of sub-modules

Automatic building consists of local building and Jenkins building. Local Building is mainly used for development and debugging. Jenkins building is mainly used for testing, product and other collection and running of Monkey.

Local build

The local build script file is located in build_local.sh under the project root directory. The main functions of this script include:

  • Call the gradlew command to execute gradle compilation to generate jar packages for each Module
  • Decompress the jar packages generated by each Module, and package the decompressed class files into a separate jar package.
  • The module packaging function controls the value of a Boolean variable.
  • The output directory is output.

The content of the build_local.sh file is as follows:

#! /Bin/sh # Use Gradle to compile each module. /gradlew clean. /gradlew build -- stacktrace -- debug # enter the output directory cd output # Clear the output directory rm-rf * # create the output subdirectory mkdir tempmkdir debugmkdir release # define the sdk version = "1.0.0" # define whether to package is_include_hfasynchttp = trueis_include_bitmapfun = trueis_include_hfjson = trueis_include_hflogger = true # omit others... # decompress the jar packages of all debug Versions to the cd tempif $ is_include_hfasynchttp; then jar-xvf .. /.. /hfasynchttp/build/intermediates/bundles/debug/classes. jarfiif $ is_include_bitmapfun; then jar-xvf .. /.. /hfbitmapfun/build/intermediates/bundles/debug/classes. jarfiif $ is_include_hfjson; then jar-xvf .. /.. /hfjson/build/intermediates/bundles/debug/classes. jarfiif $ is_include_hflogger; then jar-xvf .. /.. /hflogger/build/intermediates/bundles/debug/classes. jarfi # compress the class files of all debug Versions to an independent jar package. jar. # copy the file mv android02_$ {version} _ debug. jar .. /debug # Clear the temp directory rm-rf * # decompress the jar packages of all release versions to the temp directory if $ is_include_hfasynchttp; then jar-xvf .. /.. /hfasynchttp/build/intermediates/bundles/release/classes. jarfiif $ is_include_bitmapfun; then jar-xvf .. /.. /hfbitmapfun/build/intermediates/bundles/release/classes. jarfiif $ is_include_hfjson; then jar-xvf .. /.. /hfjson/build/intermediates/bundles/release/classes. jarfiif $ is_include_hflogger; then jar-xvf .. /.. /hflogger/build/intermediates/bundles/release/classes. jarfi # compress the class files of all release versions to a jar package. jar. # copy the file mv android02_$ {version} _ release. jar .. /release # Delete the temp directory cd .. rm-rf temp
Build Jenkins

The Jenkins compiling script file is located in build_jenkins.sh under the project root directory. The main functions of this script include:

  • Call the gradlew command to execute gradle compilation and generate the jar packages of each Moudle.
  • Decompress the jar packages generated by each Module, and package the decompressed class files into a separate jar package.
  • The module packaging function is controlled by parameters configured in Jenkins
  • The output directory is output.

As you can see, the only difference from local build is that the parametric build parameters of sub-modules are defined on Jenkins, rather than in local scripts. For the sake of completeness and clarity, we still paste the complete script file:

#! /Bin/sh. /gradlew clean. /gradlew build -- stacktrace -- debug # enter the output directory cd output # Clear the output directory rm-rf * # create the output subdirectory mkdir tempmkdir debugmkdir releasmcm temp # decompress the jar package of all debug Versions if $ is_include_hfasynchttp; then jar-xvf .. /.. /hfasynchttp/build/intermediates/bundles/debug/classes. jarfiif $ is_include_bitmapfun; then jar-xvf .. /.. /hfbitmapfun/build/intermediates/bundles/debug/classes. jarfiif $ is_include_hfjson; then jar-xvf .. /.. /hfjson/build/intermediates/bundles/debug/classes. jarfiif $ is_include_hflogger; then jar-xvf .. /.. /hflogger/build/intermediates/bundles/debug/classes. jarfi # compress the class files of all debug Versions to a jar package. jar. # Move the generated jar package to the debug directory mv android02_$ {version} _ debug. jar .. /debug # Clear the temp directory rm-rf * # decompress the jar package of all release versions if $ is_include_hfasynchttp; then jar-xvf .. /.. /hfasynchttp/build/intermediates/bundles/release/classes. jarfiif $ is_include_bitmapfun; then jar-xvf .. /.. /hfbitmapfun/build/intermediates/bundles/release/classes. jarfiif $ is_include_hfjson; then jar-xvf .. /.. /hfjson/build/intermediates/bundles/release/classes. jarfiif $ is_include_hflogger; then jar-xvf .. /.. /hflogger/build/intermediates/bundles/release/classes. jarfi # compress the class files of all release versions to a jar package. jar. # Move the generated jar package to the release directory mv android02_$ {version} _ release. jar .. /release # Delete the temp directory cd .. rm-rf temp
Parameter definitions for local and Jenkins parameterized build
Type Name Default Value Description
String Version 1.0.0 Epoch sdk version
Boolean Is_include_hfasynchttp True Whether to package hfasynchttp
Boolean Is_include_bitmapfun True Hfbitmapfun package?
Boolean Is_include_hfjson True Whether to package hfjson
Boolean Is_include_hflogger True Whether to package hflogger

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.