Using Build Flavors - Structuring source folders and build.gradle correctly
If you got in the Studio preferences, under the Gradle section, you can enable auto-import for your project (we'll enable this by default later). This will let Studio re-import your build.gradle whenever you edit it.
Creating flavors doesn't mean you're going to use custom code for them so we don't create the folders. You do need to create them yourself.
If you look at my IO talk you'll see how we mix in together values from the flavors and build type to create the variant.
For the Java source:
src/main/java
src/flavor1/java
src/debug/java
are all 3 used to create a single output. This means they can't define the same class.
If you want to have a different version of the same class in the two flavor you'll need to create it in both flavors.
src/flavor1/java/com/foo/A.java
src/flavor2/java/com/foo/A.java
And then your code in src/main/java can do
import com.foo.A
depending on the flavor selected, the right version of com.foo.A is used.
This also means both version of A must have the same API (at least when it comes to the API used by classes in src/main/java/...
Edit to match revised question
Additionally, it's important to put the same A class only in source folders that are mutually exclusive. In this case src/flavor1/java and src/flavor2/java are never selected together, but main and flavor1 are.
If you want to provide a different version of an activity in different flavor do not put it in src/main/java.
Do note that if you had 3 flavors and only wanted a custom one for flavor1, while flavor2 and flavor3 shared the same activity you could create a common source folders for those two other activities. You have total flexibility in creating new source folders and configuring the source set to use them.
On to your other points:
It's normal that the 2nd flavor source folder is not blue. You need to switch to the 2nd flavor to enable it, and then you'll be able to create packages and classes inside. Until then, Studio doesn't consider it to be a source folder. We'll hopefully improve this in the future to make the IDE aware of those unactive source folders.
I think it's also normal that you can't create resource files in the res folder. The menu system hasn't been updated to deal with all these extra resource folders. This will come later.
Android Studio build flavors - How to have same source files in diverse flavors
I think you can't have same class in main flavor and your other flavor. you should just create another flavor, then move your Hello
class from main flavor to that new flavor. this rule is just for .java files. I mean you can have an xml file in main flavor and another version in your custom flavor but you can't do this with java files.
here is a useful link with further explanation.
Build.gradle flavor sources
Instead of configuring sourceSets
within the productFlavors
block, try:
productFlavors{
flavor1{
}
flavor2{
}
}
sourceSets{
main{
java.srcDirs = ['src/main']
//other typical sourceSets stuff
}
flavor1.java.srcDirs = ['src/flavor1']
flavor2.java.srcDirs = ['src/flavor2']
}
Related Topics
Android: How to Check If a View Inside of Scrollview Is Visible
How to Add Manifest Permission to an Application
Clicking Urls Opens Default Browser
How to Lay Out Views in Relativelayout Programmatically
How to Have Onscrolllistener for a Scrollview
Listen to Volume Buttons in Background Service
Android: Broadcast Receiver for Screen on and Screen Off
Noclassdeffounderror For Code in an Java Library on Android
How to Detect Android App Uninstall
Programmatically Switching Off Android Phone
API Key for Gcm Is Suddenly Invalid? Unauthorized (401) Error
How to Sort Firebase Records by Two Fields (Android)
How to Animate Recyclerview Items When They Appear
How to Use Disk Caching in Picasso
Android Listview Selector Color