-
タイトル:
Resource Merging
-
概説:
-
We've learned how to use build types
to customize Gradle's behavior.
-
So now let's look at how build variants
allow you to control the behavior of
-
your app itself.
-
And depending on which
app we're building,
-
the Android Gradle plugin creates
a whole slew of source sets.
-
And depending on which variant you're
building, it will merge the sources and
-
resources from those source
sets into the final APK.
-
At the most broad level,
-
there's the main source set
which is located in source main.
-
This is where we've been
putting all of our code so far.
-
Additionally, there's a source set for
each product flavor.
-
Assuming our example from before,
let's say we have a free and
-
a paid product flavor.
-
There's also a source set for each build
type, in this case debug and release.
-
Finally there's also a source for
each final variant.
-
So that's source free debug, free
release, paid debug, and paid release.
-
If we have sources and resources
that we need in a paid flavor but
-
not in the free flavor,
we can put them in source paid.
-
Similarly, if there are resources
we need for debug build but not for
-
the release build,
we can put them in source debug.
-
Finally, if we have resources
that are needed only for
-
the paid debug variant, then we can
put them in the source paid debug.
-
When we build a particular variant,
Gradle will take care of all
-
the emerging sources and
resources we need for that variant.
-
It will also take care of a variety of
resources that are defined in more than
-
one configuration.
-
The rule is that the more
specific configurations
-
override the less
specific configurations.
-
Java's source files
cannot be overwritten, so
-
you need to be careful that you don't
try to define the same class definition,
-
and it ends up in multiple variants.
-
Gradle can do a bit better for
resource files like strings.xml, or
-
even Android Manifests.
-
For resources of that type,
the files will be merged, and
-
the entries will be overridden by ID.
-
To determine what sources and resources
are included when we build a particular
-
variant, we can draw a diagram
that looks like this.
-
We start in the middle.
-
If we want to build the paid debug
variant, Gradle will include everything
-
in main, then merge everything in paid,
overriding any conflicts.
-
Next Gradle will merge in everything
from debug overriding again.
-
Then, finally,
Gradle will merge in the resources and
-
source that are in paid debug.