Dependency-Aware Build
Variants in Android Studio 3.0
Stefan Martynkiw
Android Studio 3.0 Introduction
• Introduced at Google I/O 2017
• Improvements:
• Faster Gradle build times
• Profiling tools
• UI
• Network Request Inspection (with Retrofit & OkHTTP)
• Device File Explorer
• App-Private files too! (no more adb run-as!)
• Android Gradle Plugin Improvements
Application Structure and Dependencies
• Applications in Android Studio
can be structured into modules
• Drivewyze Example:
• Agatha (Mobile app)
• Gaby (Stripped down for Android
ELDs)
• Erica (Stripped down for even
older Android ELDs)
• Common
• “Magic Sauce”
Agatha Erica Gaby
Common
Why have variants?
• Production
• Signing Keys, Production
Environment
• QA
• Unsigned, QA test environment
• Development
• Unsigned, Local-host server
Agatha Erica Gaby
Common
Set Theory and the Cross Product
• {A, B, C}
• {A, B, C} ⋈ {D, E, F} =
• AD, AE, AF
• BD, BE, BF
• CD, CE, CF
• {A, B, C} ⋈ {D, E, F} ⋈ {Y, Z} =
• ADY, AEY, AFY
• BDY, BEY, BFY
• CDY, CEY, CFY
• ADZ, AEZ, AFZ
• BDZ, BEZ BFZ
• CDZ, CEZ, CFZ
Abstract  Real
• “Build type”:
• {Debug, Release, Simulated
Location}
• “Authorization Service / Server
Environment”:
• {Production, QA, Local}
• “Event cache”:
• {Production, QA, Local}
Product Flavors & Build Types & Build Variants
• Build Types
• Sets up various build properties.
• The debug build type enables debug options and signs
the APK with the debug key.
• The release build type may shrink, obfuscate, and sign
your APK with a release key for distribution.
• Android Studio creates the debug and release build
types by default.
• Product Flavors
• Product flavors represent different versions of your app
that you may release to users
• Free and paid versions of your app.
• You can customize product flavors to use different code
and resources, while sharing and reusing the parts that
are common to all versions of your app.
• Product flavors are optional and you must create them
manually.
• More info:
• https://developer.android.com/studio/build/index.html
• Build Variants
• The resulting entries in the set formed by cross-product
of BuildTypes x ProductFlavors
freeFlavor paidFlavor
debug freeDebug paidDebug
release freeRelease paidRelease
The Old Way
• “Build Type” did not propagate
to dependent modules
• Really old bug in Gradle
• Reported March, 2013
• https://issuetracker.google.com/issu
es/36967265
• “DESCRIPTION:
Gradle plugin does not propagate
whether you are doing a debug
build or a release build to a
dependent android library”
Agatha Build.conf:
buildTypes {
release {…}
debug {…}
simloc {…}
}
productFlavors {
prodFlavor {…}
qaFlavor {…}
devFlavor {…}
}
Dependencies{ …
simlocCompile project(path: ':common', configuration: 'simlocMongoQa')
debugCompile project(path: ':common', configuration: 'libraryMongoQa')
releaseCompile project(path: ':common', configuration: 'libraryMongoProd')}
Common Build.conf:
buildTypes {
mongoDev { … }
mongoQa { … }
mongoProd { … }
}
productFlavors {
library {
buildConfigField 'boolean', 'SIMLOC_BUILD', "false"
}
simloc {
buildConfigField 'boolean', 'SIMLOC_BUILD', "true"
}
}
Flavor Dimensions
• Flavor Dimensions add another
cross product.
• {A, B, C} ⋈ {D, E, F} ⋈ {Y, Z} =
• ADY, AEY, AFY
• BDY, BEY, BFY
• CDY, CEY, CFY
• ADZ, AEZ, AFZ
• BDZ, BEZ BFZ
• CDZ, CEZ, CFZ
• https://www.youtube.com/watc
h?v=daYl7edb6S0
Declaring Flavor Dimensions
• //Todo finish slide:
• https://developer.android.com/s
tudio/build/build-
variants.html#flavor-dimensions
The New Way Common: Build.conf
buildTypes {
debug{
debuggable true
buildConfigField 'boolean', 'SIMLOC_BUILD', "false"
}
release {
debuggable false
buildConfigField 'boolean', 'SIMLOC_BUILD', "false"
}
simloc {
debuggable true
buildConfigField 'boolean', 'SIMLOC_BUILD', "true"
}
}
flavorDimensions "mongo"
productFlavors {
mongoDevFlavor {
dimension "mongo"
buildConfigField 'String', 'DRIVE_EVENTREPL_URI', '”…"'
}
mongoQaFlavor {
dimension "mongo"
buildConfigField 'String', 'DRIVE_EVENTREPL_URI', '”…"'
}
mongoProdFlavor {
dimension "mongo"
buildConfigField 'String', 'DRIVE_EVENTREPL_URI', '"…"'
}
}
Agatha2: Build.conf
flavorDimensions "dsp", "mongo"
productFlavors {
prodFlavor {
dimension "dsp"
applicationId 'com.drivewyze.agatha2'
…
}
qaFlavor {
dimension "dsp"
applicationId 'com.drivewyze.agatha2.qa'
…
}
devFlavor {
dimension "dsp"
applicationId 'com.drivewyze.agatha2.dev'
…
}
mongoDevFlavor {
dimension "mongo"
}
mongoQaFlavor {
dimension "mongo"
}
mongoProdFlavor {
dimension "mongo"
}
}

