Gradle dependency tree for "test" task different in different projects - android

I am running "gradlew test" task in my 2 android projects- in one it has a dependecy on mergeDebugAssets task while in other it doesn't have.
Though I want my "gradlew test" task to have dependency on mergeDebugAssets because otherwise I am not able to access assets from folder- build/intermediates/bundles/debug in my test.
I don't think it is appropriate to explicitly modify test task for this.
Also, should it depend on android sdk or buildtools version or gradle version? Though I kept the gradle version of 2 projects same but same is happening. Where am I missing?

Related

How can my Gradle plugin define a task depending on a task of AGP 7.0.0?

I have written a Gradle plugin based on Android Gradle Plugin: for each variant, the plugin defines a task generatevariantStuff that generates an asset and I need that the mergevariantAssets (defined by AGP) takes my asset as an input, so the mergevariantAssets task must depend on my generatevariantStuff task.
My plugin works properly with AGP 4.2.2.
AGP 7.0.0 deprecates com.android.build.gradle.api.ApplicationVariant and replaces it by com.android.build.api.variant.ApplicationVariant, so that I have to replace also com.android.build.gradle.AppExtension by com.android.build.api.variant.ApplicationAndroidComponentsExtension and the loop on the variants.
Now, when Android Studio builds the model, it fails with the following error:
A problem occurred configuring project ':app'.
> Task with name 'mergeDebugAssets' not found in project ':app'.
But in Gradle view of Android Studio a can see the mergeDebugAssets task.
It seems that during the method AppExtension.getApplicationVariants().all() (AGP 4.2.2), AGP has already defined its tasks, while, during the method ApplicationAndroidComponentsExtension.onVariants() (AGP 7.0.0), AGP has not yet defined its tasks.
You can see sample code at https://gist.github.com/dscoppelletti/d4ca0e1a19ed4ba4aaea99cf48ae930a
Any help is appreciated.
I have similar issues with it. I have an aggregated task that collects artifacts from each variant and copies them to a given path, but assemble tasks for variants are not available during ApplicationAndroidComponentsExtension.onVariants()

Run a gradle task before resolving dependencies

I want to run a gradle task that fetches additional sources and sets them up before gradle tries to resolve dependencies.
In build.gradle there is a task that fetches a sub project's source code. The task needs to be run before Gradle tries to resolve dependencies, because the sub project is part of the dependencies. The task involves fetching sources from a remote repository and replacing a few build.gradle files to make the build possible.
What happens now is that:
I run the task.
Gradle tries to resolve dependencies before actually running the task.
It fails because one of the dependencies requires the sub project (the sources that my task is supposed to fetch).
Of course, resolving dependencies is part of the "Configuration" build phase, so it's pretty clear why the task is run after. The question is how to make it run before.
Of course I can make it work if I replace my gradle task with a separate bash script and run it manually before gradle does anything. However, that would mean that I duplicate some variables in the gradle and the bash scripts (like version names and git tag names). Those variables are used for other purposes in gradle, and having them in two places is bad. There are other reasons I want to avoid that, one of them being - using a bash script would mean that gradle fails at doing our build from start to finish...
Firstly you are incorrect that resolving dependencies is part of the "Configuration" phase. If you make use of the lazy evaluation of FileCollection then it will actually be resolved in the execution phase. A configuration will be resolved the first time that resolve() is invoked. Please see the javadoc for the methods which cause a Configuration to be resolved. AFAIK the core gradle code won't resolve a configuration in the "Configuration" phase but your custom code may cause this (I suggest you refactor if this is the case)
You can do something like this:
dependencies {
// this is lazy evaluated
compile fileTree(dir: "$buildDir/dynamicJars", include: "*.jar")
}
task getDynamicJars(type: Copy) {
from zipTree('path/to/somefile.zip')
into "$buildDir/dynamicJars"
}
compileJava.dependsOn getDynamicJars

What task to run to re-create Dagger 2 injection files

When I make changes to Dagger 2 components, I'd like it to rebuild the injection classes. But I don't want to run a whole project Rebuild because that takes 5 minutes, even with the gradle daemon.
In some cases it automatically rebuilds, but in other cases it doesn't.
What gradle task can I run to just re-create the Dagger 2 files?
You need to generate the source files by running the apt plugin. This happens while compiling your sources. Use gradlew compileDebugSources to trigger the compilation or use Make Project in your IDE.
If you want to check the sources, you can see that it attaches itself to the javaCompile tasks.
Also see How to trigger the minimal task on Gradle to run apt plugin.

Gradle - Which task creates the 'build' directory?

I'm using Android Studio 1.3.2 on Mac.
Gradle version is listed as 2.2.1, Android Plugin version 1.3.1.
I have applied the FindBugs Gradle plugin, and I've created a task that successfully runs the analysis on the directory 'build/intermediates/classes'.
To trigger this task on Gradle Sync, I've added it as a dependency to the preBuild task, like this:
preBuild.dependsOn findBugs
The problem with this dependency is that, at time of preBuild, the generated class files are either non-existent (first sync) or stale (remaining from previous sync). Basically, I want my task to run immediately after the 'build/intermediates/classes' directory is created, or when the files there are refreshed as part of the 'Sync' operation.
Looking at the tasks available, I can see the 'clean' task has the following description:
clean - Deletes the build directory.
However, none of the other tasks I see describe the directory's creation. My first thought was "Well, it's got to be the build task, right?". Unfortunately, as usual, it's not that simple (pressing gradle 'sync' button, does not trigger my task when I've added it as a dependency to the 'build' task). Is no such task available? If so, which task would best suit what I'm trying to achieve?
You can see all tasks using ./gradlew tasks
The build task is the main one. It builds everything, generates APKs and runs all checks.
I recommend you adding the following to the build.gradle file:
check.dependsOn 'findbugs'
So when you run the check task, it will execute findbugs as well.
Then also set a dependency on findBugs for the compilation task:
task findbugs(type: FindBugs, dependsOn: 'compileDebugSources') {...}
This will compile the source in case you run findbugs without previously compiling it.

What commands does Android Studio's `gradle-aware make` perform

Im playing with Android Studio & Gradle and am interesting in what gradle-aware make actually does. The reason for my interest is I was originally under the impression that the default run config for a new AS projects default gradle-aware make runs the gradle assembledebug command (looking at the status at the bottom of AS during build shows the app:assembleDebug task running) and then some install and run commands.
However in testing on a machine that has 1.9 as the installed system gradle version and a wrapper on the project set to 1.10 I get the following
gradle assembleDebug -> Fail : Could not create plugin of type 'AppPlugin'.
./gradlew assembleDebug -> Success
AS Run -> Success
In my mind the above AS Run should fail if gradle-aware make was using gradle assembledebug
Looking at the src I can see the MakeBeforeRunTaskProvider.java class and the relevant commits but I cant see the relevant info
(have answered my own question as in writing it I sort of found the answer - but I assume if this confused me it will someone else so am posting the simple answer anyhow)
Turns out I shouldve looked in settings as you can set the gradle version that should be used - and it defaults to the "default wrapper".
Now when you create a new project in AS you have a default wrapper set up. You can if you want add a custom wrapper section to your root build.gradle of the form
task wrapper(type: Wrapper) {
gradleVersion = '1.10'
}
and then run the task with gradle wrapper to update the projects gradle wrapper (in ./gradle/). This allows the wrapper version to easily be updated.
Im assuming the "Use cusomtizable gradle wrapper" option just runs this wrapper task before any other gradle tasks (which could have a custom url for gradle zip download), whereas "use default..." will just used the last generated wrapper. This will be grayed out if the project has no generated wrapper. Please correct me if you think this is wrong.
This is using AS 0.4.6. Annoyingly there is a bug where syncing gradle files will change the project settings here - seems like to what the previous setting was as Im seeing on one project if going to "default..." and the other to "local". Time to upgrade AS!
Also AS's gradle console window shows the exact commands and output

Categories

Resources