Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use final resolved applicationId in build.gradle

Say I have the following 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"
        }
    }
}

I want to add the resulting application id to strings.xml, like this:

resValue "string", "app_package", applicationId

So I can then use this value in intents targetPackage defined in preferences xml

But the value changes depending on what block I put that line in:

  • defaultConfig -> "com.example.base" (always the base Id, useless)
  • productFlavours.free -> "com.example.free" (problem is this does not change to "com.example.free.dev" for debug builds)
  • productFlavours -> Error, does not build
  • buildTypes.debug -> Error, does not build

By using the applicationIdSuffix, I need gradle to resolve the final id before I can use it. How do I do that?

like image 776
rockgecko Avatar asked Feb 28 '17 05:02

rockgecko


People also ask

What is applicationId in build Gradle?

Every Android app has a unique application ID that looks like a Java or Kotlin package name, such as com. example. myapp. This ID uniquely identifies your app on the device and in the Google Play Store.

How do I change a variant in build?

You can change the build variant to whichever one you want to build and run—just go to Build > Select Build Variant and select one from the drop-down menu. To start customizing each build variant with its own features and resources, however, you'll need to know how to create and manage source sets.

How do I get the current build type in Gradle?

def isCurrentBuildType(buildType) { return gradle. getStartParameter().

What is a buildType in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.


1 Answers

Add this after productFlavors within android DSL:

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

In freeDebug build this will be added in app/intermediates/res/merged/free/debug/values/values.xml:

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

In paidDebug:

<string name="app_package" translatable="false">com.example.paid.dev</string>
like image 199
azizbekian Avatar answered Oct 25 '22 04:10

azizbekian