android - 将变量声明到 build.gradle 中,该变量与带有 flavors 后缀的 applicationId 相匹配

标签 android gradle skype-for-business skypedeveloper

我不知道如何在我的 resValue 中设置资源值 ( build.gradle ),取决于构建变体选择

这里稍微解释一下。

我正在使用 Skype For Business SDK(以下称为 Sfb),在实现过程中,它要求我添加一个名为 ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE 的资源值。

所以我查看了他们的应用示例(available here),发现在 build.gradle 中他们添加了如下相应的值:

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

此值然后在 SfbSDK 类中使用,检查它是否与应用程序包名称匹配。

这是我的麻烦,我使用不同的 flavorDimensions,如下面的 build.gradle 所述。

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

根据我的构建变体选择,这将为我生成 6 个不同的 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

所以当进行匹配检查时,我的应用程序因消息错误而崩溃

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)

当然,因为 com.tsp.testcom.tsp.test.a.alpha(或任何其他 APK)不匹配。

如何根据选择的与正确的应用程序包名称匹配的构建变体实现动态 resValue

编辑:

稍微解释一下。首先,我选择构建变体如下:

  • 客户:A
  • 版本:Alpha

然后,在我的 RootActivity#onCreate()(我的启动器 Activity )中,我开始使用依赖于 SfbSDK 的应用程序实例配置 SfbSDK:

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

getInstance() 方法的某处,SfbSDKequals()context.getPackageName() 之间执行 context.getString(string.ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE);

所以,为了调试,在我的 RootActivity#onCreate() 中我只写了这两行

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*

这不匹配!所以在 SfbSDK 中条件没有通过。

最佳答案

好吧,感谢 Radesh 和 this link他提供给我,我找到了我的解决方案。

我从 defaultConfig block 中删除了 resValue 然后我在 android 插件任务中添加了一个新 block ,它创建了 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

对于变体 customer Aversion Alpha,我得到 resValue = com.tsp。测试.a.alpha.

关于android - 将变量声明到 build.gradle 中,该变量与带有 flavors 后缀的 applicationId 相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51040002/

相关文章:

azure - ADSTS50011 : The reply address does not match the reply addresses configured for the application

android - Skype for business android SDK 问题,无法加载 native 库

Android Studio 构建风格

java - 高效地使用 ConcurrentHashMap?

android - 如何高效显示连续位图?

android - 处理调试资源时出现 AAPT2 资源错误

SpringBoot - @Value 在自定义 Spring 配置位置的 JUnit 测试期间不起作用

android - Gradle 同步时此 normalizeAndCheck(UnixPath.java) NullPointerException 的修复方法是什么?

azure - 从控制台应用程序使用 Skype for Business Online 发送 IM

java - 按名称获取类(class)