How to add a Gradle task to an Android build - android

I would like to add a gradle task to my Android build under Android Studio. Various on-line documentation shows clearly how to create a task, but not where to place it in a build.gradle file and which one of them.
A simple, complete example would be perfect.
BTW, I'd like to add a pre-build task to prepare some data, and maybe a post-build task to do some verification/validation.

By trial-and-error and input from Martin Zeitler, I successfuly added pre- and post-build tasks to my build.gradle (app module) as shown below:
This works perfectly.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "MyPackage"
minSdkVersion 11
targetSdkVersion 26
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
// My libraries here
}
// MY PREBUILD TASK
task PreBuild {
println "MY PRE-BUILD TASK"
}
// MY POSTBUILD TASK
gradle.buildFinished {
println "MY POST_BUILD TASK"
}
}

Related

How do you export an Android Library as a jar?

The reason I need this is because I'm trying to work around what I'm told is a bug with Xamarin not always closing streams and thus not writing to some content providers. The technique is to create a jar and then run a script to C#ifiy it and export it to a dll. That's why I need a jar.
I initially tried this by creating a Java Library but that didn't work because I need access to various android packages (for example, android.Net). So while I did manage to create a jar for a Java Library following these instructions (Abhinav Tyagi's answer) I can't seem to get it to work for an Android Library. For an Android Library it says it ran the Create Jar script but I don't see it.
To be clear, this is what I would like to turn into a jar:
Export jar file using Android Studio
Check out the library source code from the repository.
Import checkout library in your android studio.
Modify your build.gradle
If your current module is a library project then you need to properly recognize ‘android-library’ plugin as mention below.
apply plugin: 'com.android.library'
android {
compileSdkVersion 'Google Inc.:Google APIs:16'
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 13
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:support-v13:23.0.0'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'myLib.jar')
into('release/') //you can change this directory where you want to copy your .jar
}
task clearJar(type: Delete) {
delete 'build/libs/myLib.jar'
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 'Google Inc.:Google APIs:16'
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 13
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:support-v13:23.0.0'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'myLib.jar')
into('release/') //you can change this directory where you want to copy your .jar
}
task clearJar(type: Delete) {
delete 'build/libs/myLib.jar'
}
Run gradlew to make jar.
For generating .jar, follow below steps,
Open the terminal.
Move to your project root directory.
Call makejar function which you have added in build.gradle
Example :-> D:\Users\MyWorkspace\project > gradlew makejar
After following all above steps you will find “myLib.jar” under release folder.

Error:(85, 0) Could not get unknown property 'compileJava' for project ':app' of type org.gradle.api.Project [duplicate]

In my android application I need to generate source code and use it in the app.
For that I created task genSources (using tutorials) for source generation. It works correctly if run it separately.
In my case I need to run source code generation automatically.
From tutorial I found out the following command:
compileJava.dependsOn(genSources)
but that is unknown command for the apply plugin: 'com.android.library'
gradle throws following exception:
Error:(35, 0) Could not find property 'compileJava' on project ':data'.
Looks like it can be used with apply plugin: 'Java'
but I cannot use these 2 plugins together
How can I solve this issue and generate needed source code before compilation?
build.gradle
apply plugin: 'com.android.library'
configurations {pmd}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
buildscript {
repositories {
maven {
url "http://repo1.maven.org/maven2/"
}
}
dependencies {
classpath group: 'net.sourceforge.fmpp', name: 'fmpp', version: '0.9.14'
}
ant.taskdef(name: 'fmpp', classname:'fmpp.tools.AntTask', classpath: buildscript.configurations.classpath.asPath)
}
task genSources << {
println "Generating sources...."
ant.fmpp configuration:"src/main/resources/codegen/config.fmpp",
sourceRoot:"src/main/resources/codegen/templates",
outputRoot:"target/generated-sources/main/java";
}
compileJava.dependsOn(genSources)
sourceSets {
main {
java {
srcDir 'target/generated-sources/main/java'
}
}
}
dependencies {
...
}
UPDATED
I was found some solution which at least not throw exception
gradle.projectsEvaluated {
compileJava.dependsOn(genSources)
}
Then I execute gradle build but nothing happens
With gradle 2.2+ this should work:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn genSources
}
If you also want it to happen when you evaluate (e.g. when syncing your project with gradle in android studio) you can do it like this:
gradle.projectsEvaluated {
preBuild.dependsOn genSources
}

