0

I have an app that needs to be built with custom packages and a custom configuration file. I took a look on the internet and found about gradle flavors, but I couldn't figure out how to use them.

Basically I have an app with package com.example.apps.myapp. I need to build the same application using a different package com.example.apps.myapp2 and a custom config class like com.example.config.myapp2.Configuration

I currently have this on my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.1"

    defaultConfig {
        applicationId "com.example.apps.myapp"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':api')
    compile project(':view-pager-indicator')
    compile project(':MaterialEditText')
    compile 'com.pkmmte.view:circularimageview:1.1'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.android.support:appcompat-v7:21+'
    compile 'com.android.support:cardview-v7:21.+'
    compile 'com.facebook.android:facebook-android-sdk:3.21.1'
    compile 'com.mauriciogiordano:easydb:0.1.0'
}

How easy is to create a gradle task or something to make those changes and build the application? PS: I have zero knowledge about gradle.

Thank you.

Maurício Giordano
  • 2,597
  • 3
  • 26
  • 54

1 Answers1

1

It's easy to declare flavours in build.gradle.
Just from official docs Gradle Plugin User Guide. And for different conguration include buildConfigField. This fields you can read from BuildConfig.FieldName in any part of your code.

productFlavors {
            flavor1 {
                applicationId "com.example.apps.myapp"
                buildConfigField "int", "CONFIG_INT", "123"
                buildConfigField "String", "CONFIG_STRING", "\"ABC\""
            }

            flavor2 {
                applicationId "com.example.apps.myapp2"
                buildConfigField "int", "CONFIG_INT", "456"
                buildConfigField "String", "CONFIG_STRING", "\"QWE\""
            }
        }

And access to fields

BuildConfig.CONFIG_INT
BuildConfig.CONFIG_STRING

Don't forget to press Synchronize button (right from Save) to rebuild you BuildConfig with new fields. In the newest versions of gradle it's possible to declare Android resources Watch there.

Switch flavoirs from Build Variants

Build Variants in Android Studio

Community
  • 1
  • 1
AndreyICE
  • 3,384
  • 26
  • 26