Android Studio 3 - Dependency-Aware Build Variants and Product Flavors

  • 1.
    Dependency-Aware Build Variants inAndroid Studio 3.0 Stefan Martynkiw
  • 2.
    Android Studio 3.0Introduction • Introduced at Google I/O 2017 • Improvements: • Faster Gradle build times • Profiling tools • UI • Network Request Inspection (with Retrofit & OkHTTP) • Device File Explorer • App-Private files too! (no more adb run-as!) • Android Gradle Plugin Improvements
  • 3.
    Application Structure andDependencies • Applications in Android Studio can be structured into modules • Drivewyze Example: • Agatha (Mobile app) • Gaby (Stripped down for Android ELDs) • Erica (Stripped down for even older Android ELDs) • Common • “Magic Sauce” Agatha Erica Gaby Common
  • 4.
    Why have variants? •Production • Signing Keys, Production Environment • QA • Unsigned, QA test environment • Development • Unsigned, Local-host server Agatha Erica Gaby Common
  • 5.
    Set Theory andthe Cross Product • {A, B, C} • {A, B, C} ⋈ {D, E, F} = • AD, AE, AF • BD, BE, BF • CD, CE, CF • {A, B, C} ⋈ {D, E, F} ⋈ {Y, Z} = • ADY, AEY, AFY • BDY, BEY, BFY • CDY, CEY, CFY • ADZ, AEZ, AFZ • BDZ, BEZ BFZ • CDZ, CEZ, CFZ
  • 6.
    Abstract  Real •“Build type”: • {Debug, Release, Simulated Location} • “Authorization Service / Server Environment”: • {Production, QA, Local} • “Event cache”: • {Production, QA, Local}
  • 7.
    Product Flavors &Build Types & Build Variants • Build Types • Sets up various build properties. • The debug build type enables debug options and signs the APK with the debug key. • The release build type may shrink, obfuscate, and sign your APK with a release key for distribution. • Android Studio creates the debug and release build types by default. • Product Flavors • Product flavors represent different versions of your app that you may release to users • Free and paid versions of your app. • You can customize product flavors to use different code and resources, while sharing and reusing the parts that are common to all versions of your app. • Product flavors are optional and you must create them manually. • More info: • https://developer.android.com/studio/build/index.html • Build Variants • The resulting entries in the set formed by cross-product of BuildTypes x ProductFlavors freeFlavor paidFlavor debug freeDebug paidDebug release freeRelease paidRelease
  • 8.
    The Old Way •“Build Type” did not propagate to dependent modules • Really old bug in Gradle • Reported March, 2013 • https://issuetracker.google.com/issu es/36967265 • “DESCRIPTION: Gradle plugin does not propagate whether you are doing a debug build or a release build to a dependent android library” Agatha Build.conf: buildTypes { release {…} debug {…} simloc {…} } productFlavors { prodFlavor {…} qaFlavor {…} devFlavor {…} } Dependencies{ … simlocCompile project(path: ':common', configuration: 'simlocMongoQa') debugCompile project(path: ':common', configuration: 'libraryMongoQa') releaseCompile project(path: ':common', configuration: 'libraryMongoProd')} Common Build.conf: buildTypes { mongoDev { … } mongoQa { … } mongoProd { … } } productFlavors { library { buildConfigField 'boolean', 'SIMLOC_BUILD', "false" } simloc { buildConfigField 'boolean', 'SIMLOC_BUILD', "true" } }
  • 9.
    Flavor Dimensions • FlavorDimensions add another cross product. • {A, B, C} ⋈ {D, E, F} ⋈ {Y, Z} = • ADY, AEY, AFY • BDY, BEY, BFY • CDY, CEY, CFY • ADZ, AEZ, AFZ • BDZ, BEZ BFZ • CDZ, CEZ, CFZ • https://www.youtube.com/watc h?v=daYl7edb6S0
  • 10.
    Declaring Flavor Dimensions •//Todo finish slide: • https://developer.android.com/s tudio/build/build- variants.html#flavor-dimensions
  • 11.
    The New WayCommon: Build.conf buildTypes { debug{ debuggable true buildConfigField 'boolean', 'SIMLOC_BUILD', "false" } release { debuggable false buildConfigField 'boolean', 'SIMLOC_BUILD', "false" } simloc { debuggable true buildConfigField 'boolean', 'SIMLOC_BUILD', "true" } } flavorDimensions "mongo" productFlavors { mongoDevFlavor { dimension "mongo" buildConfigField 'String', 'DRIVE_EVENTREPL_URI', '”…"' } mongoQaFlavor { dimension "mongo" buildConfigField 'String', 'DRIVE_EVENTREPL_URI', '”…"' } mongoProdFlavor { dimension "mongo" buildConfigField 'String', 'DRIVE_EVENTREPL_URI', '"…"' } } Agatha2: Build.conf flavorDimensions "dsp", "mongo" productFlavors { prodFlavor { dimension "dsp" applicationId 'com.drivewyze.agatha2' … } qaFlavor { dimension "dsp" applicationId 'com.drivewyze.agatha2.qa' … } devFlavor { dimension "dsp" applicationId 'com.drivewyze.agatha2.dev' … } mongoDevFlavor { dimension "mongo" } mongoQaFlavor { dimension "mongo" } mongoProdFlavor { dimension "mongo" } }