Android Studio: exclude gradle task from build - android

I wrote a small task, which updates an Android library from the web. This should only be done on request. I know, that there is a '-x' option, but this seems to apply only on gradle itself. The task gets executed whenever I try to build my project with Android Studio. Is there a way to exclude specific task from being executed?
My gradle task look like:
task downloadSDK {
print 'Downloading SDK...'
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
ant.get(
src: 'http://example.com/SDK.zip',
username:properties.getProperty('USERNAME', null),
password:properties.getProperty('PASSWORD', null),
dest:"${buildDir}/sdk.zip",
verbose:true)
println 'done'
}
task updateSDK(type: Copy) {
print 'Copying SDK...'
delete "src/main/java/"
def zipFile = file("${buildDir}/sdk.zip")
def outputDir = file("src/main/java")
from zipTree(zipFile)
into outputDir
println 'done'
}
updateSDK.dependsOn downloadSDK
I thought that I just have to add << to my updateSDK, but it doesn't seem to work with the Copy task.

Reading the gradle spec more deeply, I found out, that Copy is actually not a task but rather a function. So, my gradle task has to look like:
task updateSDK << {
print 'Copying SDK...'
delete "src/main/java/"
def zipFile = file("${buildDir}/sdk.zip")
def outputDir = file("src/main/java")
copy {
from zipTree(zipFile)
into outputDir
}
println 'done'
}
The difference is that I call the copy function inside a task so that doLast will work correctly.

Related

Gradle copy task not copying the files

Can someone tell me why my copy task is not working, i have seen some similar questions here but none of them provided a soultion...
def outputJar = "${buildDir}/intermediates/jar"
// Define some tasks which are used in the build process
task copyCompiledClasses(type: Copy, dependsOn: 'assemble') {
// get directory for current namespace
println "Copy compiled classes..."
mkdir Paths.get(outputJar,'classes')
mkdir Paths.get(outputJar,'bin')
from fileTree(dir: 'build/intermediates/javac/debug/classes/', exclude : '**/BuildConfig.class')
into outputJar+'/classes'
}
there are classes in the source folders, and my target folders are being created but the actual copying is not taking place!!!! grrrrr!
Think that the source-spec was wrongful.
task copyCompiledClasses(type: Copy, dependsOn: assemble) {
def outputDir = "${buildDir}/intermediates/jar"
mkdir "${outputDir}/classes"
mkdir "${outputDir}/bin"
from fileTree("${buildDir}/intermediates/javac/debug/classes") {
include '**/*'
exclude '**/BuildConfig.class'
}
into "${outputDir}/classes"
}

How to run .cmd file as task in Gradle?

I have .cmd file that I want to include everytime build.gradle is run by Android Studio.
Let's say the filename is create-artifacts.cmd and so in my module's build.gradle.
android {
...
task createArtifacts(type:Exec) {
commandLine = ["create-artifacts.cmd"]
workingDir = file("$rootDir")
}
}
How may I be helped with this?
See the ExecTask documentation
task createArtifacts(type:Exec) {
commandLine 'cmd', '/c', 'create-artifacts.cmd'
workingDir rootDir
}

Gradle: How to execute task after create lib.aar file?

I have gradle task which unbacks lib-debug.aar
need execute this task after crate this lib-debug.aar file
task unzip<<{
copy {
def aarFile = file("${buildDir}/outputs/aar/lib-debug.aar")
def outputDir = file("${buildDir}/outputs/eclipse")
from zipTree(aarFile)
into outputDir
}
You can just do:
unzip.dependsOn assemble
That will make it so that whenever you run the unzip task, the assemble task has to have run before.

Gradle file copy not working in Android Studio

I have the following script in build.gradle of an app project. All the println lines work fine meaning the task is run. However no files are copied. I am new to Gradle. I am probably missing something very simple. Any tip will be greatly appreciated.
assembleRelease.doLast {
android.applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
def releaseBuildTask = tasks.create(name: 'copy', type: Copy) {
println("Step A")
from 'build/outputs/apk/'
println("Step B")
into 'build/outputs/debug/'
println("Finished")
}
releaseBuildTask.mustRunAfter variant.assemble
}
}
println "copying task finished"
}
Update (2015-01-02)
I have noticed that I can do other file tasks such as deleting, renaming without any problem, but not copying.

Access sdk.dir value in build.gradle after project evaluation

I have a custom task in my build.gradle file that does bytecode transformations on class files before getting dex'd that looks like this:
task droidcook(type: JavaExec) {
main 'org.tsg.android.asm.Main'
}
afterEvaluate { project ->
android.applicationVariants.each { variant ->
variant.javaCompile.doLast {
project.tasks.droidcook.configure {
classpath variant.javaCompile.classpath
classpath "build/classes/" + variant.dirName
classpath sdk.dir + "/platforms/android-19/android.jar"
classpath "compile-libs/droidcook.jar"
args "build/classes/" + variant.dirName
args "com.example"
// args "-debug"
// args "-asmifier"
}
project.tasks.droidcook.execute()
}
}
}
The issue with the above is the classpath sdk.dir + ... line where sdk.dir isn't evaluated appropriately. To get this working, I currently have to hard code the path to the android.jar.
Bonus points if you can answer with an appropriate value to replace android-19 with for accessing a platform specific android.jar based on project configuration. :D
I don't see a published API to expose sdk.dir to your build file, so I just cribbed the code from https://android.googlesource.com/platform/tools/build/+/d69964104aed4cfae5052028b5c5e57580441ae8/gradle/src/main/groovy/com/android/build/gradle/internal/Sdk.groovy to read it manually. As for replacing android-19, if I read android.compileSdkVersion I get that exact string. I'm not sure that this is a published API either, so it may be subject to change and breakage in future versions of the Android Gradle plugin.
With that said, this works for me:
afterEvaluate {
def rootDir = project.rootDir
def localProperties = new File(rootDir, "local.properties")
if (localProperties.exists()) {
Properties properties = new Properties()
localProperties.withInputStream { instr ->
properties.load(instr)
}
def sdkDir = properties.getProperty('sdk.dir')
def androidJarPath = sdkDir + "/platforms/" + android.compileSdkVersion + "/android.jar"
print androidJarPath + "\n"
}
}
If there's no local.properties, this code is going to fail. If you care about that case, you can copy more code from Sdk.groovy to mimic its behavior in trying to find a reasonable default.
There's a much simpler way, although it isn't documented, so it probably isn't officially supported.
println "${android.getSdkDirectory().getAbsolutePath()}"
Oddly this method only seems to be available for certain versions of the gradle plugin. It isn't available in the commit linked above by Scott. If the above code doesn't work, then try this:
println "${android.plugin.getSdkFolder().getAbsolutePath()}"
For completeness, here's how to get the ndk.dir variable, too:
println "${android.plugin.getNdkFolder().getAbsolutePath()}"
As of android gradle plugin v2.1.2, this is what I use in my gradle script:
android.ndkDirectory.path
android.sdkDirectory.path
For example to explicitly launch ndk-build:
task ndkBuild(type: Exec) {
commandLine android.ndkDirectory.path+'/ndk-build', '-C', file('src/main').absolutePath
}
Use:
String androidJar = android.getBootClasspath()[0]
Assuming you're using SDK 21, this would set '<path-to-SDK>/platforms/android-21/android.jar' into variable androidJar (the SDK version selection performed automatically).
The next step would obviously be replacing 'classpath sdk.dir ...' etc. with:
classpath androidJar

Categories

Resources