English subtitles

← Resource Merging

Get Embed Code
4 Languages

Showing Revision 53 created 05/25/2016 by Udacity Robot.

  1. We've learned how to use build types
    to customize Gradle's behavior.
  2. So now let's look at how build variants
    allow you to control the behavior of
  3. your app itself.
  4. And depending on which
    app we're building,
  5. the Android Gradle plugin creates
    a whole slew of source sets.
  6. And depending on which variant you're
    building, it will merge the sources and
  7. resources from those source
    sets into the final APK.
  8. At the most broad level,
  9. there's the main source set
    which is located in source main.
  10. This is where we've been
    putting all of our code so far.
  11. Additionally, there's a source set for
    each product flavor.
  12. Assuming our example from before,
    let's say we have a free and
  13. a paid product flavor.
  14. There's also a source set for each build
    type, in this case debug and release.
  15. Finally there's also a source for
    each final variant.
  16. So that's source free debug, free
    release, paid debug, and paid release.
  17. If we have sources and resources
    that we need in a paid flavor but
  18. not in the free flavor,
    we can put them in source paid.
  19. Similarly, if there are resources
    we need for debug build but not for
  20. the release build,
    we can put them in source debug.
  21. Finally, if we have resources
    that are needed only for
  22. the paid debug variant, then we can
    put them in the source paid debug.
  23. When we build a particular variant,
    Gradle will take care of all
  24. the emerging sources and
    resources we need for that variant.
  25. It will also take care of a variety of
    resources that are defined in more than
  26. one configuration.
  27. The rule is that the more
    specific configurations
  28. override the less
    specific configurations.
  29. Java's source files
    cannot be overwritten, so
  30. you need to be careful that you don't
    try to define the same class definition,
  31. and it ends up in multiple variants.
  32. Gradle can do a bit better for
    resource files like strings.xml, or
  33. even Android Manifests.
  34. For resources of that type,
    the files will be merged, and
  35. the entries will be overridden by ID.
  36. To determine what sources and resources
    are included when we build a particular
  37. variant, we can draw a diagram
    that looks like this.
  38. We start in the middle.
  39. If we want to build the paid debug
    variant, Gradle will include everything
  40. in main, then merge everything in paid,
    overriding any conflicts.
  41. Next Gradle will merge in everything
    from debug overriding again.
  42. Then, finally,
    Gradle will merge in the resources and
  43. source that are in paid debug.