How to import one class from other build flavour

1.8k views Asked by At

My project structure look like below

+ src
        + main // this is my common code
            + java 
                - LoginScreen // depending on condition launch screen from flavor1 or flavor2 
            + res
        + flavor1
            + java
            + res
        + flavor2
            + java
            + res

I have Login class in main/src depending on condition, have to launch the screen from flavor1 or flavor2.

For example:

class LoginScreen{
......

    if(true){
        // launch ScreenUser from Flavor1 reset of screen flows from falvour1
    }else{
       // launch ScreenOTP from Flavor2 reset of screen flows from falvour2
    }
}

In this case, if I make build for flavor1 it is showing an error for falvor2 class and vice versa.

Both flavors has different applicationId like applicationIdSuffix ".flavor1"

Is this possible? If not how can I achieve this?

2

There are 2 answers

5
NonGrate On

One of the possibilities to do that, if you want to have only one class with the if that check your buildType/productFlavor - you can set the value of BuildConfig field in both buildTypes/productFlavors by adding:

productFlavors {
  flavor1 {
    buildConfigField "String", "BUILD_FLAVOR", "Flavor1"
  }
  flavor2 {
    buildConfigField "String", "BUILD_FLAVOR", "Flavor2"
  }
}

And then you can use it in the code:

class LoginScreen {
......

  switch(BuildConfig.BUILD_FLAVOR) {
    case "Flavor1":
      // launch ScreenUser from Flavor1
      break;
    case "Flavor2":
      // launch ScreenOTP from Flavor2
      break;
  }
}

Hope, I've helped. Good luck!

3
GeordieMatt On

When I've done this sort of thing I've used interfaces and dependency injection (I use dagger). Something like:

public interface LoginBehaviour {

    void goToNextScreen(Context context);
}

Then define an implementation in each flavour which contains the code to launch the next screen. Something like:

public class FlavourOneLoginBehaviour implements LoginBehaviour {

    @Override
    public void goToNextScreen(Context context) {
        context.startActivity(new Intent(context, ScreenUser.class));
    }

}

And:

public class FlavourTwoLoginBehaviour implements LoginBehaviour {

    @Override
    public void goToNextScreen(Context context) {
        context.startActivity(new Intent(context, ScreenOTP.class));
    }

}

The you need to set up your dependency injection container in each flavour so that it references the local implementation (this will vary depending on which framework you use). After you've done that, you can use the container in the main project to get the correct implementation of the LoginBehaviour interface at run time, and you can call goToNextScreen on it to do your navigation. Dependency injection is a really handy way to ensure that you can swap out parts of your code without having to do major refactoring, and helps a lot with reuse and flexibility. Hope that helps!