A Gradle Story

  • View
    519

  • Download
    0

Embed Size (px)

Text of A Gradle Story

  • A Gradle StoryEduardo Bonet

  • $ whoamiBonet

    Control and Automation Engineer

    Master Student in Computer Science

    Full Stack / Data Scientist Jr

    Android Hobbyist

  • Agenda What is Gradle?

    Build Types and Variables

    Flavors

    Cool Tasks

  • Why Gradle?Building and packaging an android app is complicated

    Gradle is a build tool powered by Groovy

    You CODE configurations

    It's magical!

  • Introducing John

    John is a CS student, and his dad has a bakery. He made an app for his dad's bakery and wants to publish it.

  • John's First Problem

    John tested his api calls on his local server during development. He needs to change his client to point to the new production server.

  • Error Pro

    ne!

  • Gradle Build Variables

  • John can improve it by moving the variable to build.config

    build.gradle

  • BuildConfig already contains lots of goodies

  • Installing different versions of the app

    build.gradle

  • Installing different versions of the app

  • BakeryAPP Identity Crisis

    John will now split the app into two versions: free and premium. Should he create a new app and copy/paste code? How will that be maintained, what about new features?

  • Vanilla or Chocolate? Product Flavors

  • Flavors - Multiple app versions

    build.gradle

  • Flavors - Multiple app versions

    src/ free /java/res/values/strings.xml

    src/ pro /java/res/values/strings.xml

  • Not instead. Flavors WITH BuildTypes! They can be combined!

    Why Flavors instead of BuildTypes?

    Build Type

    Flavor Debug Homolog Release

    Free freeDebug freeHomolog freeRelease

    Pro proDebug proHomolog proRelease

  • Customizing Flavors and BuildTypes| -- src| | --- test (java, res, assets)| | --- main (java, res, assets)| | --- free (java, res, assets)| | --- pro (java, res, assets)| | --- debug (java, res, assets)| | --- freeDebug (java, res, assets)

    Priority OrderflavorBuild > flavor > build > main

  • John now for some reason wants to add different behavior to the flavors: all cakes for free version are stored in memory, while only the pro version queries the API.

  • src/ pro /java/johnsdadbakery/AwesomeCakeRepository.java

  • src/ free /java/johnsdadbakery/AwesomeCakeRepository.java

  • Example: Specific Code with DIThe D on SOLID! This is where Dependency Injection shines. John first abstracts the repo and builder into an interface, in the main source set.

    src/java/ main /johnsdadbakery/ AwesomeCakeRepository.java

  • src/java/main/johnsdadbakery/ LocalCakeRepoBuilder.java

  • src/java/ main /johnsdadbakery/ RetrofitCakeRepoBuilder.java

  • Example: Specific Code with DIOur injector interface will helps us configure flavor specific behaviour

    src/java/main/johnsdadbakery/ InjectorInterface.java

  • Example: Specific Code with DIFinally, we just need to implement the Injector interface on each flavor:

    src/java/ free /johnsdadbakery/Injector.java

  • Example: Specific Code with DIFinally, we just need to implement the Injector interface on each flavor:

    src/java/ pro /johnsdadbakery/Injector.java

  • Example: Specific Code with DINow we simply ask the injector for the correct CakeRepo Implementation

    This is a very naive DI implementation, consider using Dagger2, it is way more powerful

  • John is tired of typing his Keystore credentials

    John hates typing password every time he creates a release version. Android Studio helps with that, but how a CI server would handle it?

  • Gradle, do the thing!Signing Configs

  • PASSWO

    RD ON RE

    PO

  • build.gradle

    signing.props

    Signing Config - Better

  • Lost in screenshots

    John translated his app to three different languages. And has support for multiple screens. That means every time he publishes a new release he has to generate a LOT of screenshots for the PlayStore.

  • Create screenshots with Spoon https://github.com/stanfy/spoon-gradle-plugin Frame it with https://github.com/chemouna/frame-gradle-plugin Ta-da!

    Another way: http://flavienlaurent.com/blog/2014/12/05/screenshot_automation/

    Screenshot ALL THE THINGS

    https://github.com/stanfy/spoon-gradle-pluginhttps://github.com/chemouna/frame-gradle-pluginhttp://flavienlaurent.com/blog/2014/12/05/screenshot_automation/

  • Screenshot ALL THE THINGS

  • Gradle make a SandwichCool Gradle Plugins

  • Am I Pretty? CheckStylehttp://blog.jessitron.com/2012/07/using-checkstyle-in-gradle.html

    http://checkstyle.sourceforge.net/config.html

    http://www.slideshare.net/marcprengemann/better-code-through-lint-and-checkstyle

    http://blog.jessitron.com/2012/07/using-checkstyle-in-gradle.htmlhttp://blog.jessitron.com/2012/07/using-checkstyle-in-gradle.htmlhttp://checkstyle.sourceforge.net/config.htmlhttp://checkstyle.sourceforge.net/config.htmlhttp://www.slideshare.net/marcprengemann/better-code-through-lint-and-checkstylehttp://www.slideshare.net/marcprengemann/better-code-through-lint-and-checkstylehttp://www.slideshare.net/marcprengemann/better-code-through-lint-and-checkstyle

  • OMG Google Play Services! - Dexcount https://github.com/mihaip/dex-method-counts https://github.com/KeepSafe/dexcount-gradle-plugin

    https://github.com/mihaip/dex-method-countshttps://github.com/mihaip/dex-method-countshttps://github.com/KeepSafe/dexcount-gradle-pluginhttps://github.com/KeepSafe/dexcount-gradle-plugin

  • Retrolambda - A great release task

    https://github.com/evant/gradle-retrolambdahttps://github.com/evant/gradle-retrolambda

  • And much more! Slack: https://github.com/Mindera/gradle-slack-plugin Upload to GooglePlay: https://github.com/Triple-T/gradle-play-publisher Git: https://github.com/ajoberstar/gradle-git Upload your lib to maven repos

    ..

    Turn on your coffee machine: Not done yet :) Etc etc etc

    https://github.com/Mindera/gradle-slack-pluginhttps://github.com/Triple-T/gradle-play-publisherhttps://github.com/ajoberstar/gradle-git

  • What did John learn today?

    Gradle is a build tool written that runs on Groovy, sky is the limit

    Know your build tool, it will help you a lot

    Use Build Types to configure Environments

    Use Flavors to create App Versions

    Great Plugins out there to make you more productive and happy!

  • See also http://blog.brainattica.com/how-to-work-with-flavours-on-android/

    http://tools.android.com/tech-docs/new-build-system/user-guide

    http://gradle.org/

    http://gradleplease.appspot.com/

    http://blog.brainattica.com/how-to-work-with-flavours-on-android/http://blog.brainattica.com/how-to-work-with-flavours-on-android/http://tools.android.com/tech-docs/new-build-system/user-guidehttp://tools.android.com/tech-docs/new-build-system/user-guidehttp://gradle.org/http://gradle.org/http://gradleplease.appspot.com/http://gradleplease.appspot.com/

  • Thank you

    github.com/ebonetlinkedin.com/in/eduardobonet

    http://github.com/ebonethttp://github.com/ebonethttps://www.linkedin.com/in/eduardobonethttps://www.linkedin.com/in/eduardobonet