简体   繁体   中英

Good Practice Gradle Flavors With Class

I am looking for informations about how I should use gradle flavors to define my behaviors for an Android Application.

Let's say I want to start a new activity in my flavor1 and not in my flavor2.

As gradle doesn't override classes with each other,

I have to, define the Activity in each flavors

app/src/
     |-> flavor1/java/<package>/Activity.java
     |-> flavor2/java/<package>/Activity.java
     |-> main/java/<package>

or, define it in my main folder and use an enum in a Constant class which return if I am in my flavor1 or my flavor2

app/src/
     |-> flavor1/java/<package>/Constant.java
     |-> flavor2/java/<package>/Constant.java
     |-> main/java/<package>/Activity.java

My guess is that I have to mix both solutions:

  • If I want to create a unique activity in flavor1, I have to create this same class in flavor2 and maybe just throw RuntimeException("stub!")
  • If I want to call this activity from main , I have to use something like if (Constant.getFlavor() == flavor1)

In my opinion, this is the coolest way to use gradle flavors. But I want to be sure that there is no other best ways, so far discovered, to deal with it before the final implementation.

Is there anyone who blogged something about it or want to give his own opinion ? I would love to read you!

Thanks in advance!

You can place different AndroidManifest files on each flavor. For example you can define the (let's call it) optional Activity inside the flavor1, the Class that extends the optional Activity just inside the flavor1 and you're done! Please note that the flavor AndroidManifest file must specify only what is different from the main AndroidManifest: in this case, you should include just the <activity/> (of course inside an empty <application/> )

If you wanted to avoid the if (Constant.getFlavor() == flavor1) you could create a factory class which then returns the correct intent for that flavours Activity. Then override this in each flavour. This avoids polluting the code with if statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM