Android different packageName with flavors
Asked Answered
H

3

10

I need to install 2 versions of my project (Production and Development). I need 2 apps. I'm trying to achive it by using flavors, but when I sign the apk, it always generate the same app, with the same packageName (com.company.project). I've tried removing the applicationId from the defaultConfig but it doesn't work neither.In the manifest, the package name is com.company.project.

Anyone knows how to do that?

This is the build.gradle

defaultConfig {
            multiDexEnabled true
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            applicationId "com.company.project"
    }    
    productFlavors {
                development {
                    applicationId
                    "com.company.project.DEV"
                    versionName "1.0-dev"
                    resValue "string", "app_name", "Project-Dev"                     
                }
                production {
                    applicationId
                    "com.company.project.PROD"
                    resValue "string", "app_name", "Project-Prod"
                    versionName "1.0-prod"                        
                }
            }
Hix answered 7/7, 2016 at 10:45 Comment(2)
how are you building your separate APKs? i.e. what is the command you use? how are you confirming what the package name is after you build it?Serrano
@Serrano Gradle can make all what you need by default. Just create your projects in Android Studio.Intreat
H
1

Finally I did it like this:

def appName = 'AppName'
productFlavors {
    devel {
        applicationIdSuffix ".devel"
        def buildId, appNameLabel
        buildId = androidApplicationId + '.devel' + androidVersionCode
        appNameLabel = appName + 'd' + androidVersionName
        buildConfigField "String", "BUILD_ID", '"' + buildId + '"'
        manifestPlaceholders = [app_name_label: appNameLabel, buildId: buildId]        }

    QA { 
        applicationIdSuffix ".qa"
        def buildId, appNameLabel
        buildId = androidApplicationId + '.qa' + androidVersionCode
        appNameLabel = appName + 'q' + androidVersionName
        buildConfigField "String", "BUILD_ID", '"' + buildId + '"'
        manifestPlaceholders = [app_name_label: appNameLabel, buildId: buildId]
    }

    pro {
        buildConfigField "String", "BUILD_ID", '"' + androidApplicationId + '"'
        manifestPlaceholders = [app_name_label: appName, buildId: androidApplicationId]
    }
Hix answered 10/1, 2022 at 10:38 Comment(0)
R
2

When you create productFlavors then the corresponding gradle tasks also changes.

For instance, originally you have only assembleDebug and assembleRelease. But, after implementing productFlavors, the gradle tasks will change. Taking your example in consideration, it will be

  • assembleDevelopmentDebug
  • assembleDevelopmentRelease
  • assembleProductionDebug
  • assembleProductionRelease

If you are using Android Studio, then you do not have to worry about the gradle tasks. Just select the Build Variant from the menu and build the project. It will run the corresponding gradle tasks and install the build.

I have written a blog explaining this, Product Flavors in Android. A sample project is also available on GitHub.

Reprove answered 19/8, 2016 at 17:36 Comment(0)
C
1

I'm doing a similar thing and my build.gradle looks like this and it works:

flavorDimensions 'Level'
productFlavors {
    alpha {
        dimension 'Level'
        applicationIdSuffix '.alpha'
    }
    beta {
        dimension 'Level'
        applicationIdSuffix '.beta'
    }
    major {
        dimension 'Level'
    }
}

I actually set this up in Build -> Edit Flavors and it generated everything for me.

Containment answered 9/1, 2022 at 4:48 Comment(1)
I asked this 5 years ago and I forgot to auto answer this when I get to it, this is a good start, but for more complex projects, my answer is better, because it replaces package names in manifestHix
H
1

Finally I did it like this:

def appName = 'AppName'
productFlavors {
    devel {
        applicationIdSuffix ".devel"
        def buildId, appNameLabel
        buildId = androidApplicationId + '.devel' + androidVersionCode
        appNameLabel = appName + 'd' + androidVersionName
        buildConfigField "String", "BUILD_ID", '"' + buildId + '"'
        manifestPlaceholders = [app_name_label: appNameLabel, buildId: buildId]        }

    QA { 
        applicationIdSuffix ".qa"
        def buildId, appNameLabel
        buildId = androidApplicationId + '.qa' + androidVersionCode
        appNameLabel = appName + 'q' + androidVersionName
        buildConfigField "String", "BUILD_ID", '"' + buildId + '"'
        manifestPlaceholders = [app_name_label: appNameLabel, buildId: buildId]
    }

    pro {
        buildConfigField "String", "BUILD_ID", '"' + androidApplicationId + '"'
        manifestPlaceholders = [app_name_label: appName, buildId: androidApplicationId]
    }
Hix answered 10/1, 2022 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.