Build flavor with custom sourseset - android

My project contains multiple flavor and main folder which contains common logic when i build project with a specific flavor gradle use values from main folder even if that value is present in that flavor
Please help
Project structure
MainProject
src
main (main project)
java
flavor1
java/Appconstant.java
flavor2
java/Appconstant.java
...
build.gradle file
productFlavors {
flavor1 {
applicationId <pkgName>
versionCode 15
versionName "2.4.0"
}
flavor2 {
applicationId <pkgName>
versionCode 1
versionName "1.0.0"
}
}
sourceSets {
flavor1 {
java.srcDirs = ['src/flavor1/java']
res.srcDirs = ['src/flavor1/res']
}
flavor2 {
java.srcDirs = ['src/flavor2/java']
res.srcDirs = ['src/flavor2/res']
}
}
EDIT
modified project structure as per suggestion but now flavor class in not visible in Main

Related

Flutter flavor sourceSet resource not refer flavor folder

In my Flutter mobile app have two flavors with separate res folders for each. I have added sourceSet in build.gradle file, but at runtime the running flavor does not show the relavant resource from flavor folder, instead the main folder resources.
Folder structure as below.
src
|
flavor1
------res(with sub folders)
|
flavor2
------res(with sub folders)
|
main
------res(with sub folders)
build.gradle file changes.
android {
sourceSets {
main {
java.srcDirs += 'src/main/kotlin'
res.srcDirs = ['src/main/res']
manifest.srcFile 'src/main/AndroidManifest.xml'
}
flavor1 {
res.srcDirs = [
'src/flavor1/res/drawable-hdpi',
'src/flavor1/res/drawable-ldpi',
'src/flavor1/res/drawable-mdpi',
'src/flavor1/res/drawable-v21',
'src/flavor1/res/drawable-xhdpi',
'src/flavor1/res/drawable-xxhdpi',
'src/flavor1/res/drawable-xxxhdpi',
'src/flavor1/res/drawable',
'src/flavor1/res/mipmap-hdpi',
'src/flavor1/res/mipmap-mdpi',
'src/flavor1/res/mipmap-xhdpi',
'src/flavor1/res/mipmap-xxhdpi',
'src/flavor1/res/mipmap-xxxhdpi',
'src/flavor1/res/raw',
'src/flavor1/res/values',
'src/flavor1/res'
]
manifest.srcFile 'src/flavor1/AndroidManifest.xml'
}
flavor2 {
res.srcDirs = [
'src/flavor2/res/drawable-hdpi',
'src/flavor2/res/drawable-ldpi',
'src/flavor2/res/drawable-mdpi',
'src/flavor2/res/drawable-v21',
'src/flavor2/res/drawable-xhdpi',
'src/flavor2/res/drawable-xxhdpi',
'src/flavor2/res/drawable-xxxhdpi',
'src/flavor2/res/drawable',
'src/flavor2/res/mipmap-hdpi',
'src/flavor2/res/mipmap-mdpi',
'src/flavor2/res/mipmap-xhdpi',
'src/flavor2/res/mipmap-xxhdpi',
'src/flavor2/res/mipmap-xxxhdpi',
'src/flavor2/res/raw',
'src/flavor2/res/values',
'src/flavor2/res'
]
manifest.srcFile 'src/flavor2/AndroidManifest.xml'
}
}
// Because of flavors and multiple res folders.
afterEvaluate {
android.applicationVariants.all { variant ->
tasks.named("generate${variant.name.capitalize()}ResValues")
.configure { task ->
task.outputs.upToDateWhen {false}
}
}
}
flavorDimensions "app"
productFlavors {
flavor1 {
dimension "app"
applicationIdSuffix ""
}
flavor2 {
dimension "app"
applicationIdSuffix ".flavor2"
}
}
}
Running the flavor2 app still refers to main resources is the issue. I want the respective flavor to refer only the given flavor specific resource folder in runtime.
sourceSet in not necessary, just add product flavors within app/build.gradle, folders structure is ok, at compilation time all resources are merged and main folder resources are overridden with flavor resources
just run next command
flutter run --flavor flavor2
more info here: https://medium.com/#salvatoregiordanoo/flavoring-flutter-392aaa875f36

