Android: Skip Gradle "testClasses" task for a dependency project - android

I have followed this guide to create a JUnit test file for my main Android module (let's call it "module-a"), in Android Studio v1.4.
My "module-a" has a dependency on an external library that is provided as a .aar file and for which I had to create a dedicated module.
This dependency causes an error:
When right clicking the test Java file and hitting "Run MyTestName" , it fails with this error
Error:Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Task 'testClasses' not found in project ':module-b'.
Removing the dependency on module-b solves the problem.
Excerpt of module-a build.gradle:
compile project(':module-b')
module-b build.gradle:
configurations.create("default")
artifacts.add("default", file('library-b.aar'))
How should I configure Gradle so that it does not try to run the testClasses task on "module-b" ? (this should solve my issue)

I did not find a way to skip the testClasses task for module-b: it seems that actions started from Android Studio (like running a JUnit test) run Gradle commands that cannot be modified. In my case:
Information:Gradle: Executing tasks:
[:module-a:prepareFree_flavorDebugUnitTestDependencies,
:module-a:generateFree_flavorDebugSources,
:module-a:mockableAndroidJar,
:module-a:assembleFree_flavorDebug,
:module-a:assembleFree_flavorDebugUnitTest,
:module-b:testClasses]
I found a workaround for my problem, though:
Add the following code to module-b build.gradle:
task testClasses {
doLast {
println 'This is a dummy testClasses task'
}
}

Related

Android Gradle Build: duplicate entry: META-INF/app_release.kotlin_module

I'm trying to build a release app via Android Studio > Generate Signed Bundle or APK > Android App Bundle > Release
However gradle fails with
: > Task :core:transformClassesWithMergeClassesForRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':core:transformClassesWithMergeClassesForRelease'.
> 1 exception was raised by workers:
java.util.zip.ZipException: duplicate entry: META-INF/app_release.kotlin_module
In my build.gradle I've tried adding:
packagingOptions {
exclude 'META-INF/app_release.kotlin_module'
}
But it isn't making any difference whatsoever.
How do I fix this?
For extra context, it's a multi module project.
I have a core module, and an installed module which is declared in the core build.gradle with dynamicFeatures = [":installed"]
Thanks
Please make sure all your dependencies are api or implementation,
I have the flowing dependency grap.
meemo_sdk:
api project(":gvoice")
app project:
implementation project(":gvoice")
implementation project("meemo_sdk")
It complains "META-INF/gvoice_debug.kotlin_module" collision.
After change api to implementation, it works!
So I figured it out.
I pressed shift twice in Android studio (to open up the search everywhere dialog) and searched for app_release.kotlin_module
I saw two files, that were under two of my dependencies (which funnily enough were libraries that I had created!)
I opened up these library projects, and in the build.gradle file I had to add:
ext {
PUBLISH_GROUP_ID = 'com.companyname'
PUBLISH_ARTIFACT_ID = 'packagename'
}
android {
...
compileOptions {
kotlinOptions.freeCompilerArgs += ['-module-name', "$PUBLISH_GROUP_ID.$PUBLISH_ARTIFACT_ID"]
}
}
Rebuilt the library projects with new versions, used these new versions in my other project, and it started compiling :)
Build -> Clean Project Worked for me
I found the same issue comes randomly by using Android Studio 4.2.1 and Java8, I sorted out successfully by deleting .gradle file in the project (not the main one) every time I get the error.
Java version: JDK8221
Kotlin: 1.5.0
AndroidStudio: 4.2.1
Not the best solution but is a good workaround for now.

Could not get unknown property 'assembleRelease' for project

