3

I don't figure out how can I set a resource value (resValue) in my build.gradle, depending on the build variant selection.

Here a bit of explanation.

I'm working with the Skype For Business SDK (hereafter referred to as Sfb) and during is implementation, it ask me to add a resource value named ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE.

So I was looking in their application example (available here) and found that in build.gradle they have added as follow the corresponding value :

android {
...
   defaultConfig {
      applicationId "com.microsoft.office.sfb.sfbdemo"
      ...
      resValue ("string", "ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE", "${applicationId}"
   }
   ...
}

This value then used in a SfbSDK class, checking if it match the application package name.

And here is my trouble, I work with different flavorDimensions as describe in my build.gradle below.

apply plugin: 'com.android.application'
...
android {
   ...
   defaultConfig {
      applicationId "com.tsp.test"
      ...
      resValue ("string", "ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE", "${applicationId}"
   }
   ...
   flavorDimensions("customer", "version")
   productFlavors {
      a {
         dimension "customer"
         applicationIdSuffix ".a"
      }
      b {
         dimension "customer"
         applicationIdSuffix ".b"
      }
      alpha {
         dimension "version"
         applicationIdSuffix ".alpha"
      }
      beta {
         dimension "version"
         applicationIdSuffix ".beta"
      }
      release {
         dimension "version"
         applicationIdSuffix ".release"
      }
   }
}
...

Depending on my build variant selection, this will generate me 6 different APK :

  • com.tsp.test.a.alpha ; com.tsp.test.a.beta ; com.tsp.test.a.release
  • com.tsp.test.b.alpha ; com.tsp.test.b.beta ; com.tsp.test.b.release

So when the match checking is does, my application crash with the message error

Caused by: java.lang.RuntimeException: ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE string not set to applicationId
                      at com.microsoft.office.sfb.appsdk.Application.initialize(Application.java:110)
                      at com.microsoft.office.sfb.appsdk.Application.getInstance(Application.java:144)
                      at com.tsp.test.RootActivity.onCreate(RootActivity.java:89)

Of course, because com.tsp.test doesn't match com.tsp.test.a.alpha (or any other APK).

How can I achieve a dynamic resValue depending on the build variant selected that match the right application package name ?

EDIT :

To explain a bit more. First I choose the Build Variants as follow :

  • Customer : A
  • Version : Alpha

Then, in my RootActivity#onCreate() (my launcher activity), I start to configure the SfbSDK with an application instance depending on the SfbSDK :

this.mApplication = com.microsoft.office.sfb.appsdk.Application.getInstance(this.getApplication().getApplicationContext());

Somewhere in getInstance() method, the SfbSDK do an equals() between the context.getPackageName() and context.getString(string.ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE);

So, for debug, in my RootActivity#onCreate() I just wrote this two lines

String whatIWant = this.getPackageName(); // give me *com.tsp.test.a.alpha*
String whatIGet  = this.getString(R.string.ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE); // give me *com.tsp.test*

This doesn't match ! So in SfbSDK the condition wasn't passed.

tspckr
  • 301
  • 1
  • 5
  • 16

2 Answers2

3

Well, thanks to Radesh and this link he provided to me, I've found my solution.

I removed resValue from the defaultConfig block then I added a new block in the android plugin task, that create the resValue for each variant.

apply plugin: 'com.android.application'
...
android {
   ...
   defaultConfig {
      applicationId "com.tsp.test"
      ...
   }
   ...
   flavorDimensions("customer", "version")
   productFlavors {
      a {
         dimension "customer"
         applicationIdSuffix ".a"
      }
      b {
         dimension "customer"
         applicationIdSuffix ".b"
      }
      alpha {
         dimension "version"
         applicationIdSuffix ".alpha"
      }
      beta {
         dimension "version"
         applicationIdSuffix ".beta"
      }
      release {
         dimension "version"
         applicationIdSuffix ".release"
      }
   }
   ...
   applicationVariants.all { variant ->
      variant.resValue "string", "ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE", "\"${applicationId}\""
   }
}
...

This generate correctly the resValue with the package name of the selected variant.

For variant customer A and version Alpha, I get resValue = com.tsp.test.a.alpha.

tspckr
  • 301
  • 1
  • 5
  • 16
1

you must declear variable ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE in gradle.properties like below

//default
org.gradle.jvmargs=-Xmx1536m
ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE=xxx
Radesh
  • 13,084
  • 4
  • 51
  • 64
  • And what should I put for xxx ? Because I have already seen proposals like that, the fact is I want to set the right package name depending on the build variant. – tspckr Jun 26 '18 at 11:47
  • in what i see in your code you must replace xxx with your Application Id (com.tsp.test) – Radesh Jun 26 '18 at 12:04
  • Ok, I get `com.tsp.test` (already try this solution) and next how I get the whole package name depending on build variant ? If I choose build variant with *customer* ***A*** and *version* ***Alpha***, my package name isn't `com.tsp.test` but `com.tsp.test.a.alpha` – tspckr Jun 26 '18 at 12:09
  • 1
    oh, that get hard ! check this link . https://blog.davidmedenjak.com/android/2016/11/09/build-variants.html – Radesh Jun 26 '18 at 12:25
  • Yes, I agree, a bit harder ! I will dig your link, thanks ! – tspckr Jun 26 '18 at 12:29