Android uses Jenkins for continuous integration and automatic packaging

Keywords: Android Mac Gradle Junit

1, Using shell to package apk automatically

Command to call gradlew under android project root directory to complete packaging

Create a new Android build.sh command file, and the contents of the configuration file are as follows

#!/bin/bash 
	#The directory where the version.properties file is located
	path='/Users/mac/androidProject/TestMaster'
	content=$(cat ${path}/version.properties)
	echo "Read content:$content"
	#Read the value corresponding to version "code" of the file, and save the version code variable
	versionCode=`grep VERSION_CODE ${path}version.properties|cut -d'=' -f2`
	#Add versionCode+1 to get the accumulated addVersionCode
	addVersionCode=`expr $versionCode + 1`
	echo "versionCode====$versionCode"
	# Add the addVersionCode to the version? Code of the file
	sed -i "s#^VERSION_CODE=.*#VERSION_CODE=${addVersionCode}#g"  ${path}version.properties
	content=$(cat ${path}version.properties)
	addVersionCode=`grep VERSION_CODE ${path}version.properties|cut -d'=' -f2`
	echo "After replacement====$content"
	echo "addVersionCode====$addVersionCode"
	#Judge whether the versionCode is accumulated successfully. If the - gt ID is greater than the return value, true will be returned
	if [ $addVersionCode -gt $versionCode ]
	then
	# Pack apk
	#Corresponding directory of gradlew
	BUILD_TOOL_PATH='/Users/mac/androidProject/TestMaster'
	echo "Start packing..."
	#cd $BUILD_TOOL_PATH && ./gradlew assembleinsectRelease
	 cd $BUILD_TOOL_PATH && ./gradlew assembleDebug
	openRootPath='/Users/mac/androidProject/TestMaster/app'
	#After packaging, open the directory where the package is located. Of course, please replace the absolute path in the program with your corresponding path, otherwise the program will not run successfully
	explorer $openRootPath'\build\outputs\apk'
	else
	echo "error : versionCode Not added 1"
	fi

Among them, you need to package the debug version and configure gradlew assemblydebug / assemblyinsectdebug,

To package the release version, you need to configure gradlew assemblyrelease / assemblyinsectrelease

When using, cd to the AndroidBuild.sh folder, and use the command sh AndroidBuild.sh

2, Gradle configuration

First, you need to create a new version code configuration file in the root directory to record the version number of the package. For example, you need to create a new version.properties file with the content VERSION_CODE=1

Configure the Gradle file of the main Module. The file contents can be simply configured as follows

apply plugin: 'com.android.application'

def getVersionCode() {
    def versionFile = file('../version.properties')
    if (versionFile.canRead()) {
        def Properties versionProps = new Properties()
        versionProps.load(new FileInputStream(versionFile))
        def versionCode = versionProps['VERSION_CODE'].toInteger()
        def runTasks = gradle.startParameter.taskNames //Only when the assemblyrelease task is to increase the version number, other channel packages are configured here separately
//        if ('assembleDebug' in runTasks|'assembleinsectDebug' in runTasks) {
            versionProps['VERSION_CODE'] = (++versionCode).toString()
            versionProps.store(versionFile.newWriter(), null)
//        }
        return versionCode
    } else {
        throw new GradleException("Could not find version.properties!")
    }
}
android {
    def currentVersionCode = getVersionCode()
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.liuxingyu.testmaster"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode currentVersionCode
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        release {
            storeFile file('/Users/mac/androidProject/TestMaster/app/release.keystore') //Absolute path
            storePassword "jure123456"
            keyAlias "key0"
            keyPassword "jure123456"
        }
    }
    buildTypes {
        debug {
            minifyEnabled false
            shrinkResources false
            zipAlignEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            zipAlignEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

2, Continuous integration with Jenkins

To be continued...

Posted by xxxzom_biexxx on Mon, 06 Jan 2020 14:32:48 -0800