I have my android application split into four different build flavours. Each of these flavours have a portion of unique java and xml files, and a portion of shared files under main. For the sake of clarity, let's call these Cucumber, Tomato, Onion and Pepper. These flavours have unique layouts, drawables, strings, colours, java files, etc.
My requirement is now that I need to split my Pepper flavour into two sub-flavours; Red-Pepper and Green-Pepper. The layouts for these two flavours should be identical - the only different is that the two flavours must use different drawable and string resources. I don't want to have duplicate layout files in both the red and green flavours for obvious reasons.
How can this be achieved in gradle?
You can define multiple resource folders.
In this way you can use common folders for 2 flavors (in you redPepper and greenPepper).
For example something like this:
android {
...
sourceSets {
main {
//....
res.srcDirs = ['/src/main/res']
}
redPepper {
res.srcDirs = ['/src/flavorRed/res', '/src/commonPepper/res']
}
greenPepper {
res.srcDirs = ['/src/flavorGreen/res', '/src/commonPepper/res']
}
//.....other flavors
}
}
Related
I'm developing on Android and have some difficulty with Gradle's build logic.
I'm trying to have a set of resources and java files for a combined flavors.
I put them in a folder src/productFlavor1ProductFlavor2/
But when I compile the build variant productFlavor1ProductFlavor2DevDebug , it simply doesn't get anything from the productFlavor1ProductFlavor2 folder.
I tried pretty much everything...
From the syntax and order:
src/productFlavor1ProductFlavor2/
src/productFlavor1productFlavor2/
src/productFlavor1-productFlavor2/
src/productFlavor1/ProductFlavor2/
To trying to indicate the folder to gradle
android.applicationVariants.all { variant ->
if (variant.getProductFlavors().get(0).name.equals('productFlavor1')
&& variant.getProductFlavors().get(1).name.equals('productFlavor2'))
variant.sourceSets = 'src/productFlavor1ProductFlavor2'//read only... so doesn't work
}
Anyone knows why the combination of flavors indicated in https://developer.android.com/studio/build/ doesn't actually work....? Or if I'm missing somethine there...
To make this work you need to define multiple dimensions of flavors.
It is not allowed to combine the single flavor dimensions.
I don't think it is a good idea to put source code to multi-flavor folders anyway as you are getting too many combinations to take care of.
If I were on your place, I would treat every dimension separately instead ie:
have a separate folder with productFlavor1 & productFlavor2.
Check this out: flavors, overview of build system.
The priority order for the different folders:
1. build variant (fully qualified name, like debugFlavor1Flavor2/src)
1. build type (debug/src)
1. product flavor (if you have multiple dimensions they ordered in order of declaration)
1. main/src
I need to combine two flavors that are available in my project and each flavor is divided for different aspects i.e one flavor for one functionality. All I need is to combine the two flavors such that i can have two functionalities in one build. Is there any method to combine two flavors? If there plz help me..thanks in advance.
productFlavors {
flavor1{
//for one functionality
}
flavor2{
//for second functionality
}
flavor3{
//for third functionality
}
}
Flavours are used to create versions of the same app (as the name indicates).
If you want to split your application into features it will be better to separate them into android library modules.
Here is a guide of how to do this:
https://developer.android.com/studio/projects/android-library.html
After so much of research got an answer to my problem.The solution is as follows:
sourceSets{
flavor3{
java.srcDirs 'src/flavor1/java','src/flavor2/java'
res.srcDirs 'src/flavor1/res','src/flavor2/res'
}
}
By doing so i can combine two flavors into single functionality as i wanted.
Using build variant in Android we can support different flavors.
I need to develop an application where I am supporting different clients. Each client needs are a little different.
However, the basic data, network call class etc are same for all clients.
How can I ensure partial code of my application remains same for all flavors?
This will help in maintaining one repository for all common classes.
You need to understand how build variant works.
Each client needs are a little different is a vague statement
Imagine you have an application which has different screen's for different countries. But major functionalities are the same.
Now using build variants you can make different flavors
1) For country one : This will have screens designed specific to country one
2) For country two : This will have screens designed specific to country two
3) Common part : All the common business logic can be put under your common package
While project is build, the common part is considered and specific flavor too becomes part of flavorXX.apk
productFlavors {
employee {
applicationId "com.myapp.employee"
}
driver {
applicationId "com.myapp.driver"
}
asset {
applicationId "com.myapp.asset"
}
vehicle {
applicationId "com.myapp.vehicle"
}
}
sourceSets {
asset {
manifest.srcFile 'src/asset/AndroidManifest.xml'
}
driver {
manifest.srcFile 'src/driver/AndroidManifest.xml'
}
employee {
manifest.srcFile 'src/employee/AndroidManifest.xml'
}
vehicle {
manifest.srcFile 'src/vehicle/AndroidManifest.xml'
}
}
In the above example , I am having different flavors of same application. Inorder to split accordingly you need to understand which part of your app goes into specific flavor and which can be kept common. Go through below links for more details.
Understanding Product Flavors reference link 1
Understanding Product Flavors reference link 2
I'm working on a project with a lot of .xml layout files. I want to organize them in separate folders.
It seems like Resource Merging would be the right solution.
http://tools.android.com/tech-docs/new-build-system/resource-merging
I want to split my layout folder in activity, listview, dialog, button, etc.
How do I modify my project and build.gradle file to accomplish this ?
Resource merging isn't really the concept you're looking for here -- that document specifies how if you have multiple build types and flavors, how they combine to provide a single view of the project's resources that will be built into the final result. In your case, you probably have a single build type and flavor, and want to have subdirectories in your resources to help organize them better.
The bad news is that Android isn't very friendly about this. The build system expects resources to be arranged in a rigid format, with all layouts being in a single folder underneath your project root, for example, and it doesn't let you deviate from that. The best thing you can do is to have multiple resource folder trees, which would look like this:
AppModule
+ src
+ main
+ java
+ res
+ drawable
+ layout
+ ...etc...
+ extra-res
+ drawable
+ layout
+ ...etc...
Each resource sub-tree has its subdirectories in the same format. You don't need to have an exhaustive list of subdirectories in there if they're empty; just include the ones that have things you need.
To make this work, you need to have the following in your build.gradle script:
android {
sourceSets {
main {
res.srcDirs = ['src/main/res', 'src/main/extra-res']
}
}
}
Im trying to use a series of different resource folders for my product flavors but at the moment what Im doing isnt working for all flavors which is weird.
Can some one just point me to where I can simply see how to assign an entire resource directory to a productFlavor. Like I said It works for two flavors (the main one and another) and then the rest all default to the main set.
The fix I came up with for this issue was to explicitly declare my res files for my flavors that werent working by using sourceSets like this
sourceSets{
flavor6{
res.srcDirs = ['src/flavor6/res']
}
}