Android Import changes with flavor, even with same folder structure

64 views Asked by At

My app has two flavors core and foss, default flavor is core.

I've got 3 subfolders in my src folder, core, foss and main.

I'm trying to access my Home Screen which is referenced in the MainActivity and located in the main folder. The HomeScreen itself needs a HomeViewModel and that is contained in both the core and foss folder, but not in the main folder. enter image description here

The odd thing for me is when I try to build the core flavor it accepts this as a valid import in the Home.kt file:

import viewmodels.HomeViewModel

In foss flavor it only accepts this import:

import com.blazecode.tsviewer.viewmodels.HomeViewModel

For my understanding these imports are essentially the same, but I have to change it every time I want to switch the flavor.

This is how I declared my flavors:

flavorDimensions "version"
productFlavors {
    core {
        dimension "version"
        versionNameSuffix "-core"
    }
    foss {
        dimension "version"
        versionNameSuffix "-foss"
    }
}

sourceSets {
    core {
        java.srcDirs = ['src/core/java']
    }
    foss {
        java.srcDirs = ['src/foss/java']
    }
}

Is there any way I can move files around or more ideally have one general import so that I don't have to manually change things?

2

There are 2 answers

0
BlazeCodeDev On BEST ANSWER

Found the issue, the file's package declaration was different.

My core flavor file had this package declaration:

package viewmodels

While the foss flavor one had this:

package com.blazecode.tsviewer.viewmodels

I changed both to package viewmodels and now it's working!

11
Kristy Welsh On

https://developer.android.com/build/build-variants will refer you to the correct way to build flavors. You can have the different variants point to different folders will still keeping the package structure consistent.