Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build automation with Gradle by Viktor Kifer

Build automation with Gradle by Viktor Kifer

Build automation with Gradle by Viktor Kifer

GDG Ternopil

October 08, 2015
Tweet

More Decks by GDG Ternopil

Other Decks in Programming

Transcript

  1. Automate It! Gradle is Open Source Build Automation Tool which

    natively supports Java/Android/C/C++ builds. Many other languages are supported with plugins. Advantages: - cross platform builds - DevOps Lifecycle integration - Easy dependency management - Programmable builds - High Performance - Build Reports
  2. Let’s start with git git init # initializes git repository

    # create .gitignore git add . # adds changes in local repository to stage git commit -m “Initial commit” # commits staged changes git tag v0.0.0 # signes last commit with label “v0.0.0”
  3. Useful git options git checkout v0.0.0 # checkout code to

    specified tag git tag --list # list all tags git describe --tags # shows last tag (and last commit if has changes) git push <origin> --tags # pushes tags to remote repository
  4. Get app versionCode from git // returns current app version

    code based on number of tags in git // returns -1 if an error occurred def getVersionCodeFromGitTagCount = { -> try { def code = new ByteArrayOutputStream() exec { commandLine 'git', 'tag', '--list' standardOutput = code } return code.toString().split("\n").size() } catch (ignored) { return -1; } }
  5. Get app versionName from git // returns current app version

    name based on last tag in git // returns undefined if an error occurred def getVersionNameFromLastGitTag = { -> try { def stdout = new ByteArrayOutputStream() exec { commandLine 'git', 'describe', '--tags' standardOutput = stdout } return stdout.toString().replaceAll("^v", "").trim() } catch (ignored) { return "undefined"; } }
  6. Usage of those functions android { //... defaultConfig { ...

    versionCode getVersionCodeFromGitTagCount() versionName getVersionNameFromLastGitTag() //... } }
  7. Setting apk name format android { applicationVariants.all { variant ->

    def date = new Date(); def formattedDate = date.format('yyyyMMdd') if (variant.name == "release") { variant.outputs.each { output -> output.outputFile = new File( output.outputFile.parent, "${formattedDate}-MyApp-${variant.versionName}.apk") } } } //... }
  8. Setting mapping file name format android { applicationVariants.all { variant

    -> def date = new Date(); def formattedDate = date.format('yyyyMMdd') variant.outputs.each { output -> if (variant.getBuildType().isMinifyEnabled()) { variant.assemble.doLast { copy { from variant.mappingFile into output.outputFile.parent rename { String fileName -> "${formattedDate}-mapping-${variant.versionName}.txt" } }}}}} //... }
  9. Manage Code Quality SonarQube is an open platform to manage

    code quality. It covers the 7 axes of code quality
  10. Installation # install database sudo apt-get -y install mysql-server mysql-client

    # download and install sonarqube sudo sh -c 'echo deb http://downloads.sourceforge.net/project/sonar-pkg/deb binary/ > /etc/apt/sources.list.d/sonarqube.list' sudo apt-get update sudo apt-get -y --force-yes install sonar sudo update-rc.d sonar defaults # mysql -u root -p mysql > create schema sonar DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; mysql > create user 'sonar'@'%' Identified By 'sonar'; mysql > grant all privileges on sonar.* to sonar; # change configuration in /opt/sonar/conf/sonar.properties (uncomment): sonar.jdbc.username=sonar sonar.jdbc.password=sonar sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar? useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
  11. Back to gradle apply plugin: 'sonar-runner' android { //... sonarRunner

    { sonarProperties { property "sonar.host.url", SONAR_URL property "sonar.jdbc.url", SONAR_DB_URL property "sonar.jdbc.driverClassName", SONAR_DB_DRIVER property "sonar.jdbc.username", SONAR_USER property "sonar.jdbc.password", SONAR_PASSWORD property "sonar.language", "java" property "sonar.sources", "src" property "sonar.sourceEncoding", "UTF-8" property "sonar.projectKey", android.defaultConfig.applicationId property "sonar.projectName", "My App Name" property "sonar.projectVersion", android.defaultConfig.versionName property "sonar.exclusions", "**Test.java,**IT.java,**Stub.java" } } //... }