Search code examples
androidandroid-studioandroid-productflavors

Douts with flavors and packages names


I have two Flavors on my app, so my project has this structure:

productFlavors {
            green {
                resValue "string", "app_name", "green"
                minSdkVersion 14
                applicationId 'com.company.green'
                targetSdkVersion 22
                versionCode 3
                versionName '3.0.3'
            }

            red {
                resValue "string", "app_name", "red"
                minSdkVersion 14
                applicationId 'com.company.red'
                targetSdkVersion 22
                versionCode 3
                versionName '3.0.3'
                 }
             }          

Also I have different class implementation in every Flavor and commun classes in the main package.
Now let´s say that I have to implement the class:

MyClass

That class will have a different implementation for each flavor , so I am implementing the class inside of the green and red Flavor

After create MyClass on the green Flavor I am getting in the this error in the class itself.

package name com.company.MyClass does not correspond to the file path

I have also try with

package com.company.green.MyClass

But I am getting the same package name error

So my question is : What is correct package name for each Flavor ?


Solution

  • File MyClass.java should be located in 2 places: <project>/<module>/src/green/java/com/company/MyClass.java <project>/<module>/src/red/java/com/company/MyClass.java

    And should has package com.company,
    so applicationId set in flavor doesn't affect the package name and location of classes.

    Then you may do import com.company.MyClass anywhere in you project and appropriate version of MyClass will be taken.

    file tree

    For more details look at this gist.