A2i_
A2i_

Reputation: 398

Gradle: How to let product flavors inherit from src/main except for one

I want to use product flavors for my android apps since it seems like a big advantage. However there is one thing I don't get solved:

Lets say, I have a project Planets com.example.planets and I want to build an app for every planet:

productFlavors {
    mercury {
        applicationId "com.example.planets.mercury"
    }
    venus {
        applicationId "com.example.planets.venus"
    }
    ...
}

A lot of code is shared and therefore put in the src/main folder.

I also want to use the same MainActivity in every app except for the earth app as it differs in this app. How can I achieve that without copying the non-earth-version in every other folder?

I tried to "override" the MainActivity I mentioned above folder but that didn't work.

Thanks in advance!

Upvotes: 3

Views: 864

Answers (2)

Arnav Gupta
Arnav Gupta

Reputation: 966

I would suggest a file structure like this

    .
└── src
    ├── earth
    │   └── java
    │       └── com
    │           └── example
    │               └── app
    │                   ├── MainActivity.java // extends Activity + your code
    │                   └── SomeClass.java
    ├── main
    │   └── java
    │       └── com
    │           └── example
    │               └── app
    │                   ├── MainBaseActivity.java // extends Activity + your code
    │                   └── SomeClass.java
    ├── mercury
    │   └── java
    │       └── com
    │           └── example
    │               └── app
    │                   ├── MainActivity.java // extends MainBaseActivity + empty body
    │                   └── SomeClass.java
    └── venus
        └── java
            └── com
                └── example
                    └── app
    │                   ├── MainActivity.java // extends MainBaseActivity + empty body
    │                   └── SomeClass.java

Where SomeClass.java represents all your common classes and codes mercury and venus will use the MainActivityfrom "main", while earth has it's own specified.

Upvotes: 3

aga
aga

Reputation: 29416

I suppose you can put non-earth version inside the src/main directory and add earth-related MainActivity inside the source directory of earth flavour. That way, default version of MainActivity will be used by all flavours other than earth.

Upvotes: 0

Related Questions