Run executables in gradlew - android

How can I run the mvn executable from the gradle wrapper?
task install(type: Exec, dependsOn: assemble) {
description = "Som description."
executable = 'mvn'
args = ["install:install-file", <more-args-here>]
}
I can access the command from the terminal normally. I also added MAVEN_HOME in the Path Variables but looks like gradlew can still not find the command.

Depends on your OS. If you are on Windows try to rewrite your task into this
task install(type: Exec, dependsOn: assemble) {
description = "Som description."
commandLine 'cmd', '/c', 'mvn', 'install:install-file', <more-args-here>
}
On linux go for
task install(type: Exec, dependsOn: assemble) {
description = "Som description."
commandLine './mvn', 'install:install-file', <more-args-here>
}

Related

cd command in gradle not working

The cd command in my Gradle task is not working. Using it to go to another folder.
task assembleTask(overwrite: true, type:Exec) {
commandLine "gradle", "assembleDev"
doLast {
commandLine "cd tests"
commandLine "ls"
}
}
The Exec task only runs once. You are setting the commandLine property 3 times.
once in the configuration phase, before the task runs
twice after the task has run (this will have no effect)
If you want one task to run another, you might do
task assembleTask {
dependsOn assembleDev
doLast {
file('tests').listFiles().each { File f ->
println f.name
}
}
}
Or perhaps you want a GradleBuild task, not sure
If you want to run multiple execs in a single task you might want to use project.exec() instead of Exec task. Eg:
task assembleTask {
doLast {
exec {
commandLine 'foo'
}
exec {
commandLine 'bar'
}
exec {
commandLine 'baz'
}
}
}

Gradle execute android tasks in specific order

I would like to merge Android gradle tasks into one and execute them in a specific order.
task.dependsOn did not work for me for a list of tasks, neither did the shouldRunAfter method.
task buildAll {
shouldRunAfter = ['clean', 'checkstyle', 'build']
}
What is the best way to run Android gradle tasks in specific order?
I ended up using doLast in connection with exec. In this example we define buildAll task which executes clean, checkstyle, build and lint in order - one after the other.
task buildAll {
doLast {
exec {
commandLine "${getRootDir()}/gradlew", "clean", "checkstyle", "build", "lint"
}
}
}
you can also use:
task buildAll {
doLast {
exec {
commandLine "${getRootDir()}/gradlew", "clean"
}
exec {
commandLine "${getRootDir()}/gradlew", "checkstyle"
}
exec {
commandLine "${getRootDir()}/gradlew", "build"
}
exec {
commandLine "${getRootDir()}/gradlew", "lint"
}
}
}

Why doesn't gradle tasks show group or description when using exec?

My Gradle task stopped showing the group and description in ./gradlew tasks, since I've added an exec {} in the root build.gradle.
What's going on, and how do I get it back?
task doSomething << {
group 'yourGroupName'
description 'Runs your bash script'
exec {
workingDir "$projectDir/../pathto/"
commandLine 'bash', '-c', './bashscript.sh'
}
}
Everything else works.
You cannot configure group and description in a doLast() closure
this code
task doSomething << {
exec {
workingDir "$projectDir/../pathto/"
commandLine 'bash', '-c', './bashscript.sh'
}
}
is the same than
task doSomething {
doLast {
exec {
workingDir "$projectDir/../pathto/"
commandLine 'bash', '-c', './bashscript.sh'
}
}
}
In the following group and description are not taking into account
task doSomething {
doLast {
group 'yourGroupName'
description 'Runs your bash script'
exec {
workingDir "$projectDir/../pathto/"
commandLine 'bash', '-c', './bashscript.sh'
}
}
}
But here it does :
task doSomething {
group 'yourGroupName'
description 'Runs your bash script'
doLast {
exec {
workingDir "$projectDir/../pathto/"
commandLine 'bash', '-c', './bashscript.sh'
}
}
}

Gradle - How to get values from AndroidManifest?