After updating Android Studio to version 2.2 and the gradle plugin to 2.2.0, I get following error:
Error:(32, 1) A problem occurred evaluating project ':jobdispatcher'.
Could not get unknown property 'assembleRelease' for project ':jobdispatcher' of type org.gradle.api.Project.
The problem is in the build.gradle file of an imported jobdispatcher module:
task aar(dependsOn: assembleRelease)
What changes can I make to fix this?
Note, this issue is very similar to, but still a bit different to, that reported here.
Move your dependency dependsOn inside your gradle task like shown below:
task aar() << {
dependsOn 'assembleRelease'
}
Just add "" like this to fix your problem:
from:
task aar(dependsOn: assembleRelease)
to:
task aar(dependsOn: "assembleRelease")
I tried all the previous answers, all are not working. Here is the one working after gradle 2.2.
Starting from 2.2, those tasks also include "assembleDebug" and "assembleRelease". To access such tasks, the user will need to use an afterEvaluate closure:
afterEvaluate {
task aar(dependsOn: assembleRelease) {
//task
}
}
task aar {
....
}
aar.dependsOn('assembleRelease')
and task aar will run after task "assembleRelease" finished~
wish this will help you~ :-D
I had the same problem.
Disabling instant run under Android Studio/Preferences/Build, Execution, Deployment/Instant Run worked for me.

Gradle clean task fails for not resolving all dependencies

When I ran ./gradlew clean for my Android project, the task failed for not resolving all dependencies. The complaints came from the gradle task _debugCompile.
I can verify some of the local dependencies didn't exist which failed the dependencies resolving process, but I'm still curious about why the clean task would check for dependencies while not building the project.
Shouldn't it just delete some directories as mentioned in the following link?
https://docs.gradle.org/current/userguide/java_plugin.html#sec:clean
Is there any gradle built-in task that could just clean the build folder without checking dependency?
Thanks!
Gradle already resolves dependencies during its configuration phase which precedes the execution phase, see The Build Lifecycle (it downloads dependencies only once they are needed, though). However, dependencies being out of date does not make resolution fail. If resolution fails that dependency is likely not available online anymore. If you still have the dependency in your Gradle cache you might be able to work around this by specifying the --offline switch:
$ ./gradlew --offline clean

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.

Gradle: Only resolve dependencies for the desired variant

I have a test app with three flavors:
dev: Uses a local copy of the library during development
qa: Uses a snapshot during QA
rc: Uses a prerelease build for release candidate testing.
dependencies {
devCompile project(':library')
qaCompile 'com.example:library:1.0.0-SNAPSHOT#aar'
rcCompile 'com.example:library:1.0.0#aar'
}
I run Gradle, and expect it to do the minimum amount of work necessary to build just what I want:
./gradlew :test-app:connectedAndroidTestDevDebug
However, the build fails, because it is trying to resolve dependencies for all build flavors, not just the one I am building.
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':test-app'.
Could not resolve all dependencies for configuration ':test-app:_qaDebugCompile'.
Could not find com.example:library1.0.0-SNAPSHOT.
Searched in the following locations:
https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
Required by:
project-name:test-app:unspecified
The SNAPSHOT that the qa flavor is trying to resolve doesn't exist yet, and that should be fine, because I'm not trying to build the qa flavor. If that SNAPSHOT build is present, then everything works fine.
Questions:
Why are all build flavors having their dependencies resolved?
How can I accomplish building just one flavor without encountering this problem?
Is there some better way to do this that will be more "Gradley"?
I assume that the build works if you exclude the task manually?
gradle connectedAndroidTestDevDebug -x _qaDebugCompile
It looks like the task connectedAndroidTestDevDebug has a dependency on the task _qaDebugCompile, which causes your problem. I don't know how your tasks are defined, but you can study your dependencies using Gradle's built-in tasks gradle dependencies and gradle dependencyInsight. Maybe that will give you a hint in the right direction:
gradle dependencyInsight --dependency com.example:library:1.0.0-SNAPSHOT
You can read more about task dependencies in the gradle User's guide.
I finally solved this by checking the list of tasks and if there is a task for the desired build variant, adding the dependency.
dependencies {
gradle.startParameter.taskRequests.each { taskRequest ->
taskRequest.args.each { taskName ->
String flavorName = "qa";
if (taskName.toLowerCase().endsWith(flavorName+"debug") ||
taskName.toLowerCase().endsWith(flavorName+"release")) {
qaCompile 'com.example:my-library:1.0.0-SNAPSHOT#aar'
}
}
}
devCompile project(':localLibrary')
//qaCompile 'com.example:my-library:1.0.0-SNAPSHOT#aar' // What I used to do.
rcCompile 'com.example:my-library:1.0.0#aar'
}
Keep in mind that the task list will not contain dependent task names, so if you aren't invoking a task that contains your build flavor name, it won't work. This is just something I got working.

Categories

Resources