首页 > 解决方案 > 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.

标签: androidgradleskype-for-businessskypedeveloper

解决方案


好吧,感谢拉德什和他提供给我的这个链接,我找到了我的解决方案。

resValuedefaultConfig块中删除,然后在android插件任务中添加了一个新块,resValue 为每个 变体创建。

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}\""
   }
}
...

这将正确生成resValue带有所选变体的包名称的 。

对于变体客户 A版本 Alpha,我得到resValue = com.tsp.test.a.alpha.


推荐阅读