How to import java file of flavours in main folder?

517 views Asked by At

I am creating an android project with different product flavours. My project structure is like this Flavor1

Flavor2

Main

where Flavor1 contains a Constants.java file, Flavor2 also contains a Constants.java file but it is not in main. My main package contains another class Controller that uses variables defined in Constants. When I try to run my project it throws an error 'Cannot resolve symbol Constants'. When I press alt+enter it imports the file from selected flavor and the error is gone but when I switch to other flavor it doesn't identify the import because it's from flavor1. Here is code from my build.gradle file than defines product flavors and sourcesets.

 buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
        applicationIdSuffix ".debug"
    }
}
productFlavors {
    Flavor1{
        applicationId = "app.com.Flavor1


    }
    Flavor2{
         applicationId = "app.com.Flavor2
    }
}
sourceSets {
    main{
        java {
            srcDirs('src/Flavor1/java/src', 'src/Flavor2/java/src','src/main/java/src')
        }
        res {
            srcDirs('src/Flavor1/res/src', 'src/Flavor2/res/src','src/main/res/src')
        }
    }
    Flavor1{
        java {
            srcDirs('src/Flavor1/java/src', 'src/main/java/src')
        }
        res {
            srcDirs('src/Flavor1/res/src', 'src/main/res/src')
        }
    }
    Flavor2{
        java {
            srcDirs('src/Flavor2/java/src', 'src/main/java/src')
        }
        res {
            srcDirs('src/Flavor2/res/src', 'src/main/res/src')
        }
    }

}

I want every flavor to have its own Constants File. How this can be achieved? How can I use Constants file of every flavor in main so that it imports or uses file of selected Build Variant? Any help will be appreciated.

0

There are 0 answers