androidgradleskype-for-businessskypedeveloper

Declare variable into build.gradle that match the applicationId with flavors suffix


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 :

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 :

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.


Solution

  • 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.