How I pass params from Jenkins to build.gradle? - android

I want to pass value to my build.gradle file through jenkins (build with params).
I have a String and I need to use this String params to make my release ?

Parametrized builds provide those parameters as environmental variables, like $PARAM_NAME. Use your favorites gradle method of getting environmental variables, like System.getenv('PARAM_NAME') or environmentString = "$System.env.PARAM_NAME"

In General configuration set "This project is parameterized" checkbox to true, and set String/stash/choice/int parameter as you wish for , the name field is your variable name. In your dev project , in the gradle file just grab this variable by calling simple getEnv method.
String inputFromJenkins = System.getenv('VARIABLE_NAME')
println('VARIABLE_NAME = ' + inputFromJenkins)
for extra documentation: gradle env variables documentation

You can use the plugin This build is Parameterized and select string Parameter int that. In this you can define parameter name and value. Suppose PARA is your parameter name and value is its default value, then you can pass this value to build.gradle as $PARA. For more details see https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build.

Related

Getting version code for About screen

Is there any way to get the version code from my project settings to use on my About screen?
Project structure/app/Flavours has both a Version Code and a Version Name. Is there any way I can "inject" either one into a text field that appears on my About screen? I'm thinking of the same technique you use to put variables in other text messages, like "Order number %1$s has been deleted as per your request" which gets filled in via
getString(R.string.message01, orderNumber)
or something similar.
If the technique for the version code is the same, what would I use to get the version code or name in the above code?
I'm running Android Studio 3.1.4.
You can access your version name and code by using the BuildConfig class.
int code = BuildConfig.VERSION_CODE;
String name = BuildConfig.VERSION_NAME;
Or as you want to use it:
<string name="version_example">App version: %1$s</string>
getString(R.string.version_example, BuildConfig.VERSION_NAME)
The BuildConfig class gets auto-generated at compile time depending on your Gradle config. You can also use it to define custom fields, see https://developer.android.com/studio/build/gradle-tips#simplify-app-development

How to configure extra properties to load in a android app initialization?

I want to setup a variable SERVER_URL and it'll be switched between environments production, test, development.
What I want to do:
#Override
protected void onCreate(Bundle bundle) {
R.urls.SERVER_URL; // is it a valid approach using resources?
}
Is there a way to switch environments(dev, prod, test) without change the code?
What's the best approach to implement this behavior?
Is there a way to configure it in the playstore my variable(SERVER_URL) or must I implement only in code?
There are 2 ways you can do it:
1/ By string resource like you want
Add a resource file called secret_keys.xml or whatever name to separate it from other resources file. Put your keys, api endpoints here as the normal string resource, remember to add translatable="false" if you don't want to mess with localization.
Place that file in app/debug/res/values/. Create a new debug folder if it doesn't exist. Do the same for staging or release, Android will automatically use the folder with the same name as the build type.
2/ By properties files
Create 3 .properties files and put your keys inside:
HOST="http://api.blablabla.com"
CLIENT_ID="hahaha"
CLIENT_SECRET="hehehe"
Bind it to BuildConfig variable in your app build.gradle, do the same for other build types:
def getPropertiesFile = { path ->
Properties properties = new Properties()
properties.load(new FileInputStream(file(path)))
return properties
}
android {
...
buildTypes {
debug {
...
getPropertiesFile('./config/development.properties').each { p ->
buildConfigField 'String', p.key, p.value
}
}
...
}
}
In your app just call BuildConfig.HOST to get the string you want
UPDATE
Ignore these config files in .gitignore:
app/config
secret_keys.xml
You can use different approaches. Ideally you shouldn't change URL at Runtime to minimize the attack surface. This approach could have direct impact on your app's security.
If your target is to modify this URL without touching code, you can do can bind this value at Compile time. You can create application.properties file and modify this file for different target builds (dev,production,test). In your code, you can read the values from properties file instead of hardcoded value. You can place this file in your assets folder and apply necessary obfuscation. This way only the properties file will change and your app's security remains intact.
Another way would be provide this parameter at build time (when you execute gradlew command). You can add commandline parameters which would be added to BuildConfig. In your code, you can simply refer to URL by calling BuildConfig.SERVER_URL. You can follow this SO to achieve this.
In either case I would recommend you to bind this value at compile time.

Access a variable in Java or string.xml from Gradle

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 ?
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

Member variable prefix ("m") in getters and setters in Android studio 1.1

How do I configure Android Studio "v 1.1" to correctly generate getters and setters for member variables with prefixes and not to generate getters like: getmName() and instead generate getName()?
I've looked at many questions like : Intellij (Android studio) member variable prefix, but it seems to be removed in "Version 1.1"
Am i right?
in preferences -> code style -> java you'll find the tab code generation. There you will find a matrix of text box, where you can define prefix and suffix for field, static field, parameter and local variable. Enter m for the pair (Name Prefix, Field) and it should work
Editor->Code Style->Java, then last tab 'Code Generation'
write m in field's 'Name Prefix' column.
You must to use names without 'm' in start all private values.
http://jakewharton.com/just-say-no-to-hungarian-notation/

How can I get user defined Environment variables?

I would like to use self defined Environment variables in my source code. I use System.getenv() to do this and the code line looks like this. Log.d("MyTest","== MyEnv " + System.getenv("AP") + " ANDROID_ASSETS:" + System.getenv("ANDROID_ASSETS"));
before I execute my code I define my AP variable with export: export AP="12345" and the this is my output of set command
ANDROID_ASSETS=/system/app
ANDROID_BOOTLOGO=1
ANDROID_DATA=/data
ANDROID_PROPERTY_WORKSPACE=9,32768
ANDROID_ROOT=/system
AP=12345
...
Then I execute my code and I get this line from logcat
D/MyTest( 5363): == MyEnv null ANDROID_ASSETS:/system/app
The value for my defined Environment variable is null. Any suggestions on why it didn't work?
Export command works for one session only, not for a whole system. You can't set environment variables that way. getprop/setprop doesn't work neither.
I had similar problem and found that easiest way to pass some arguments from console is to do something like:
echo "12345" > /sdcard/myapp/args/AP
Then read this file from Java.

Categories

Resources