From the code you've posted and from the comments, I believe you'd be able to achieve what you're looking for with product flavors read more here.
in your APP-level build.gradle, you'd do something like this :
android {
flavorDimensions 'default'
productFlavors {
foo{
dimension 'default'
}
bar{
dimension 'default'
}
}
}
this will define two different product flavors for you, specifically foo
and bar
. after doing a gradle sync, you'll see you now have these available as build variants.
next what you'll do, is create a folder structure similar to this:
creating a folder for bar
, a folder for foo
and leaving the folder for main, with the package signature inside each folder matching what your main structure is using. if you have image resources which will be different, make sure they're in appropriate folders here. If you're struggling to create a specific type of file/folder, I'd suggest just copying it over.
everything inside main will be used in BOTH types of apps, so all the logic which is the same, will remain there. everything which can be different will reside in the folders for bar
and for foo
. In my case, a simple class called Example
:
class Example {
fun giveValue(context: Context): String {
return context.getString(R.string.value)
}
}
the implementation for both of these classes are exactly the same in both folders, although yours could (and will) be entirely different.
You'll also see I have added strings.xml into my product flavors (in your case, perhaps this would be image resources as well), I've added the following string:
<resources>
<string name="value">from bar</string>
</resources>
into the strings.xml found under the bar
directory and
<resources>
<string name="value">from foo</string>
</resources>
into the strings.xml found under the foo
directory
my Example
class therefore just returns whatever this value is based on the build variant
and that's it.
inside my main activity:
val result = Example().giveValue(this)
Log.v("testing", result)
so, depending on the build variant i'm using (either fooDebug or barDebug
) the output will be different.