How can I specify a default value for this simple build.gradle script:
println "Hello $build_version"
So that I don't get the error:
A problem occurred evaluating root project 'hello_gradle'.
> Could not find property '$build_version' on root project 'hello_gradle'.
I tried some of the operators, checking for nulls etc, but I think just the reference to the property makes it fail. I could fix that by always providing the property, but that's less than ideal.
gradle -Pbuild_version=World
if (!project.hasProperty("build_version")) {
ext.build_version = "1.0"
}
This checks if the property exists and assigns a default value if not:
def build_version=project.properties['build_version'] ?: "nokey"
Starting from Gradle 2.13:
ext.buildVersion = project.findProperty('build_version') ?: '1.0'
This worked for me:
def AWS_ACCESS_KEY="nokey"
def AWS_SECRET_KEY="nokey"
if (project.hasProperty("AWS_ACCESS_KEY")) {
AWS_ACCESS_KEY=project.get("AWS_ACCESS_KEY")
}
if (project.hasProperty("AWS_SECRET_KEY")) {
AWS_SECRET_KEY=project.get("AWS_SECRET_KEY")
}
Another way to make it short and nice:
ext.buildVersion = project.getProperties().getOrDefault("build_version", "1.0")
I'm adding this to my build.gradle:
String propValue(String propName, String defValue) {
(project.hasProperty(propName) && project.getProperty(propName)) ? project.getProperty(propName) : defValue
}
then use when needed propValue('build_version', 'nokey').
Have you tried this?:
println "Hello ${project.getProperty('build_version', 'default_string_value')}"
One-liner using ternary operator:
println "Hello ${project.hasProperty('build_version') ? getProperty('build_version') : 'World'}"
gradle <your_task> -Pbuild_version=SomethingElse
Related
i cloned kickstarter from github & facing this error
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
useIR = true
}
i have tried rebuilding , cleaning project but cant resolve this issue
& there are no similar issues i found out there
A problem occurred evaluating project ':app'.
Could not set unknown property 'useIR' for object of type org.jetbrains.kotlin.gradle.dsl.KotlinJvmOption
// Copy google-services.json from variant directory to root of app
gradle.taskGraph.beforeTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.name}GoogleServices/) {
copy {
from "src/${variant.name}"
into '.'
include 'google-services.json'
}
}
}
}
}
i am not sure if this might help but this error cannot reslove 'task' is in build gradle & this is the only error
Remove the
UseIR = true
option and your code should compile
Which kotlin version you are using? It seems to be removed in 1.7.
https://kotlinlang.org/docs/compatibility-guide-17.html#remove-useir-compiler-option.
I want to suppress Javadoc warnings in the output, because it fails the automatic builds.
task clientApiDocs(type: Javadoc) {
source = sourceSets.main.allJava
destinationDir = reporting.file("javadoc")
classpath = configurations.compile
options.addBooleanOption('Xdoclint:none', true)
}
This task will print some warnings:
User.java:124: warning - #param argument "password" is not a parameter name.
StateListener.java:17: warning - #author: is an unknown tag.
There are many of them, and It's not possible to fix them.
I tried to add options.addBooleanOption('Xdoclint:none', true) and options.addStringOption('Xdoclint:none', '-quiet') but this does not help. There are still Javadoc warnings in concole.
However, comparing to Ant, option <javadoc destdir="${dir.build}/doc" additionalparam="-Xdoclint:none"> works pretty well and does not print any warnings.
It seems like there are many of people who tried to solve the same problem, but there is no way to deal with it. E.g. link
What is the possible way or workaround to resolve it?
It's a pity that it cannot be switched off completely, the following 2 ways of reducing do get close though ... in build.gradle.kts, kotlin:
tasks.withType<Javadoc> {
options {
this as StandardJavadocDocletOptions
addBooleanOption("Xdoclint:none", true)
addStringOption("Xmaxwarns", "1")
}
}
tasks.withType<Javadoc> {
(options as StandardJavadocDocletOptions).addBooleanOption("Xdoclint:none", true)
(options as StandardJavadocDocletOptions).addStringOption("Xmaxwarns", "1")
}
I want to evaluate the string expression without using script engine:
string expression can be like:
"((true&&(!false)||true)&&true)&&true"
Anybody have any idea how this can be done in android using kotlin
Thanks
Kotlin Android
app.gradle file
Add a new dependency
dependencies {
implementation 'net.objecthunter:exp4j:0.4.8'
}
main.kt file
var value = TextView.text.toString()
var result = ExpressionBuilder(value).build().evaluate()
I know my answer will not help you in your case where your string expression contain true, false, &&, ||, ! and (). BUT for those who wants to evaluate mathematical expressions I found this library that handles almost all mathematical operators.
Add it to your project and use it this way
// In root build.gradle
repositories {
maven {
url "https://dl.bintray.com/kaendagger/KParser"
}
}
//Add in the dependencies
dependencies{
implementation 'io.kaen.dagger:KParser-jvm:0.1.1'
}
And then in your code you can do something like this
val parser = ExpressionParser()
val result = parser.evaluate("5+1+cos(PI)-2*2/4")
println(result)
i got this error from gradle when i try to build the project after i put the api key in gradle file
android {
.....
buildTypes.each {
it.buildConfigField 'String', 'cedf6......80c62', MyOpenWeatherMapApiKey
}
}
this error i got when i build the project :-
Error:(22, 0) Could not find property 'MyOpenWeatherMapApiKey' on com.android.build.gradle.AppExtension_Decorated#48bee3b8.
Open File
i think i have to make a file in some folder , but i don't know in which folder ? ,
and what i ishould to type inside the file after i create it ?
Since you are using a String you have to use this syntax:
buildConfigField "String" , "OPEN_WEATHER_MAP_API_KEY" , "\"XXXXX-XXXXX-XXX\""
The last parameter has to be a String
Otherwise you can use something like this:
resValue "string", "OPEN_WEATHER_MAP_API_KEY", "\"XXXXX-XXXXX-XXX\""
The first case generates a constants iin your BuildConfig file.
The second case generates a string resource value that can be accessed using the #string/OPEN_WEATHER_MAP_API_KEY annotation.
Credits: Could not find property 'xxxx' on com.android.build.gradle.AppExtension_Decorated
Alternatively, you can use a combination of double quotes and single quotes to avoid the escape characters (where xxx is your API):
buildTypes.each {
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', '"xxxxxxxxx"'
}
In Gradle you need to define subprojects to be built in a 'settings.gradle' file. To build three child projects, you would do something like this:
include "child1", "child2", "child3"
The problem I'm having is that I have quite a few projects to include. Is there a way to use a wildcard in this definition? I'm looking for something like this:
include "*"
That of course does not work. This would be a lot easier to work with since I have many subprojects to include. Is there a way to automatically include subdirectories as projects?
include rootDir.listFiles().findAll {
it.isDirectory()
&& !( it =~ ".*/\\..*") // don't add directories starting with '.'
&& !( it =~ "^\\..*") // don't add directories starting with '.'
}.collect {
it.getName()
}.toArray(new java.lang.String[0])
Did the trick for me
The following code supports a project hierarchy of arbitrary depth:
rootDir.eachFileRecurse { f ->
if ( f.name == "build.gradle" ) {
String relativePath = f.parentFile.absolutePath - rootDir.absolutePath
String projectName = relativePath.replaceAll("[\\\\\\/]", ":")
include projectName
}
}
Can you do something like:
include (1..10).collect { "Child$it" }
To include "Child1" up to "Child10"?
Obviously, you'd need to change the collect to some sort of folder scan, but it that quick test works then the scan has a good chance