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. 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
  2. With Eclipse and Ant • Code base contained in library

    project • Needed an additional project for each variant which referenced library project
  3. 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
  4. Build Types • Define how to build and package your

    app • debug and release build types created by default • Can create your own
  5. 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 } }
  6. Product Flavours • Provide alternative code, resources or configuration •

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

    creating flavour directory • Gradle creates combination of each Build Type and Product Flavour
  8. 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' }
  9. 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
  10. 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 }
  11. That’s Not All! • Modify more build parameters • Include

    libraries, code, Activities etc only when required • Lots of examples in official docs