Project Configurations for Android πŸ› οΈ

Hero image for Project Configurations for Android πŸ› οΈ

Gradle configuration

  • Gradle should be the default option for build system (for now, you may want to review some other later too such as Bazel, Buck…)
  • Although Gradle offers a large degree of flexibility in your project structure, unless you have a compelling reason to do otherwise, you should accept its default structure as this simplify your build scripts.
  • Building a Signed Release APK, you can choose either way:
    • AS Menu -> Build -> Generate Signed APK -> Manually adding the keystore directory / passwords -> Build.
    • Gradle config for Signing APK:

      // Put this in your app module level gradle:
      signingConfigs {
          release {
              try {
                  storeFile file("myapp.keystore")
                  storePassword KEYSTORE_PASSWORD
                  keyAlias "thekey"
                  keyPassword KEY_PASSWORD
              }
              catch (ex) {
                  throw new InvalidUserDataException("You should define KEYSTORE_PASSWORD and KEY_PASSWORD in gradle.properties.")
              }
          }
      }
      
      // And create another `gradle.properties` file to keep the password, don't PUSH this to the repo.
      KEYSTORE_PASSWORD=real_keystore_password
      KEY_PASSWORD=real_key_passw
      
  • Don’t ever expose the key secrets like this:

      // Don't do this
      signingConfigs {
          release {
              storeFile file("myapp.keystore")
              storePassword "storepassword"
              keyAlias "thekey"
              keyPassword "nicepassword"
          }
      }
    

Gradle configuration for app flavors

The purpose is to separate the Develop environment (endpoints, library keys…) with the real Production environment. To achieve this:

  • First, define the flavor in app level gradle config:

      productFlavors {
          production {
              applicationId "com.app.android"
          }
    
          staging {
              // Staging AppId should be different from the real production.
              applicationId "com.app.staging"
          }
      }
    
      sourceSets {
          staging {
              res.srcDirs = ['src/staging/res']
          }
          // production doesn't need to clarify the resource directory as it uses the default main one.
      }
    
  • Second, create a proper staging directory for the staging resources (endpoints, different keys…):

      β”‚  β”‚  β”œβ”€ main
      β”‚  β”‚  β”‚  β”œβ”€ java
      β”‚  β”‚  β”‚  └─ res
      β”‚  β”‚  β”‚     β”œβ”€ anim
      β”‚  β”‚  β”‚     β”œβ”€ color
      β”‚  β”‚  β”‚     β”œβ”€ drawable
      β”‚  β”‚  β”‚     β”œβ”€ layout
      β”‚  β”‚  β”‚     └─ values
      β”‚  β”‚  β”‚
      β”‚  β”‚  β”‚
      β”‚  β”‚  └─ staging
      β”‚  β”‚     └─ res
      β”‚  β”‚        β”œβ”€ anim
      β”‚  β”‚        β”œβ”€ color
      β”‚  β”‚        β”œβ”€ drawable
      β”‚  β”‚        β”œβ”€ layout
      β”‚  β”‚        └─ values
    

ABI filter

  • The ABI defines, with great precision, how an application’s machine code is supposed to interact with the system at runtime. You must specify an ABI for each CPU architecture you want your app to work with. Applying an incorrect ABI choice on a mismatched CPU architecture won’t make it work. For example: running on x86 emulator requires you to use the proper x86 apk.
  • You can read more from here, but in this example, we’re just simply splitting the app into 2 different flavors:
    • production_x86 for x86 chipset.
    • production for armeabi, armeabi-v7a chipset.
      // Adding this to your app module level gradle:
      production_x86 {
          // Append automatically a different code version to identify the different version when app is published and we observe the tracking info.
          versionCode Integer.parseInt("6" + defaultConfig.versionCode)
          applicationId "com.redplanet.android"
          ndk {
              abiFilters "x86"
          }
      }
    
      production {
          applicationId "com.redplanet.android"
          ndk {
              abiFilters "armeabi", "armeabi-v7a"
          }
      }
    
      // Don't forget to bring the proper native library files (.so) to the main/jniLibs/
    
  • Some Gradle tips that would be helpful.

This is only needed when the project is using native libraries.

Proguard

Proguard is normally used on Android projects to shrink and obfuscate the packaged code. We do this usually for release Apk (it helps saving time of downloading for users, hence increase the app download rates).

buildTypes {
    debug {
        minifyEnabled false
    }
    release {
        signingConfig signingConfigs.release
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Always check the Release Build whenever we add a new project config/dependency. You won’t want to have a little surprise within a short time when preparing for the final step and countering problem!

Keystore for CI/CD

When distributing a debug build, either via an automated CI/CD process, or a manual build process (building APK locally and send it to PM/Tester), we should create and configure a debug keystore.

Having a debug keystore removes a lot of issues for Product Managers, clients, and testers when installing a new (or old) build. Without it, installation always requires uninstalling the previously installed version on the device to process. It happens to both Firebase App Distribution and manual installation.

  • To generate a debug key:

      $ keytool -genkey -v -keystore debug.keystore -alias debug-key-alias -keyalg RSA -keysize 2048 -validity 10000
    
  • Then configure gradle for the debug flavor signing with the created key:

      signingConfigs {
          debug {
              keyAlias "debug-key-alias"
              keyPassword System.getEnv("DEBUG_KEYSTORE_PASSWORD")
              storeFile file("debug.keystore")
              storePassword System.getEnv("DEBUG_KEYSTORE_PASSWORD")
          }
          ...
      }
    
      buildTypes {
          debug {
              signingConfig signingConfigs.debug
          }
          ...
      }