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

Building Android Applications with Gradle

Building Android Applications with Gradle

Benefits and drawbacks of using Gradle in Android in comparison to other build systems such as Ant, Maven or Buck.

Hugo Doménech Juárez

March 12, 2014
Tweet

More Decks by Hugo Doménech Juárez

Other Decks in Programming

Transcript

  1. “Ant supplies a number of built-in tasks allowing to compile,

    assemble, test and run Java applications.” Using Ant –ant.apache.org 4
  2. “Maven can manage a project's build, reporting and documentation from

    a central piece of information.” Using Maven –maven.apache.org 5
  3. “Gradle combines the power and flexibility of Ant with the

    dependency management and conventions of Maven into a more effective way to build.” Using Gradle –gradle.org 6
  4. “Buck is a build system for Java and Android that

    encourages the creation of small, reusable modules consisting of code and resources.” Using Buck –facebook.github.io/buck/ 7
  5. Creating Android Applications android create project --target 25 --path .

    --package com.project --activity MainActivity 10
  6. Creating Android Applications For both the apk and aar: -

    AndroidManifest.xml - classes.jar - res - R.java 11
  7. 13

  8. Step 1: Generate Resource java code and packaged Resources aapt

    package -f -M ${manifest.file} -F ${packaged.resource.file} -I $ {path.to.android-jar.library} -S ${android-resource-directory} [-m -J $ {folder.to.output.the.R.java}] Step 2: Compile java source codes + R.java use javac Step 3: Convert classes to Dalvik bytecode dx.bat –dex –output=${output.dex.file} ${compiled.classes.directory} $ {jar files..} Step 4: Create unsigned APK apkbuilder ${output.apk.file} -u -z ${packagedresource.file} -f ${dex.file} 14
  9. With Gradle buildscript { repositories { mavenCentral() } dependencies {

    classpath 'com.android.tools.build:gradle:0.9.+' } } apply plugin: 'android' android { compileSdkVersion 19 buildToolsVersion "19.0.3" } 15
  10. Gradle benefits • Integration with Android Studio • Simple, Declarative,

    Domain-specific Language - Groovy • A Single Build System • Product Flavors, Build Variants, and Build Type • Android Signing Configuration • Dependency Management • Multi-project Support • Binary Bundles for Libaries (.aar) • Full Incremental Builds • A Focus on Testing • Test Server API supports Hosted Testing - Integration with Jenkins-based build servers and services from AppThwack, TestDroid, and Manymo 16
  11. Gradle bonuses • Android’s official build tool: after 1.0 though!

    • Gradle has frequent releases demonstrating significant feature development - The Gradle project has a six week release cycle and practices complete transparency for feature planning. On the other hand, Maven had two releases in 2012 and one release in 2013. Gradle has momentum, we're moving very quickly to expand support for other languages while Maven stands still • Gradle provides more natural support for multiple languages and platforms - Scala, Groovy, C/C+ +, and Android first class plugins. 17
  12. Gradle drawbacks • No official support for Unit Testing: through

    plugins (Novoda, JakeWharton) • No support for debugging in testing • No maven archetypes 18
  13. Gradle todos • run test coverage using emma (standard tests):

    • run test coverage using cobertura (robolectric) • run test coverage using jacoco (both tests): • play uiautomator tests • play monkey runner tests 19
  14. Wrapper - No need to have Gradle installed - Everyone

    builds with the same version task wrapper(type: Wrapper) { gradleVersion = '1.11' } 21
  15. Consistency in the tools buildscript { repositories { mavenCentral() }

    dependencies { classpath 'com.android.tools.build:gradle:0.9.+' } } android { buildToolsVersion "19.0.3" } 22
  16. Flexible android { sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs

    = [‘src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } androidTest.setRoot('tests') } } // NOTE: several srcDirs allowed! 23
  17. Tests • check • lint • connectedCheck • connectedAndroidTest •

    connectedUiAutomatorTest (not implemented yet) • deviceCheck • This depends on tasks created when other plugins implement test extension points. 26
  18. Dependencies dependencies { compile project(‘:libraries:NetworkStack') compile files('libs/crittercism_v3_0_8_sdkonly.jar') compile ‘com.actionbarsherlock:actionbarsherlock:4.4.0@aar’ internalReleaseCompile

    ‘net.hockeyapp.android:HockeySDK:3.0.1' testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT' androidTestCompile ‘com.google.guava:guava:11.0.2' } 27
  19. Configurability • versionCode • versionName • minSdkVersion • targetSdkVersion •

    packageName • TestPackageName • testInstrumentationRunner • signingConfig • proguardFile • proguardFiles 30
  20. References • developer.android.com/ • tools.android.com/ • gradle.org • maven.apache.org/ •

    ant.apache.org • facebook.github.io/buck/ • asantoso.wordpress.com/2009/09/15/how-to-build-android- application-package-apk-from-the-command-line-using-the-sdk- tools-continuously-integrated-using-cruisecontrol/ 33