Several tasks not accessable in build.gradle file in connection with Android Studio

i found a weird behaviour in Android Studio.
I want to access the dexDebug task in my module build.gradle file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.testprj.test"
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dexDebug.doFirst {
// do some stuff here
}
But i get the following error: Error:(rowNumber, 0) Could not find property 'dexDebug' on project ':app'.
That's weird because i can call this task over the Android Studio terminal via gradle dexDebug.
What is the reason for it?
Create your own task:
task myTask() {
}
Make your task "process stuff" before dexDebug:
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(dexDebug)) { // <-- check if your task is ready
dexDebug.dependsOn myTask // <-- make it depend on your task
}
}

Gradle. How to generate source code before compilation in android app

In my android application I need to generate source code and use it in the app.
For that I created task genSources (using tutorials) for source generation. It works correctly if run it separately.
In my case I need to run source code generation automatically.
From tutorial I found out the following command:
compileJava.dependsOn(genSources)
but that is unknown command for the apply plugin: 'com.android.library'
gradle throws following exception:
Error:(35, 0) Could not find property 'compileJava' on project ':data'.
Looks like it can be used with apply plugin: 'Java'
but I cannot use these 2 plugins together
How can I solve this issue and generate needed source code before compilation?
build.gradle
apply plugin: 'com.android.library'
configurations {pmd}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
buildscript {
repositories {
maven {
url "http://repo1.maven.org/maven2/"
}
}
dependencies {
classpath group: 'net.sourceforge.fmpp', name: 'fmpp', version: '0.9.14'
}
ant.taskdef(name: 'fmpp', classname:'fmpp.tools.AntTask', classpath: buildscript.configurations.classpath.asPath)
}
task genSources << {
println "Generating sources...."
ant.fmpp configuration:"src/main/resources/codegen/config.fmpp",
sourceRoot:"src/main/resources/codegen/templates",
outputRoot:"target/generated-sources/main/java";
}
compileJava.dependsOn(genSources)
sourceSets {
main {
java {
srcDir 'target/generated-sources/main/java'
}
}
}
dependencies {
...
}
UPDATED
I was found some solution which at least not throw exception
gradle.projectsEvaluated {
compileJava.dependsOn(genSources)
}
Then I execute gradle build but nothing happens
With gradle 2.2+ this should work:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn genSources
}
If you also want it to happen when you evaluate (e.g. when syncing your project with gradle in android studio) you can do it like this:
gradle.projectsEvaluated {
preBuild.dependsOn genSources
}

Android Studio Gradle Build Copy Task

I want to copy files before the build starts, but the gradle task dont start.
My section android in my build.gradle file:
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
}
//Copy app_values.xml
task copy_app_values (type: Copy)<<{
println 'Copy app_values'
copy_app_values.from pathtoValues+'app_values.xml'
copy_app_values.into projectDir+'/'+moduleName+'/src/main/res/values'
}
buildTypes {
release {
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
debuggable true
}
hockeyapp {
debuggable true
println 'Build HockeyApp'
tasks.add(copy_app_values)
}
}
}
Can anyone explain how this works. I'm newbie in gradle.
I am not sure by your question what exactly you are looking for. Either you just want to run this task once before gradle tries to create the hockeyapp build or just before the compile. Either way, try the following,
task copyDocs(type: Copy) {
from 'src/main/doc'
into 'build/target/doc'
}
tasks.withType(Compile) {
compileTask -> compileTask.dependsOn(copyDocs)
}
Place this piece of code out of the Android section and try running it. Hope it helps.

Categories

Resources