How to get Running Android Flavor Name in gradle script - android

This time I have this problem, I am trying to get the current flavor in a gradle script. I have tried the answers given here How to get current flavor in gradle
without luck.
I haven seen in some answers that they use
// Get all flavors
android.productFlavors.all { flavor ->
if (flavor.name.equals("flavorName")) {
// do what you like
}
// ...
}
But also I didn't have any luck with that because i get the following error: > Could not get unknown property 'android' for task
So I don't know how to get the current flavor, any help will be very appreciated thanks!!!
EDIT: What I need to do is to execute a piece of code that is diferent for each flavor, my current idea is to know the selected build variant to do this in a task, but if there is any othe way to do this would be perfect.

I already posted a working solution here, that is:
The following function returns exactly the current flavor name:
def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern;
if( tskReqStr.contains( "assemble" ) )
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher( tskReqStr )
if( matcher.find() )
return matcher.group(1).toLowerCase()
else
{
println "NO MATCH FOUND"
return "";
}
}
You need also
import java.util.regex.Matcher
import java.util.regex.Pattern
at the beginning or your script.
In Android Studio this works by compiling with "Make Project" or "Debug App" button.

You can get this error if your use it out of "android" closure at app level gradle script. Make sure that you use it inside

Related

Gradle task for Sentry not compiling

I have to add the Analytics tool Sentry to our Android project. In order to make it work, one needs to create mappings for the obfuscated code (from Proguard/R8) and upload it later to Sentry.
On the website https://docs.sentry.io/platforms/android/ it is even described how to do that.
There it is written that one needs to create a gradle task looking like this:
gradle.projectsEvaluated {
android.applicationVariants.each { variant ->
def variantName = variant.name.capitalize();
def proguardTask = project.tasks.findByName(
"transformClassesAndResourcesWithProguardFor${variantName}")
def dexTask = project.tasks.findByName(
"transformClassesWithDexFor${variantName}")
def task = project.tasks.create(
name: "processSentryProguardFor${variantName}",
type: Exec) {
workingDir project.rootDir
commandLine *[
"sentry-cli",
"upload-proguard",
"--write-properties",
"${project.rootDir.toPath()}/app/build/intermediates/assets" +
"/${variant.dirName}/sentry-debug-meta.properties",
variant.getMappingFile(),
"--no-upload"
]
}
dexTask.dependsOn task
task.dependsOn proguardTask
}
}
This shall wait until Proguard is finished, than copy this properties file to the assets. However, when I add this to my Android gradle script I get the error:
Could not create task
':app:processSentryProguardForPlayStoreStagingDebug'.
No signature of method: java.util.ArrayList.multiply() is applicable for argument types: (ArrayList) values: [[sentry-cli, upload-proguard,
--write-properties, {Application-Path}/app/build/intermediates/assets/playStoreStaging/debug/sentry-debug-meta.properties,
...]] Possible solutions: multiply(java.lang.Number),
multiply(java.lang.Number)
I assume there is something wrong with the multiplication symbol * before the commandLine array. But when I remove it I get the error
Could not create task
':app:processSentryProguardForPlayStoreStagingDebug'.
Cannot cast object 'sentry-cli' with class 'java.lang.String' to class 'int'
So I tried to test this with only that line
commandLine "sentry-cli", ...
Which gave me another error
What went wrong: Cannot invoke method dependsOn() on null object
Thus I assume something went really wrong with that gradle script since it seems the dependend task can't be found. Does anyone have any idea how to fix this (or optionally have any other idea how to copy that sentry-debug-meta.properties file to my assets in another way, once Proguard/R8 is finished)?
Thanks!
-------- EDIT --------
I noticed something important.
The gradle tasks are defined in a different name than what was defined in the manual. Looking at my tasks I have them named
transformClassesAndResourcesWithR8For...
and
transformClassesWithDexBuilderFor...
However, I print the variantName then for checking but it seems my tasks are incomplete.
In my tasks list there exist
transformClassesAndResourcesWithR8ForPlayStoreStagingDebug
but not
transformClassesAndResourcesWithR8ForPlayStoreStagingRelease
and thus the task can't be found. I think that is the real problem here. So where are these gradle tasks defined?
------- EDIT 2 --------
Okay I noticed something strange here. Some variants don't have tasks. It makes sense that DEBUG tasks don't have R8 tasks but I found this here:
Variant: PlayStoreStagingRelease DexTask is null
Variant: PlayStorePreviewRelease DexTask is null
Variant: HockeyAppRelease DexTask is null
Variant: LocalServerRelease DexTask is null
Variant: PlayStoreProductionRelease DexTask is null
So how can this be?
I'd recommend using the Sentry Gradle integration (Gradle plugin) which is described here https://docs.sentry.io/platforms/android/#gradle-integration
The official Android Gradle plugin changed its task names over versions, Gradle version also affects those code snippets.
Google also replaced Proguard with R8 and it also affected those code snippets.
Is there a reason why not using the Sentry Gradle integration? if so, We'll be looking into updating them.
Thanks.
java.util.ArrayList.multiply() hints for that * in front of the [ ] list, which looks strange to me. Try removing the *[ ], only keeping List<String> (there's no ArrayList expected, to begin with):
commandLine "sentry-cli", "upload-proguard", "--write-properties", "${project.rootDir.toPath()}/app/build/intermediates/assets/${variant.dirName}/sentry-debug-meta.properties", variant.getMappingFile(), "--no-upload"
You'd have to look up how your tasks are actually being called, but it should be something alike:
def r8Task = project.tasks.findByName("transformClassesAndResourcesWithR8For${variantName}")
def d8Task = project.tasks.findByName("transformClassesWithDexBuilderFor${variantName}")
With a null check, because not every variant might have minifyEnabled true set:
if(r8Task != null) {
d8Task.dependsOn task
task.dependsOn r8Task
}
Maybe even a previous null check is required, because variant.getMappingFile() needs R8.
And that some flavors have no D8 task might be based upon the absence of code (nothing to do).
Here's a summary of the steps that I followed for integrating Sentry with my Android app. These steps are to ensure the sentry gradle plugin works as expected and automatically uploads the proguard mapping files, without you having to worry about uploading using cli. I assume you would have setup the Sentry SDK as described here:
https://docs.sentry.io/platforms/android/#integrating-the-sdk
Ensure you have Android Studio gradle plugin 3.5.0 (Not 3.6.x, that seems to break the sentry plugin. I observed that the sentry proguard or native symbol upload tasks are not configured or executed at all). This value should be in your root project's build.gradle in dependencies block
Provide a sentry.properties file the root folder of your project. The sentry.properties file should have the following values at minimum:
defaults.project=your_sentry_project_name
defaults.org=your_sentry_org_name
auth.token=sentry_project_auth_token
You can get info about generating auth tokens here: https://sentry.io/settings/account/api/auth-tokens/
(Optional: If you have build flavors) In my case, I have different flavors for my app. So, I had to put the sentry.properties inside my flavor specific folder in /app/src/ folder. Then, I wrote a gradle task to copy the flavor specific sentry.properties file into the project's root folder during gradle's configuration phase. Example:
task copySentryPropertiesTask {
if (getBuildFlavor() != null && !getBuildFlavor().isEmpty()) {
println("Copying Sentry properties file: ${getBuildFlavor()}")
copy {
from "src/${getBuildFlavor()}/"
include "sentry.properties"
into "../"
}
}
}
def getBuildFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern;
if (tskReqStr.contains("assemble"))
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher(tskReqStr)
if (matcher.find())
return matcher.group(1)
else {
println "NO MATCH FOUND"
return ""
}
}
Note 1: You can place this task in your app/build.gradle anywhere (I had placed it at the end).
Note 2: If you followed step 3 for build flavors, you can also add the root folder's sentry.properties in .gitignore. Since, it will be copied everytime you create a build.
Sentry should now be able to upload the proguard files for any release builds (or if you set minifyEnabled=true for any buildType).

