【问题标题】:How to use final resolved applicationId in build.gradle如何在 build.gradle 中使用最终解析的 applicationId
【发布时间】:2017-07-18 22:38:54
【问题描述】:

假设我有以下 build.gradle

android{
    defaultConfig{
        applicationId "com.example.base"
    }
    buildTypes{
        release{
            minifyEnabled true
        }
        debug{
            applicationIdSuffix ".dev"
            minifyEnabled false
        }
    }
    productFlavors{
        free{
            applicationId "com.example.free"
        }
        paid{
            applicationId "com.example.paid"
        }
    }
}

我想将生成的应用程序 ID 添加到 strings.xml,如下所示:

resValue "string", "app_package", applicationId

然后我可以在首选项 xml 中定义的意图 targetPackage 中使用此值

但值会根据我将该行放入的块而变化:

  • defaultConfig -> “com.example.base”(始终是基本 ID,无用)
  • productFlavours.free -> "com.example.free"(问题是这个不会改变为调试版本的“com.example.free.dev”)
  • productFlavors -> 错误,无法构建
  • buildTypes.debug -> 错误,无法构建

通过使用 applicationIdSuffix,我需要 gradle 解析最终的 id 才能使用它。我该怎么做?

【问题讨论】:

标签: android gradle android-gradle-plugin android-resources android-flavors


【解决方案1】:

在android DSL 内的productFlavors 之后添加:

applicationVariants.all { variant ->
    variant.resValue  "string", "app_package", variant.applicationId
}

在freeDebug 构建中,这将被添加到app/intermediates/res/merged/free/debug/values/values.xml:

<string name="app_package" translatable="false">com.example.free.dev</string>

在paidDebug:

<string name="app_package" translatable="false">com.example.paid.dev</string>

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 2023-01-09
    相关资源
    最近更新 更多