Inside build.gradle for Android project
task runAndroidApplication(type: Exec, dependsOn: ':installDebug') {
//TODO update Activity name below or find a way to get it from AndroidManifest.xml
if (System.properties['os.name'].toLowerCase().contains('windows')) {
// windows
commandLine 'cmd', '/c', 'adb', 'shell', 'am', 'start', '-n', "com.example.androidapptorun.MainActivity"
} else {
// linux
commandLine 'adb', 'shell', 'am', 'start', '-n', "com.example.androidapptorun.MainActivity"
}
}
How to get value for main Activity from AndroidManifest for the default Activity?
(Also if there are several activities, making logic to select one would make it long,
while processing is to be inside Android tools)
Is there better way from android plugin then parsing AndroidManifest.xml ?
UPDATE: https://github.com/novoda/gradle-android-command-plugin can possible suit some needs, but I needed no-argument version to make quick run in Eclipse via http://marketplace.eclipse.org/content/gradle
You can use XmlSlurper class.
example:
AndroidManifest.xml
<manifest package="com.example.your.app">
and retrieve it in gradle
def manifest = new XmlSlurper().parse(file("AndroidManifest.xml"))
// returns "com.exmaple.your.app"
manifest.#package.text()
I just wrote this for ADT 20 (L), Gradle 1.12 and com.android.tools.build:gradle:0.12.2.
It works with flavors, build types (don't forget myBuildType.initWith(existingBuildType)), and applicationIdSuffix.
Put the following at the end of android { ... }:
applicationVariants.all { variant ->
if (variant.install) {
tasks.create(name: "run${variant.name.capitalize()}", type: Exec, dependsOn: variant.install) {
description "Installs the APK for ${variant.description}, and then runs the main launcher activity."
def getMainActivity = { file ->
new XmlSlurper().parse(file).application.activity.find{ it.'intent-filter'.find{ filter ->
return filter.action .find{it.#name.text() == 'android.intent.action.MAIN'} \
&& filter.category.find{it.#name.text() == 'android.intent.category.LAUNCHER'}
}}.#name
}
doFirst {
def activityClass = getMainActivity(variant.processManifest.manifestOutputFile)
commandLine android.adbExe, 'shell', 'am', 'start', '-n', "${variant.packageName}/${activityClass}"
}
}
}
}
For libraries you need to change applicationVariants to libraryVariants: see manual.
Update: ADT 20, Gradle 2.1 and com.android.tools.build:gradle:0.13.1:
applicationVariants.all { variant ->
if (variant.install) {
tasks.create(name: "run${variant.name.capitalize()}", type: Exec, dependsOn: variant.install) {
description "Installs the APK for ${variant.description}, and then runs the main launcher activity."
def getMainActivity = { file ->
new XmlSlurper().parse(file).application.activity.find{ it.'intent-filter'.find{ filter ->
return filter.action .find{it.'#android:name'.text() == 'android.intent.action.MAIN' } \
&& filter.category.find{it.'#android:name'.text() == 'android.intent.category.LAUNCHER'}
}}.'#android:name'
}
doFirst {
def activityClass = getMainActivity(variant.outputs.processManifest.manifestOutputFile)
commandLine android.adbExe, 'shell', 'am', 'start', '-n', "${variant.applicationId}/${activityClass}"
// or without the XML hacking: commandLine android.adbExe, 'shell', 'monkey', '-p', variant.applicationId, '1'
}
}
}
}

How do I use this Gradle Script in Android Studio?

I found this gradle script built for android studio to allow the use of the NDK until the team can create an official (and more understandable) way of accomplishing this. I don't really understand how it works or how it ties in to a normal gradle script as I am still new to this IDE. Can anybody explain it to me?
The script is:
//////////////
// NDK Support
//////////////
// If using this, Android studio will fail run the following to set the environment
// variable for android studio:
// launchctl setenv ANDROID_NDK_HOME
// /Users/boos_patrick/Development/Android/android-ndk-r8e
// otherwise remove the dependsOn part and run ./gradlew buildNative from the
// command line
task copyNativeLibs(type: Copy, dependsOn: 'buildNative') {
dependsOn 'buildNative'
from(new File('libs')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File(buildDir, 'native-libs')
}
task buildNative(type: Exec) {
if (System.env.ANDROID_NDK_HOME != null) {
def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build')
commandLine ndkBuild
} else {
doLast {
println '##################'
println 'Skipping NDK build'
println 'Reason: ANDROID_NDK_HOME not set.'
println '##################'
}
}
}

Categories

Resources