Android: Are there any methods to share a resource betwwen different falvors and sourceset?

I have an application with below source sets and flavors:
flavorDimensions "brand"
productFlavors {
flavor1 {
dimension "brand"
sourceSets {
main {
manifest.srcFile "src/main/flavor1/AndroidManifest.xml"
resources.srcDirs = ['src/flavor1/']
}
}
}
flavor2 {
dimension "brand"
sourceSets {
main {
manifest.srcFile "src/main/flavor2/AndroidManifest.xml"
resources.srcDirs = ['src/flavor2/']
}
}
}
flavor3 {
dimension "brand"
sourceSets {
main {
manifest.srcFile "src/main/flavor3/AndroidManifest.xml"
resources.srcDirs = ['src/flavor3/']
}
}
}
My application is such that 99% of layouts and classes are the same. E.g. only layout file main_activity.xml is different among flavors but the other 20~30 layout files are completely the same. I don't want to copy these 20~30 in three different flavor source set. Are there any ways to share them among three flavors and if a file was present in source set, it overrides the share files? (Such as want android does for drawable and drawble-hdpi , ...)?
So I don't want to have
Unless I have misunderstood the question, you could solve the issue by changing build.gradle like this:
productFlavors {
flavor1 {
dimension "brand"
}
flavor2 {
dimension "brand"
}
//...
}
and structuring your app module like this:
/src
/main
/flavor1
/flavor2
...
and placing all common files(source, resources) in main and the custom/specific files in the flavorX folders which will cause gradle to replace the identically named files in main when each flavor is built.

Product flavors in Android Studio [duplicate]

This question already has answers here:
Reference different assets based on build flavor
(2 answers)
Closed 3 years ago.
I can not get product flavours working. I have this gradle
apply plugin: 'com.android.application'
android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
compileSdkVersion 27
}
signingConfigs {
release {
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
signingConfig signingConfigs.release
}
}
repositories {
maven { url "https://jitpack.io" }
}
flavorDimensions "dim1", "dim2", "dim3"
productFlavors {
flavor1 {
dimension "dim1"
applicationId "com.example.dim1.app"
}
flavor3 {
dimension "dim2"
applicationId "com.example.dim2.app"
}
flavor3 {
dimension "dim3"
applicationId "com.example.dim3.app"
}
}
sourceSets {
flavor1 {
java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example1/AndroidManifest.xml"
assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example1/assets/"]
resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example1/res/"]
}
flavor2 {
java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example2/AndroidManifest.xml"
assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example2/assets/"]
resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example2/res/"]
}
flavor3 {
java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example3/AndroidManifest.xml"
assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example3/assets/"]
resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example3/res/"]
}
}
}
dependencies {
api 'com.google.android.gms:play-services-maps:15.0.0'
api 'com.google.android.gms:play-services-location:15.0.0'
api 'com.android.support:appcompat-v7:27.1.1'
api 'com.github.PhilJay:MPAndroidChart:v2.0.8'
}
...
When I got to "Build | Select variant" I can only select
Module:app
Build Variant:flavor1Flavor2Flavor3Debug,flavor1Flavor2Flavor3Release
I would have liked to get
the following build variants: flavor1Debug,flavor2Debug,flavor3Debug,flavor1Release,flavor2Release,flavor3Release
I have tried "File | Sync project with gradle file"
...
I get this error
Caused by: java.lang.RuntimeException: Cannot read packageName from
W:\android-studio-projects\sharedid\app\src\main\AndroidManifest.xml
I have tried to both
have no such file (hoping it would take the product flavor one?)
have the "main" manifest only define shared stuff between all product flavors
Just try like below,
flavorDimensions "dim1"
productFlavors {
flavor1 {
dimension "dim1"
applicationId "com.example.dim1.app"
}
flavor3 {
dimension "dim1"
applicationId "com.example.dim2.app"
}
flavor3 {
dimension "dim1"
applicationId "com.example.dim3.app"
}
}
For more details about build variant see this link
I think there are two unrelated problems :
Currently you have 2 build types (the automatically created debug and release) and 3 dimensions (dim1, dim2 and dim3), each one having 1 variant (flavor1 for dim1, flavor2 for dim2, ...)
this gives at most :
2 * 1 * 1 * 1 = 2 combinations
You should switch to 2 build types and 1 dimension (say dim1) having 3 variants (flavor1, flaver2 and flavor3) to have :
2 * 3 = 6 apks
You should have a main manifest. Unlike other resources the manifest is not simply overriden but merged from multiple sources (see Merge Multiple Manifest Files for more details).
It should at least contains a package name (possibly different from the final applicationId(s)) as explained by this note from Configure Product Flavors :
Note : You still need to specify a package name using the package attribute
in the main/ manifest file. You must also use that package name in
your source code to refer to the R class, or resolve any relative
activity or service registration. This allows you to use applicationId
to give each product flavor a unique ID for packaging and
distribution, without having to change your source code.
I would have liked to get
the following build variants:
flavor1Debug,flavor2Debug,flavor3Debug,flavor1Release,flavor2Release,flavor3Release
For this, you need to define the same dimension for all flavors.
I get this error
Caused by: java.lang.RuntimeException: Cannot read packageName from
W:\android-studio-projects\sharedid\app\src\main\AndroidManifest.xml
You get this error because the path is not reachable.
Just think, how can app find W: when it is running?
So, you need to use a relative path here.
Also from official documentation (https://developer.android.com/studio/build/build-variants#configure-sourcesets):
If you have sources that are not organized into the default source set
file structure that Gradle expects, as described above in the section
about creating source sets, you can use the sourceSets block to change
where Gradle looks to gather files for each component of a source set.
You don't need to relocate the files; you only need to provide Gradle
with the path(s), relative to the module-level build.gradle file,
where Gradle should expect to find files for each source set component

Product flavors in android not working

I have been stuck on this for a day now. I created 2 flavors for my project and added them under the buildtype in android in build.gradle. I then created a res folder for both and changed the icons for each. When I run the project for any of the variants, it is showing the icon from the project.
My project structure in as follows
[project]
[app]
src
main
res
mipmap
flavor1
flavor2
res
mipmap
and my build.gradle is
productFlavors {
flavor1 {
applicationId "abc"
}
flavor2 {
applicationId "abc"
}
}
sourceSets {
flavo1 {
res.srcDirs = ['src/flavor1/res']
}
flavor2 {
res.srcDirs = ['src/flavor2/res']
}
}
PS: nvm the naming
You should remove the icon from main and leave it only in flavor1 and flavor2 directories.
Also check the names of the flavors in the build.gradle, I think it should be flavor1 instead of flavo1 and flavor2 instead of staging.
Another note, sourceSets definition is not required, standard res directories inside flavors' directories will be used automatically.

Gradle: sourceSets not working as expected

Gradle: sourceSets not working as expected.
Here is my code:
android {
...
defaultConfig {
applicationId "com.firsthash.smartyoutubetv2"
minSdkVersion 14
targetSdkVersion 25
versionCode 111
versionName "6.8.39"
multiDexEnabled = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
main {
res.srcDirs = ['src/main/res', 'src/main/res-exoplayer', 'src/main/res-xwalk', 'src/main/res-webview']
}
debug {
res.srcDirs = ['src/main/res', 'src/main/res-exoplayer', 'src/main/res-xwalk', 'src/main/res-webview']
}
}
productFlavors {
full {
}
beta {
applicationIdSuffix ".beta"
}
}
}
The problem. Everything goes ok until I try to do an update of any project's layout file. All changes that I've made didn't reflected on the final application. I figured out that to obtain right behavior I need to manually delete project's 'build' directory. This directory contains cached build files. Such happens when I built apk using 'gradle: installDebug task' or Android Studio internal build system.
UPD: best workaround that could find is to use flavor dimensions: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-flavor-variants

Categories

Resources