Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

A Gradle Story


Published on

Build Tools are important. Really important. If you are an Android Developer, understanding Gradle and how to harness its power will increase your productivity and make your life easier in many possible ways.

This talk was presented at The Developer's Conference Floripa 2016, on the Android Track

Published in: Technology
  • Be the first to comment

A Gradle Story

  1. 1. A Gradle Story Eduardo Bonet
  2. 2. $ whoami Bonet Control and Automation Engineer Master Student in Computer Science Full Stack / Data Scientist Jr Android Hobbyist
  3. 3. Agenda ● What is Gradle? ● Build Types and Variables ● Flavors ● Cool Tasks
  4. 4. Why Gradle? Building and packaging an android app is complicated Gradle is a build tool powered by Groovy You CODE configurations It's magical!
  5. 5. 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.
  6. 6. 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.
  7. 7. Error Prone!
  8. 8. Gradle Build Variables
  9. 9. John can improve it by moving the variable to build.config build.gradle
  10. 10. BuildConfig already contains lots of goodies
  11. 11. Installing different versions of the app build.gradle
  12. 12. Installing different versions of the app
  13. 13. 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?
  14. 14. Vanilla or Chocolate? Product Flavors
  15. 15. Flavors - Multiple app versions build.gradle
  16. 16. Flavors - Multiple app versions src/ free /java/res/values/strings.xml src/ pro /java/res/values/strings.xml
  17. 17. 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
  18. 18. 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 Order flavorBuild > flavor > build > main
  19. 19. 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.
  20. 20. src/ pro /java/johnsdadbakery/
  21. 21. src/ free /java/johnsdadbakery/
  22. 22. Example: Specific Code with DI The 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/
  23. 23. src/java/main/johnsdadbakery/
  24. 24. src/java/ main /johnsdadbakery/
  25. 25. Example: Specific Code with DI Our injector interface will helps us configure flavor specific behaviour src/java/main/johnsdadbakery/
  26. 26. Example: Specific Code with DI Finally, we just need to implement the Injector interface on each flavor: src/java/ free /johnsdadbakery/
  27. 27. Example: Specific Code with DI Finally, we just need to implement the Injector interface on each flavor: src/java/ pro /johnsdadbakery/
  28. 28. Example: Specific Code with DI Now 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
  29. 29. 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?
  30. 30. Gradle, do the thing! Signing Configs
  32. 32. build.gradle signing.props Signing Config - Better
  33. 33. 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.
  34. 34. ● Create screenshots with Spoon ● Frame it with ● Ta-da! Another way: Screenshot ALL THE THINGS
  35. 35. Screenshot ALL THE THINGS
  36. 36. Gradle make a Sandwich Cool Gradle Plugins
  37. 37. Am I Pretty? CheckStyle checkstyle
  38. 38. OMG Google Play Services! - Dexcount ● ●
  39. 39. Retrolambda - A great release task
  40. 40. And much more! ● Slack: ● Upload to GooglePlay: ● Git: ● Upload your lib to maven repos ….. ● Turn on your coffee machine: Not done yet :) ● Etc etc etc
  41. 41. 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!
  42. 42. See also ● ● ● ●
  43. 43. Thank you