How to provide different launcher icon per product flavour
Asked Answered
C

1

26

I have a project defined by build.gradle file

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.5.6'
}
}
apply plugin: 'android'

dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}

android {
compileSdkVersion 17
buildToolsVersion "17.0.0"

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['resources']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

    instrumentTest.setRoot('tests')
}

defaultConfig {
    ...
}

signingConfigs {
    debug {
        storeFile file("debug.keystore")
    }

    release {
        ...
    }
}

buildTypes {
    debug {
        debuggable true
        jniDebugBuild true
        signingConfig signingConfigs.debug
        packageNameSuffix ".debug"
        versionNameSuffix ".debug"
    }
    release {
        debuggable false
        jniDebugBuild false
        signingConfig signingConfigs.release
    }
}

flavorGroups "version", "market"


productFlavors {
    amazon {
        flavorGroup "market"
        buildConfig "public static final int COMPILATION = 1;"
    }
    google {
        flavorGroup "market"
        buildConfig "public static final int COMPILATION = 0;"
    }
    lite {
        flavorGroup "version"

        packageName = "package.name.lite"
    }
    full {
        flavorGroup "version"

        packageName = "package.name.full"
    }
}

android.sourceSets.amazon {
    res {
        srcDir "amazon"
    }
    manifest {
        srcFile "amazon/AndroidManifest.xml"
    }
}

android.sourceSets.google {
    res {
        srcDir "google"
    }
}

android.sourceSets.full {
    res {
        srcDir "full"
    }
}

android.sourceSets.lite {
    res {
        srcDir "lite"
    }
}
}

It works fine but now i want to provide specific launcher icons for every product flavor. I tried to put extra Manifest file for Amazon but it didn't work. I got an error "Duplicates files copied in APK AndroidManifest.xml". What could be wrong?

Croteau answered 27/8, 2013 at 8:1 Comment(1)
demo for android flavors goo.gl/5NSnEMLogo
P
27

You have a couple options:

  1. Switch to the new android studio/gradle layout and create folders for your productFlavors:

    src/main/ - Shared code/resources across all flavors
    src/amazon - Amazon specific code/resources across all flavors
    src/google - Google specific code/resources accross all flavors

    So in your main manifest (src/main/AndroidManifest.xml) if your android:icon is @drawable/icon, you would have the appropriate icon located in src/amazon/res/drawable-*/icon.png and likewise for the rest of your productFlavors.

  2. Keep your existing layout (and build.gradle) and append a resource directory to the res.srcDirs:

So, for your amazon sourceset:

android.sourceSets.amazon {
    res.srcDirs = ['res', '/path/to/amazon/res/dir']
}

And then in your /path/to/amazon/res/dir/drawable-* you would have your launcher icon.

Prebendary answered 27/8, 2013 at 18:33 Comment(2)
It's ok but only partially. It will work if I have only 2 icons for example for google and amazon version or for lite and full version. The problem is that i have flavor groups and i want my icon for lite version amazon to be different than lite version for google. If i provide icon for amazon it will be used for both version (lite and full) and i don't want that behavior. Is there any specific task like AmazonLite where I can put that icon or do I have to remove flavorgroups and make only pure flavors?Croteau
@Prebendary do I need to create folders for different productFlavours manually or will it be created automatically?Fletcherfletcherism

© 2022 - 2024 — McMap. All rights reserved.