Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Level Up with Android Build Variants

Level Up with Android Build Variants

Phil Shadlyn

July 07, 2015
Tweet

More Decks by Phil Shadlyn

Other Decks in Programming

Transcript

  1. Level Up with Android Build Variants

  2. Why do I care? • It’d be nice to build

    multiple variants of an Android app from a single code base • Common variants include: ◦ debug/release ◦ free/paid ◦ dev/staging/production
  3. With Eclipse and Ant • Code base contained in library

    project • Needed an additional project for each variant which referenced library project
  4. None
  5. With Android Studio and Gradle • All contained in single

    project • Define Build Types and Product Flavours in build.gradle file • Create src directories to hold code specific to each build type/flavour
  6. None
  7. Build Types • Define how to build and package your

    app • debug and release build types created by default • Can create your own
  8. signingConfigs { release { storeFile file( 'release.keystore' ) storePassword 'verysecurepassw0rd'

    keyAlias 'release' keyPassword 'verysecurepassw0rd' } } buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile( 'proguard-android.txt' ), 'proguard-rules.pro' signingConfig signingConfigs.release } debug{ applicationIdSuffix ".debug" signingConfig null } }
  9. Product Flavours • Provide alternative code, resources or configuration •

    Examples: ◦ Different API endpoints for dev/staging/production environment ◦ Free/paid version
  10. Free / Paid • Provide alternative resources and code by

    creating flavour directory • Gradle creates combination of each Build Type and Product Flavour
  11. productFlavors{ free{ applicationId "com.physphil.android.sandbox.free" } paid{ applicationId "com.physphil.android.sandbox.pro" } }

    } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' compile 'de.greenrobot:eventbus:2.4.0' // Only full version requires network calls paidCompile 'com.squareup.retrofit:retrofit:1.9.0' paidCompile 'com.squareup.okhttp:okhttp:2.2.0' }
  12. None
  13. Free / Paid

  14. Dev / Staging / Production • Define flavour for each

    server environment • Store API base URL in a Config class • Create directory in src for each flavour, which contains an instance of Config
  15. buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile(

    'proguard-android.txt' ), 'proguard-rules.pro' signingConfig signingConfigs.release } debug { applicationIdSuffix ".debug" signingConfig null } } productFlavors { dev staging production }
  16. None
  17. That’s Not All! • Modify more build parameters • Include

    libraries, code, Activities etc only when required • Lots of examples in official docs
  18. Additional Resources • http://tools.android.com/tech-docs/new-build- system/user-guide • https://developer.android. com/tools/building/configuring-gradle.html • http://www.vogella.

    com/tutorials/AndroidBuild/article.html
  19. Questions?