Different style applicationId in multiple dimensions

Problem: setting applicationId depending on flavor.
More problem: Two apps are already on the store, both having a different style of applicationId.
com.name.dimension1.dimension2
com.name.dimension1dimension2 (without dot)
In our Android app we need to introduce new flavors/dimensions.
Dimensions:
flavorDimensions "company", "app", "server"
Seeing that, this is why we cannot use applicationIdSuffix in build.gradle because it is automatically adding . (dot) before suffix.
We already have method to decide which versionCode should be done for every flavor (thanks to that answer on Stack)
def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern
if (tskReqStr.contains("assemble"))
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher(tskReqStr)
if (matcher.find())
return matcher.group(1).toLowerCase()
else {
println "NO MATCH FOUND"
return ""
}
Even more problem: Same method which helps us with setting version code, cannot help with applicationId.
def getFlavorApplicationId() {
def flavor = getCurrentFlavor()
if (flavor.contains("company1") && flavor.contains("app1")) {
return ext.company1app1AppId
} else if (flavor.contains("company2") && flavor.contains("app1")) {
return ext.company2app1AppId
} else if (flavor.contains("company2") && flavor.contains("app2")) {
return ext.company2app2AppId
}
return "nothing"
}
When app is built/synchronized - everything is working properly (file BuildConfig and also generated apk is having correct applicationId).
The problem is occurring when we are trying to Run the app with applicationId is depending on flavor.
Error while executing: am start -n "**non.of.those**/com.rsqtechnologies.rsqphysio.splash.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=non.of.those/com.rsqtechnologies.rsqphysio.splash.SplashActivity }
Error type 3
Error: Activity class {**non.of.those**/com.rsqtechnologies.rsqphysio.splash.SplashActivity} does not exist.
Error while Launching activity
If I understand this well, Android Studio is not taking applicationId to run the app from BuildConfig or even generated .apk. It is trying to generate it while launching app (when it cannot gather information about flavor from gradle task - def getCurrentFlavor())
when I am running the app by myself in Terminal with the same command but correct appId - everything works fine.
Things which I have already tried also:
Solution from this Stack answer
Tried to find a way in Android Studio to edit the configuration so it would take proper appId (failed)
Does anyone have some advice? Pretty complex problem, I can share more details if anyone interested.
When you are using groovy you can change the applicationId for all your merged flavors:
android.applicationVariants.all { variant ->
def isApp1 = variant.name.contains('app1')
def isApp2 = variant.name.contains('app2')
def idAppendix = ""
if (isApp1) idAppendix = ".withDotForApp1"
if (isApp2) idAppendix = "noDotForApp2"
mergedFlavor.setApplicationId(mergedFlavor.applicationId + idAppendix)
}
Note that this won't work if you plan to use kotlinscript since the applicationid of the merged flavor is a val and doesnt offer a setter.

How to encrypt passwords in build.gradle with Android 3.3? Getting "Cannot resolve symbol" in IDE

Since update to Android Studio 3.3 we get a weird warning with some legacy code about encrypted gradle parameters.
In build.gradle we have this line:
apply from: "encryption.gradle"
In encryption.gradle we have this content:
afterEvaluate {
android.applicationVariants.all { variant ->
def pwd = "";
variant.productFlavors.each { flavor ->
if (flavor.ext.has("pwd1")) {
pwd = flavor.ext.pwd1
}
}
if (pwd.isEmpty() && variant.buildType.ext.has("pwd2")) {
pwd = variant.buildType.ext.pwd2
}
variant.resValue 'string', 'pwd', encryptPassword(pwd, variant.signingConfig, variant.applicationId)
}
}
def String encryptPassword(String password, signingConfig, String applicationId) {
...
}
In the code we use it like this:
getString(R.string.pwd)
And since the AS update we get the following error:
Cannot resolve symbol 'pwd'
When compiling/building the project everything runs fine because it can find the parameter. But when working in the IDE, all files that try to use R.string.pwd are marked red and show the error message, which is pretty annoying.
Any way to make this go away easily? SuppressWarnings("all") and SuppressLint("all") are not helping.
Is afterEvaluate the right place to do this or would it be better somehow in the defaultConfig section of build.gradle?
Even i was facing similar issues. so i reverted my gradle to 3.2.1 and it works as expected, it might be a technical glitch/issue with gradle 3.3
While i was on gradle 3.3 i tried invalidating cache for android studio, restarted it etc, but nothing solved the issue.

After upgrading to Android studio 3 (stable version) I get this error

Gradle sync failed:
Manifest Tasks does not support the manifestOutputFile property any more, please use the manifestOutputDirectory instead.
For more information, please check
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html
I opened the link and applied it but still can not solve my problem ..
it was said to add this code in build.gradle module app ..
applicationVariants.all { variant ->
variant.outputs.all { output ->
output.processManifest.doLast {
// Stores the path to the maifest.
String manifestPath = "$manifestOutputDirectory/AndroidManifest.xml"
// Stores the contents of the manifest.
def manifestContent = file(manifestPath).getText()
// Changes the version code in the stored text.
manifestContent = manifestContent.replace('android:versionCode="1"',
String.format('android:versionCode="%s"', generatedCode))
// Overwrites the manifest with the new text.
file(manifestPath).write(manifestContent)
}
}
}
I may apply it in wrong way because no resources that cover this topic.
I also try this answer .. Error:Manifest Tasks does not support the manifestOutputFile property any more, please use the manifestOutputDirectory instead. (Android Studio 3.0)
for all my trials the error still exist ..

Getting package name of application variant in gradle plugin

I'm building a gradle plugin that adds a new task for every application variant. This new task needs the package name of the application variant.
This is my current code, which stopped working with the most recent version of the android gradle plugin:
private String getPackageName(ApplicationVariant variant) {
// TODO: There's probably a better way to get the package name of the variant being tested.
String packageName = variant.generateBuildConfig.packageName
if (variant.processManifest.packageNameOverride != null) {
packageName = variant.processManifest.packageNameOverride
}
packageName
}
This has stopped working in the most recent version of the android plugin because of changes to the build config processing. It seemed like a hack before anyway, so I'm not surprised it stopped working. Is there a canonical way to fetch the package name?
I use this:
// Return the packageName for the given buildVariant
def getPackageName(variant) {
def suffix = variant.buildType.packageNameSuffix
def packageName = variant.productFlavors.get(0).packageName
if (suffix != null && !suffix.isEmpty() && suffix != "null") {
packageName += suffix
}
return packageName
}
The only way I found was to define packageName in the project's build.gradle and then from the plugin do project.android.defaultConfig.packageName.
If the project has flavors that define their own package then the solution stated by stealthcopter would work.
However a plugin is meant to work with any project regarless whether packageName is defined or not in build.gradle so these solutions are far from ideal.
Let me know if you find something better.
This will handle nullable applicationIdSuffix for you:
android.applicationVariants.all { variant ->
def applicationId = [variant.mergedFlavor.applicationId, variant.buildType.applicationIdSuffix].findAll().join()
println applicationId
}

Categories

Resources