1

I have a value for a String in String.xml

<string name="id">4</string>

and I have a class which contains a variable

public static int Id=1;

Now what I need is I want to get either of these two values in the gradle, which will check a condition and based on the condition it will rename my app. Below given is the part of the gradle code

def identifier //here i need to get value from the java or xml 
switch(identifier)
 {
  case 1: 
   temp="ApplicationNewName";break;
  }
 newName=newName.replace("-release",temp);
output.output.File=new File(output.outputFile.parent,newName);

My question is that, Can i access the variables initialised in the java file or string xml in gradle file ?

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185

1 Answers1

0

You're approaching this problem backwards, Gradle gives you the ability to set those variables within the script itself and then you can further access those variables throughout your Android code. Here's a relevant answer for how you can set build configuration variables: https://stackoverflow.com/a/17201265/2168085

It also sounds like you are trying to build different apps from a single code base, or build variations of those apps. If that's the case then you should really look into build flavors to solve this problem. Essentially a build flavor allows you build different apps from a single main code base and apply variations or new functionality to the different flavors. This can be as basic as having a free and paid version of an app or a full white label code base where you can build very different apps from the same master code base. In Android these are more commonly known as build variants and the developer documentation gives plenty of good information on how to get started: https://developer.android.com/studio/build/build-variants.html

Community
  • 1
  • 1
CodyEngel
  • 1,480
  • 14
  • 21