0

I would like to create two falvors for my project. I did this successfully in the past but with new ANdroidStudio I a little bit concerned how to do that. The official guide is out-dated.

In the past what we need to do is to create additional folders on the level of "src" folder. In the current version of AndroidStudio we have app->{/java;/manifests;/res} structure and we can create folder only under the same package and I don't see the way to create additional folder for "res"

Can you advise please how I can create flavors for my app?

xf3172
  • 3
  • 2

2 Answers2

0

Maybe try with build types, somewhat similar to this post. You may change resource folders in that fashion

Community
  • 1
  • 1
webo80
  • 3,365
  • 5
  • 35
  • 52
0

The official guide is out-dated.

The core of the official guide's section on product flavors seems fine to me.

In the past what we need to do is to create additional folders on the level of "src" folder.

That would define a sourceset for a product flavor. You still need to declare the product flavor in build.gradle for the module, via a productFlavors closure or via the Project Structure dialog.

In the current version of AndroidStudio we have app->{/java;/manifests;/res} structure

Switch from the Android view of your project contents to the Project view, via the drop-down just above the project structure tree.

You will start with a tree like:

Android Studio project, Android view

and change it to a tree like:

Android Studio project, Project view

Can you advise please how I can create flavors for my app?

Add a productFlavors closure to your module's build.gradle file, manually or via the Project Structure dialog:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.commonsware.myapplication"
        minSdkVersion 8
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        vanilla {
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

If you want to have alternative source for that product flavor (Java, resources, manifest, etc.), create src/.../ in the project, where ... is the name of your flavor (src/vanilla/ would correspond with the sample build.gradle shown above). To create this directory, either:

  • in the Project view, create it as you would any other directory

  • in the Android view, just start defining a resource, and the dialog that appears will have a drop-down for the sourceset:

New layout resource dialog, showing possible sourcesets

However, as I do not know how to put Java code in a particular sourceset this way, I always work in the Project view.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491