简体   繁体   English

Android-如何获取Flavor的ApplicationId

[英]Android - How To Get Flavor's ApplicationId

I'm building an app wit different build variant flavors. 我正在构建具有不同构建变体口味的应用程序。 The flavors are "Free" and "Paid". 口味为“免费”和“付费”。 I want to create some logic on my java classes which should only get triggered if the app is "Paid". 我想在我的Java类上创建一些逻辑,这些逻辑仅在应用程序为“付费”时才被触发。 Therefore I need a way to get the "applicationId" set during the gradle build process as shown below: 因此,我需要一种方法来在gradle构建过程中设置“ applicationId”,如下所示:

gradle.build gradle.build

    productFlavors {
    free {
        applicationId "com.example.free"
        resValue "string", "app_name", "Free App"
        versionName "1.0-free"
    }
    paid {
        applicationId "com.example.paid"
        resValue "string", "app_name", "Paid App"
        versionName "1.0-paid"
    }

Once I have the application ID I could do something like this: 获得应用程序ID后,我可以执行以下操作:

    if(whateverpackageid.equals("paid")) {
      // Do something or trigger some premium functionality.
    } 

Am I correct to say that during the gradle build process the "applicationId" eventually becomes the "package name" once the app has been compiled? 我是否正确地说,在gradle构建过程中,一旦编译了应用程序,“ applicationId”最终将成为“程序包名称”? If so what is the best way to get either the "application ID" or "package name" so that I can implement some flavor dependent logic in my java files? 如果是这样,什么是获取“应用程序ID”或“包名称”的最佳方法,以便我可以在Java文件中实现一些依赖于口味的逻辑?

I would use build configuration variables in your product flavors. 我会在您的产品中使用构建配置变量。 Something along the lines of: 类似于以下内容:

productFlavors {
    free {
        applicationId "com.example.free"
        resValue "string", "app_name", "Free App"
        versionName "1.0-free"
        buildConfigField "boolean", "PAID_VERSION", "false"
    }
    paid {
        applicationId "com.example.paid"
        resValue "string", "app_name", "Paid App"
        versionName "1.0-paid"
        buildConfigField "boolean", "PAID_VERSION", "true"
    }
}

Then after a build you can use: 然后,在构建之后,您可以使用:

if (BuildConfig.PAID_VERSION) {
    // do paid version only stuff
}

You may have to do a sync/build on gradle after you add the attribute before you can compile and import the BuildConfig class that Gradle generates on your behalf. 添加属性后,可能必须在gradle上进行同步/构建,然后才能编译和导入Gradle代表您生成的BuildConfig类。

I found best solution to get all values like APPLICATION_ID, BUILD_TYPE, FLAVOR, VERSION_CODE and VERSION_NAME . 我找到了获取所有值的最佳解决方案,例如APPLICATION_ID,BUILD_TYPE,FLAVOR,VERSION_CODE和VERSION_NAME

Just write : Log.d("Application Id : ",BuildConfig.APPLICATION_ID); 只需编写:Log.d(“ Application Id:”,BuildConfig.APPLICATION_ID); in your code. 在您的代码中。 It will provide APPLICATION_ID of your flavor. 它将提供您的口味的APPLICATION_ID。

BuildConfig.java BuildConfig.java

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "";
}

For more details you can refer this link : http://blog.brainattica.com/how-to-work-with-flavours-on-android/ 有关更多详细信息,您可以参考以下链接: http : //blog.brainattica.com/how-to-work-with-flavours-on-android/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:产品风格,applicationId(程序包名称)和清单 - Android: product flavor, applicationId (package name) and manifest 使用风味维度时设置 Android applicationId 的正确方法是什么? - What is the correct way to set Android applicationId when using flavor dimensions? 检索不同口味的applicationId? - Retrieve the applicationId of a different flavor? 有没有办法在AndroidManifest文件中获取当前产品风格的applicationId? - is there a way to get the applicationId of current product flavor in AndroidManifest file? 如何使用flavorDimensions为每种口味组合设置不同的applicationId? - How to set different applicationId for each flavor combination using flavorDimensions? GoogleServices - 如何使用flavorDimensions 为每种风味组合设置不同的applicationId? - GoogleServices - How to set different applicationId for each flavor combination using flavorDimensions? 如何使用 JNI android 获取应用程序包名称或 applicationId - How to get app package name or applicationId using JNI android 如何明智地构造一个Android applicationId - How to construct an Android applicationId wisely 如何更改 Android Studio 的默认构建风格? - How to change Android Studio's default build flavor? 使用 Gradle Kotlin-DSL 时,如何使用 flavorDimensions 为每个风味组合设置不同的 applicationId? - How to set different applicationId for each flavor combination using flavorDimensions when using Gradle Kotlin-DSL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM