Not Running Groovy task on building android application - java

I got a code from internet to copy and paste module jar to another location and renaming it.
task copyCloudSdkJar(type: Copy) {
from('WiSe-Cloud-SDK/build/intermediates/bundles/default/')
into('WiSe-Cloud-SDK/release/')
include('classes.jar')
rename('classes.jar', 'WiSe-Cloud-SDK.jar')
}
The above given is the task I was written. And it is working when we manually executing task.
Problem
But When I rebuilding/building/cleaning my application the task is not running automatically
Thanks in advance.

You should add this task in dependencies for build task.
For example:
task copyCloudSdkJar(type: Copy) {
...
}
// if you want to run before build
build.dependsOn copyCloudSdkJar
// if you want to run after build
build.finalizedBy copyCloudSdkJar

Related

Android - Run a Java method in Gradle after an apk build

I'm currently building a Android application, and I would like to do some processing after the apk is built.
I've already succeeded to launch a gradle task after the build:
tasks.whenTaskAdded {
task ->
if(task.name == 'assembleRelease'){
task.finalizedBy postApkProcess
}
}
task postApkProcess{
doLast {
println 'OK'
}
}
But I struggle to launch a method inside that task. What I would like to do is to call something like new MyClass().postBuild() inside the class (or if it is not possible, run the main method of a Java class), but I don't find a way to do it.
I've tried to build a task task postProcess(type: JavaExec), but the line apply plugin: 'java'conflicts with the Android plugin.
Is there a way to do it ?
You could use commandLine to run the main method in MyClass which could call postBuild:
commandLine 'java' '<pathToSrc>/<mypackage>.MyClass'
Be careful to set the package name and source location properly. The path should point to the location (minus package directory tree) of the MyClass.class file, not MyClass.java. If your class is not compiled or it changes often, you could call javac to compile it each time before you run it (optionally using a shell script / batch file) to bundle both commands and simplify the gradle call and help you test the setup directly.
If your class uses code from libraries ensure you add them to the classpath:
'-classpath' 'mylib1.jar:mylib2.jar'
If you use java in the build process you might also have a look at scar

Gradle task in configuratoin phase has dependency on a task in execution phase

i have 3 different build files,
prodbuild.gradle, build.gradle and build.xml
and the hierarchy is, i will first call prodbuild.gradle
// Script used by Prod builds,
// Import development gradle file for basic build
apply from: 'build.gradle'
task prodX {
...
}
task prodY {
...
}
task prodZ {
...
}
as per my understanding this is calling build.gradle
ant.importBuild('build.xml')
task A {
...
}
task B {
...
}
and this is calling build.xml
<target name="antX" >
...
...
</target>
<target name="antY" dependson "antX">
...
...
</target>
Problem statement
target antX uses the directory created by task B in build.gradle, but the line
ant.importBuild('build.xml')
is executed in configuration phase, and at that time task A and task B are not executed, this is causing the issue,
i tried to avoid this by changing the build.gradle as
task importAntBuild (){
doLast {
ant.importBuild('build.xml')
}
}
but task prodX is dependent on target antY, since i made importAntBuild to run at last this will again cause the problem.
this is the first time i am working on a gradle project, correct me if my understanding is wrong, any help is appreciated.
PS : all of this cycle was working fine with gradle 2.3, this issue is observed after i upgraded it to gradle 7.4

How to include a custom .jar file in a java gradle build?

I have a gradle project to build a .jar file from Java sources. Some of the classes it needs are in a separate .jar file (let's called it helper.jar). To build helper.jar there is an external build system that I can invoke via a shell script.
If I manually run the shell script first to create helper.jar, and then add the file as a top-level dependency:
dependencies {
....
implementation files('/path/to/helper.jar')
Then everything compiles ok.
I am trying to convert this to automatically run the shell script to build helper.jar if necessary. I removed the file from the dependencies above and tried adding this task:
tasks.register('buildHelperJar', Exec) {
workingDir '/path/to/helper/root'
commandLine 'build_helper.sh'
outputs.file file('/path/to/helper.jar')
}
And then add it as a dependency:
project.afterEvaluate { proj ->
tasks.findByName('compileReleaseJavaWithJavac').dependsOn('buildHelperJar')
}
This seems to invoke the buildHelperJar task ok (and helper.jar gets built ok) but the subsequent compileReleaseJavaWithJavac task fails with missing symbol errors for everything that should be in helper.jar. So it's like I'm not actually adding the jar file itself into the compile step. What should I be doing to achieve this? Thanks a lot

How to call a Gradle task in all subprojects?

Say, I have a hierarchy of Gradle projects and some of them have java plugin applied:
root
projA
projA1
projA2 (java)
projB
projB1 (java)
projB2
projB21 (java)
projB22 (java)
projC (java)
I want to execute the test task in all subprojects where this task exists: :projA:projA2:test, :projB:projB1:test and :projC:test. Probably I will add more projects in future and I don't want to manually support a list of all test tasks in all subprojects. How can I achieve it?
One thing that came to my mind is something like the following:
// In root I iterate over all subprojects and find the task by name causing
// its creation and configuration
tasks.register("testAll") {
dependsOn subprojects.findResults { it.tasks.findByName("test") }
}
I don't like this approach as it goes against task configuration avoidance style.
Another option is to iterate over subprojects and check if the java plugin is applied there:
// In root
tasks.register("testAll") {
dependsOn subprojects.findAll { it.plugins.hasPlugin("java") }.collect { it.tasks.named("test") }
}
It works but I have a filling that I miss something simpler...
EDIT 1: Sorry for that but I forgot one important detail - I need to run tests in a subtree of projects. Say, everything down the path :projB.
Unless I'm missing something, you want to run tests for all of your submodules.
You can just...do that.
./gradlew clean test
This will run the test task in all of the subprojects that have it sufficiently configured.
If you need to run the tasks in a specific subproject, from the root project you can specify the subproject you want to run the task.
./gradlew clean :projB:test
If your subprojects have a task that needs to run after test, then you can do this in your subprojects block.
subprojects {
myTask.dependsOn("test")
}

Execute gradle task when its dependency is still running

I have to run a jar file and exec npm tests on it, but using gradle tasks. I'm using dependsOn to run the npm test after the jar is running.
These are my gradle tasks:
task runServer1 (type: Exec) {
// Run the jar file
}
task runNpmTest (type: Exec, dependsOn: ':runServer1') {
// Run npm tests
}
The problem is that when I execute gradle runNpmTest gradle stops at runServer1, which makes sense, because the server is still running. But my NPM tests will never run.
Any ideas?
It will not work this way, since runServer1 task is still running - it's a process. What you need is to run a server in background - so it won't block the main thread - and then run the tests. This probably should be done in a single task and configured via actions. Please have a look here and here to catch some useful knowledge.

Categories

Resources