As per the documentation http://gradle.org/docs/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html
A string dependency notation looks like "group:name:version:classifier#extension"
How do we specify the type (http://ant.apache.org/ivy/history/latest-milestone/terminology.html#type) here?
You can declare like below:
artifacts {
archives file: '', name: '', type: 'jar'
}
and for more information here.
Related
I am building my own ROM and want to use a aar file Library in one system service.
Is there any way this could be achieved?
If you are using Android.bp this can be done via android_library_import module type (link to soong spec).
Also, if you try to grep the AOSP source tree, you will easily find many examples of its use, for example: packages/apps/Settings/Android.bp:
android_library_import {
name: "contextualcards",
aars: ["libs/contextualcards.aar"],
}
android_library {
name: "Settings-core",
....
srcs: ["src/**/*.java"],
static_libs: [
...
"contextualcards",
...
}
I am trying to install a custom archetype in my local repo. When user generate a project based on this archetype, they are required to provide the artifactId. Most of the time it is in all lower case. However, the main Class name (also the java filename) is dependent on this artifactId with the first letter capitalized. Instead of asking user to input another variable, I would like to call some String method to convert the artifactId to the correct format for class name.
In Maven archetype: Modify artifactId, looks like you can embed Java method in archetype-metadata.xml as below:
<requiredProperty key="artifactIdWithUnderscore" >
<defaultValue>${artifactId.replaceAll("-", "_")}</defaultValue>
</requiredProperty>
So I did something similar in my archetype-metadata.xml to capitalize first letter.
<requiredProperty key="artifactId" />
<requiredProperty key="serviceName">
<defaultValue>${artifactId.toLowerCase().substring(0,1).toUpperCase()+actifactId.toLowerCase().substring(1)}</defaultValue>
</requiredProperty>
However I got the following parse error:
SEVERE: Parser Exception: serviceName
org.apache.velocity.runtime.parser.ParseException: Encountered "+artifactId.toLowerCase().substring(1)}" at line 1, column 55.
Was expecting one of:
"[" ...
"}" ...
at org.apache.velocity.runtime.parser.Parser.generateParseException(Parser.java:3679)
What is correct way to insert Java String method in this archetype xml?
The plus character can't be used as string concatenation operator here, it's also not needed. Just concatenate two replacements (without any operator between):
<defaultValue>${artifactId.toLowerCase().substring(0,1).toUpperCase()}${actifactId.toLowerCase().substring(1)}</defaultValue>
<defaultValue>
${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1).toLowerCase()}
</defaultValue>
works for me, but artifactId is coming from the archetype project and not from my argument artifactId as expected...
This will capitalize the first letter.
<requiredProperty key="artifactId" />
<requiredProperty key="serviceName">
<defaultValue>${StringUtils.capitalize("artifactId")}</defaultValue>
</requiredProperty>
Make sure you have Apache commons dependency included.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
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"'
}
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
I want to write a Gradle function, which creates a pom.xml file with data (groupId, artifactId, version) passed as arguments.
I created following script:
apply plugin: 'maven'
apply plugin: 'java'
def createMainPom(mainDir, groupId, artifactId, version)
{
pom
{
project
{
groupId '$groupId' // Error
artifactId '$artifactId'
version '$version'
packaging 'pom'
name 'New app'
modules
{
module
{
name 'app'
}
module
{
name 'integration-tests'
}
}
}
}.writeTo('$mainDir/pom.xml');
}
[...]
When I run it, I get the error at the position marked with // Error above:
> No signature of method: java.lang.String.call() is applicable for argument typ
es: (java.lang.String) values: [a]
Possible solutions: wait(), any(), wait(long), split(java.lang.String), find(jav
a.lang.String), count(java.lang.String)
How can I fix this?
The plugin documentation states:
Note: groupId, artifactId, version, and packaging should always be set directly on the pom object.
Does moving those properties up a level, out of project, make a difference?
For strings variables resolution use double quotes:
groupId "$groupId"
artifactId "$artifactId"
version "$version"
...
}.writeTo("$mainDir/pom.xml");