Return to Video

Resource Merging

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

more » « less
Video Language:
English
Team:
Udacity
Project:
UD867 - Gradle
Duration:
02:10

English subtitles

Revisions Compare revisions