How is Android Client Gradle packaged

  • 2021-01-25 07:54:26
  • OfStack

1. Introduction

As android client development came to an end, my colleague in charge of SEO suddenly sent me a sad news involving 45 release channels. The previous working mode of only releasing its own channels (manually modifying parameters for packaging) was no longer satisfied with the demand, so the recently popular gradle packaging technology was introduced.

gradle is based on groovy and was introduced to facilitate the migration from the current eclipse development environment to Android Studio, so blablabla... Without further ado, dry goods first.

2. Preparation

1. First, if using eclipse as the development environment, you need to right-click on the project and select "Export..." from the menu.
2. Then, select "Android-" from the list that pops up > Generate Gradle build files"
3. Select "Next" > "
4. Select "Next "again > "
5. Check the list of items in which you want to create the gradle configuration script and continue with "Next" > "
gradle configuration script has been generated in eclipse. It may be necessary to right-click refresh the project

3. Simple project Gradle package script configuration


buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
// Project configuration version number, and apk The output directory 
ext.appVersionCode = 2
ext.appVersionName = "2.0"
ext.appReleaseDir = "/Users/freedoms/Desktop/release"
apply plugin: 'com.android.application'
// Get timestamp 
def getDate() {
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
return formattedDate
}
// Package configuration 
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
   // The project contains so Package, need to be added jni Associated directory configuration, otherwise the program runs to the call so An error will occur when 
jniLibs.srcDir(['libs']) 
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
// When packing starts, lint If an exception occurs, the warning will be ignored and the script will continue to run. If it is ignored, the packaging process will be terminated 
lintOptions { 
  abortOnError false
} 
  // Signature configuration 
signingConfigs {
myConfig {
storeFile file("/Users/freedoms/Desktop/ Product Requirements Document /android.keystore")
storePassword "123123"
keyAlias "android.keystore"
keyPassword "123123"
}
}
 // Confuse configuration 
buildTypes{
release {
signingConfig signingConfigs.myConfig          // Don't confuse setting to false
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
  // Custom configuration 
productFlavors {
// baidu 
baidu{          //AndroidManifest.xml The placeholder completion configuration defined in the configuration, in the example AndroidManifest.xml In the configuration "${UMENG_CHANNEL_VALUE}", After the script runs, it will baidu_android_channel Automatically replace to this position, AndroidManifest.xml Other parameters that need to be dynamically configured by channel can refer to this practice 
manifestPlaceholders = [UMENG_CHANNEL_VALUE:"baidu_android_channel",BAIDU_CHANNEL_VALUE:"baidu_baidu_android_channel"]
}
//360
"360"{// If the channel name is configured to begin with a number, it must be enclosed in quotes 
manifestPlaceholders = [UMENG_CHANNEL_VALUE:"360_android_channel",BAIDU_CHANNEL_VALUE:"baidu_360_android_channel"]
}       // It can be expanded by referring to the above two items... 
}

   // Package rename algorithm, output XXX_Android_v2.0_20160127_baidu.apk To the output directory configured before the script 
android.applicationVariants.all { variant ->
def file = variant.outputFile
if(variant.buildType.name.equals('release')){
variant.outputFile = new File(appReleaseDir + '/','XXX_Android_v' + appVersionName +getDate()+ '_' + variant.productFlavors[0].name + '.apk')
}
}
}
// Encoding configuration 
tasks.withType(Compile) { 
options.encoding = "UTF-8" 
}

4. gradle package script configuration with project dependencies

1. Depend on projects

a) If you are using eclipse as your development environment, you first need to generate gradle configuration scripts (see 2 for details). Preparation work)
b) modify the apply plugin in the gradle script to the following configuration

apply plugin: 'android-library'

ES68en) for the same configuration as the main project, refer to the simple project ES69en script configuration

2. Main project

a) If you use eclipse as your development environment, you first need to generate gradle configuration scripts (see 2 for details). Preparation work)

b) modify the apply plugin in the gradle script to the following configuration

apply plugin: 'com.android.application'

c) create an setting.gradle text file in the main project root directory to use as a reference to the dependent project configuration


// Introduce the dependent project name include 'library'
include 'library_pullToRefresh'
// Creates a directory reference with the absolute path to which the dependent item resides in quotes at the end 
project(':library').projectDir = new File('/Users/freedoms/git/library') 
project(':library_pullToRefresh').projectDir = new File('/Users/freedoms/git/library_pullToRefresh') 

d) Add the following configuration to the main project build.gradle


// Depend on the configuration 
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':library_pullToRefresh') 
compile project(':library')
}

5. Run the build script

1, From the command line, cd to the main project root directory
2, Enter gradle clean (clean gradle generated check file and type APK, can not do, but do not guarantee that there will be any strange problems in the middle, develop a good habit)
3. Enter gradle check for execution (check the project, the time is different according to the number of channels)
4, Enter gradle build execution (execute the build script and start packing, depending on the number of channels, it will take more than 1 hour for 45 channels)
5. Check the output directory of the main project ES123en.gradle configuration to see that the channel package is already in it

6. Matters needing attention (to be continued)

Q1: The following error may be reported when performing check or build


FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':lint'.
> Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
...
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED 
  A1: in build.gradle Add to the script 
lintOptions {
abortOnError false
}

Q2: Depending on the number of channels, it may be reported that the java virtual machine is out of memory during packaging


The system is out of resources.
Consult the following stack trace for details.
java.lang.OutOfMemoryError: Java heap space
at com.sun.tools.javac.util.Position$LineMapImpl.build(Position.java:139)
at com.sun.tools.javac.util.Position.makeLineMap(Position.java:63)
at com.sun.tools.javadoc.DocCommentScanner.getLineMap(DocCommentScanner.java:438)
at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:512)
at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:550)
at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:804)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
at com.sun.tools.javac.main.Main.compile(Main.java:353)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:115)

The above is the site to share Android client program Gradle how to package the relevant knowledge, I hope to help you.


Related articles: