41

I'm trying to create a single project with 2 flavors: free and pro (versions).

My app is already in PlayStore with different packages (E.g.: com.example.appfree and com.example.app)

This is my build.gradle:

defaultConfig {
   applicationId "com.example.appfree"
}
productFlavors{
   lite {
       applicationIdSuffix 'lite'
   }

   pro {

   }
}

And this is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".SplashScreenActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"/>

    </application>

</manifest>

I tried to upload a build apk for free and pro flavors. Pro flavor is ok, but free flavor isnt accepted by Google, because the package is incorrect. How can I solve this?

====== Solved: ====== applicationIdSuffix only works with buildTypes.

Felipe Porge Xavier
  • 2,236
  • 6
  • 19
  • 27
  • 6
    I was not aware that `applicationIdSuffix` worked for flavors. Use `applicationId` to spell out the exact application ID that you want to use for the flavor. – CommonsWare Sep 17 '16 at 23:15
  • @CommonsWare, great answer! applicationIdSuffix works only with buildTypes! I used applicationId and it works perfectly! :) – Felipe Porge Xavier Sep 18 '16 at 02:47

3 Answers3

37

With the new Android Gradle build system, you can easily build multiple different versions of your app; for example, you can build both a "free" version and a "pro" version of your app (using flavors), and these should have different packages in the Google Play store such that they can be installed and purchased separately, both installed at the same time, and so on. Similarly, you may also build both "debug" and "alpha" and "beta" versions of your app (using build types) and these can also similarly contribute to unique package names.

app/build.gradle:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 19
    buildToolsVersion "19.1"
    defaultConfig {
        applicationId "com.example.my.app"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
...

app/build.gradle:

productFlavors {
    pro {
        applicationId = "com.example.my.pkg.pro"
    }
    free {
        applicationId = "com.example.my.pkg.free"
    }
}

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
}
....

from Android Studio Project Site - ApplicationId versus PackageName

Veener
  • 4,771
  • 2
  • 29
  • 37
  • 3
    You didn't answer my question: I tried to upload a build apk for free and pro flavors. Pro flavor is ok, but free flavor isnt accepted by Google, because the package is incorrect. How can I solve this? – Felipe Porge Xavier Sep 18 '16 at 02:29
  • 2
    He showed you how do to do it you just need to use the appropriate package names for your app in the flavor definitions. – nasch Sep 18 '16 at 19:13
8
flavorDimensions "version"
productFlavors {
    demo {
        // Assigns this product flavor to the "version" flavor dimension.
        // This property is optional if you are using only one dimension.
        dimension "version"
        applicationIdSuffix ".demo"
        versionNameSuffix "-demo"
    }
    full {
        dimension "version"
        applicationIdSuffix ".full"
        versionNameSuffix "-full"
    }
}
fMadTech
  • 308
  • 3
  • 5
8

Besides the basic gradle.build (app level) changes. Note that you only need to add the config you need to, not all of these:

productFlavors {
  enterprise {
      applicationId = "com.example.appname.enterprise"
  }
  consumer {
      applicationId = "com.example.appname.consumer"
  }
}

buildTypes {
  debug {
      applicationIdSuffix ".debug"
  }
}

I wanted to add that if you are using Firebase and you are required to have a google-services.json file. You will see the following:

ERROR: No matching client found for package name "com.example.appname.debug"

To fix it, you will need to register the new package name in Firebase and download the new google-services.json which will have all your existing packages names. If not you will have a compile error saying no client was found for the new package name.

Benny
  • 1,650
  • 17
  • 20
  • 1
    Thank you very much. I initially added only dev app in firebase and I was getting this error. Adding all app flavours in firebase and using the updated google-services.json fixed this. – John Doe Sep 01 '23 at 14:33