I have a project using dynamic feature module, and I want to run my unit test in feature module via gradle task (for my CI purpose):
./gradlew :feature_product:test
But it always gives me NoClassDefFoundError for tests that have dependencies on classes from the base module:
com.example.android.feature.product.ProductViewTest > on vote change to negative FAILED
java.lang.NoClassDefFoundError: app.BaseView
ProductView class from the feature module extends BaseView from the base module.
Oddly, it succeeds when run in Android Studio, it works fine.
Then I notice something different in the logs, when I run via command line and when I run Android Studio. The first line in the Android Studio is generateDebugSources, something which absent when I run ./gradlew test
Executing tasks: [:lib_ui:generateDebugSources, ...]
How do I fix this? Does Android Studio has different command with the provided command ./gradlew test when I press Ctrl+Shift+R ?
After searching further about this issue, I found it also being reported in the android-test and app-bundle-samples projects and there is also an issue in the issue tracker.
It turns out this issue fixed in the Android Gradle Plugin 4.1.0 as per comment in the issue tracker.
If you don't want to update AGP to 4.1.0 which is still in alpha, adding this to the feature module's build.gradle fixed the issue for me, as per this comment:
testRuntimeOnly(files("$projectDir/../b_app/build/intermediates/app_classes/debug/classes.jar"))
If it is a missing task that you believe is necessary then calling it first like below should do the trick:
./gradlew :lib_ui:generateDebugSources :feature_product:test
I would even go full on and assemble the dependencies if necessary though that might take more time:
./gradlew :lib_ui:assemble :feature_product:assemble :feature_product:test
Related
I created a custom lint rule as a module for my project, the Detector is working correctly and has tests that validate that is working, I then added this lint module as a dependency of a common module that all other modules depend lintPublish project(':lint')
Here is my custom rule but it only shows after runing the ./gradlew lint command and onlt if the lint detects that a rule is not respected, because if every thing is ok the rule does not show here, so when we change the code and an error was supposed to happen it does not automatically detects it
Closing Android studio losses this configurations and I'm required to run the lint task again, any ideas of what I'm doing wrong? Shouldn't this be automatically added to the lint rules of the project?
Android Studio 4.0-beta02
Gradlew 4.0.0-beta02
Gradle 6.1.1
lint 27.1.0-alpha02
When I am trying to run Android tests by executing:
./gradlew connectedDebugAndroidTest
The following error occurs:
com.android.builder.testing.ConnectedDevice > No tests found.[devicename] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack #Test annotations).
:connectedDebugAndroidTest FAILED
FAILURE: Build failed with an exception.
I have not made any changes to build.gradle or gradle-wrapper.properties files.
The issue can't be solved by updating everything to the latest version (gradle, android plugin, build tools, etc.)
All tests were previously successful. What could cause this mystic regression? Thanks.
One possible reason for this error message is that you get a crash in the app early in the test Runner. In such cases the exception stack will be in logcat but not in the gradle output.
I had the issue, try upgrading the test runner. Upgrading it to 0.5 from 0.4.x solved it. Ensure you have these lines in your build.gradle:
androidTestCompile "junit:junit:4.12"
androidTestCompile "com.android.support.test:runner:0.5"
I faced this exact issue today. Please do feel free to use all the other worthy solutions as mentioned above but what specifically worked for me was to delete the ".gradle" folder (found under the project panel on the left in Android Studio) entirely, followed by a clean build. A simple clean build didn't work for me.
This can happen if the tests are placed in the test or androidTest directory without a package. The path should be something like /src/androidTest/kotlin/yourPackageName/yourTestClass.
You can check if individually, all tests pass. Could be some test(s) is(are) failing. Had a similar issue and that was my case
I solved this by deleting androidTests folders from unneeded modules - /common and /data in my case.
I have a test suite for my Android app, and all unit tests run fine. However, whenever I make a single change in one of my unit test classes (for example, ModelUnitTests), when trying to run that class again, I get this message
Process finished with exit code 1
Class not found: "xxx.xxxxxx.xxx.ModelUnitTests"Empty test suite.
If I do a gradle clean and then run the class tests again, it runs fine (but it takes 4 minutes to do...), but then a new change will break it again.
Any advice on how to fix this? I'm not exactly sure which test configuration should I post. I'm using the Unit Tests artifact and my tests are located on the
module/src/test/package folder
I had a similar problem and it was because I first created an Unit Test with the same class name. When I created the Instrumented Unit Test I got the error.
To solve it, I went to Edit Configurations, on the left of the run icon. Then below Unit Test, it was the 'conflicting' class, which I deleted. Click on Apply/Ok. Then I right click on the class name, click on run and voilĂ , it works.
The fix on Android Studio is:
step 1.- Go to Run/Debug configuration
step 2.- Go to Android Tests section
step 3.- Remove the test configuration file with (-)
step 4.- Press Apply and OK
step 5.- Run the test again
Just ran into this - writing my unit tests in Kotlin. In my case it turned out I forgot to add kotlin plugin in given modules build.gradle file:
apply plugin: 'kotlin-android'
I had the same problem. I noticed that the method under test was being displayed in the Run/Debug configuration drop-down as:
TestClassName.testMethod()
rather than the correct:
testMethod()
I fixed it by deleting the TestClass.testMethod() Run/Debug configuration for the test method which was giving this error, then re-running the test.
If that recreates the same problem, delete the incorrect Run/Debug configuration, then right-click on the test method and select:
Create 'testMethod()'...
(rather than Run or Debug) to create a working configuration.
I had this problem, and none of the answers on this post (or the other highly-visible Stack Overflow posts) resolved it for me.
However, manually running the gradle task compileTestKotlin appears to have resolved the issue for me.
This was for Kotlin tests, Android Studio 3.1.2
If you are working on a team, check all your build.gradle files to make sure nobody is disabling the test tasks. I had the 'empty test suite' error and eventually found it was caused by the following in build.gradle at the project root:
gradle.taskGraph.whenReady {
tasks.each { task ->
if (task.name.contains("Test"))
{
task.enabled = false
}
}
}
If you use Robolectric you may need to set Working directory in run configuration as $MODULE_DIR$
Also set VM Options
: -ea
or: -noverify
http://robolectric.org/getting-started/
In my case this was cause by an exception being thrown in the #BeforeClass method.
Happened to me in AS 3.3.
I'm using flavors and this happened in a module which only has src/main and src/test. The app module has src/main src/common and src/flavor. The build type selected in AS was flavorDebug.
To fix it I went to "Run Configurations" and in the "Use classpath of module" dropdown the app module was selected. Select the module that you would like to test and voila!
Work for me:
Build > Clean Project
Run test
For me finally worked:
./gradlew check - checked all the errors and tried to fix as many of them.
Restarted Android Studio
Created a new configuration for Android JUnit, test kind: all in the directory, selected test directory (app/src/test) and app module.
Run tests
I had the same issue. I created Suite Class and it resolved the issue
Solved it using a lower gradle version
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
That will have to do for now
I'm working on an Android project with two projects inside it. When trying to run a build with gradlew (./gradlew build), I see the following error:
Execution failed for task ':example:lint'.
Could not initialize class
com.android.build.gradle.tasks.Lint$LintGradleIssueRegistry
With stacktrace enabled, this is listed as a java.lang.NoClassDefFoundError.
Oddly, the first time I ran this (which downloaded dependencies), the build failed with a different error:
Execution failed for task ':example:lint'.
lombok/ast/Node
The gradle wrapper that Android SDK created for the project is using Gradle 2.8.
Could this be a configuration issue with the project or my dev machine? I'm trying to avoid using the Android SDK lint tool as this complains about the projects using Gradle (and I hear it may miss some parts of these projects).
Turned out this was a configuration issue in our build.gradle. The following line had been added for testing and never removed:
configurations.classpath.exclude group: 'com.android.tools.external.lombok'
As such, the classes needed for linting were missing.
The following discussion pointed us in the right direction, in case it's useful to anyone else:
https://github.com/evant/gradle-retrolambda/issues/96
I have a Gradle file that runs the tests. It does not build anything. All it has is a unzip task that extracts all jars in a Gradle configuration into a particular directory and then runs the task of type Test. This test task points the testClasses directory to the location where the previous copy task has extracted the configuration into.
My build fails with the error message:
Problems reading data from Binary store
When I run 'gradle dependencies' it does not show any error/warning
When I run the copy task individually, it runs very well fine.
Only when I run everything it fails with error :
> Could not resolve all dependencies for configuration ':testConfig'.$
> Problems reading data from Binary store in /tmp/gradle8793563212642185736.bin (exist: false)$
I see the file /tmp/gradle8793563212642185736.bin does not exist. What could be the reason for this?
I solved upgrading following packages to last version (06/02/2021), so:
project app-level build.gradle file:
com.google.firebase:firebase-analytics:17.2.2
to
com.google.firebase:firebase-analytics:18.0.2
Project-level build.gradle file:
com.google.gms:google-services:4.3.3
to
com.google.gms:google-services:4.3.5
In my case setting org.gradle.parallel=false flag fixes the issue, but it's not a solution for my project, flag increase CI build time from ~15min to ~45min.
Turns out it was just a network problem for me. Gradle just needed to connect to the internet.
It happened with Flutter development in my case. Apparently, it is not an actual issue and there is no magic fix. Cleaning build file with "Flutter clean" and restarting the command line executer window solved the issue.
I was working with version 5.x earlier and then I upgraded to 6.0.1 and it resolved the issue.
this is how you can upgrade
cd android
./gradlew clean
./gradlew wrapper --gradle-version 6.0.1
./gradlew -v # to download the new version
cd ..
flutter clean
flutter run # might take 15 - 20mins for the first time
There are mainly a few steps to solve this problem.
Go to gradle.properties and set org.gradle.parallel=false and sync. Make sure your sync gradle offline option is off
Start the build with this option. If it's a big project setting org.gradle.parallel=false will take a long time.
Cancel the build repeat the steps setting org.gradle.parallel=true.
Also do not turn on offline build
This solution worked in my flutter project. I solved it by making these changes:
In android level build.gradle file, replace classpath 'com.android.tools.build:gradle:3.5.4' with classpath 'com.android.tools.build:gradle:4.0.1' (basically upgrade the version from 3.5.4 to 4.0.1)
In gradle-wrapper.properties file, replace distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip with distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip (upgrade from 5.6.2 to 6.1.1)
and if you have added classpath 'com.google.gms:google-services:4.3.3' to android level build.gradle file, replace that too with classpath 'com.google.gms:google-services:4.3.5' (upgrade from 4.3.3 to 4.3.5)
In my case the writing problem was there was no enough storage to build the app so I needed to restart my Mac and it finished building successfully.
I know am too late but it might help someone in the future so
Start by upgrading Gradle to the latest version.
Binary store issues reported to GitHub are often fixed right away, especially if they include a Minimal, Reproducible Example.
If upgrading didn't work, it can be useful to run the task again with Performance options disabled:
--no-parallel
--no-configure-on-demand
Next please find an existing issue or open a new one so it can be fixed in future versions. These things are all helpful:
Minimal, Reproducible Example
Build Scan
Smallest change that introduced the problem (e.g. worked on 5.5, but not 5.6)
I ended up here after publishing an artifact to mavenLocal().
I tried a lot but in the end what helped me was:
Upgrading gradle from 6.3 to 6.9
./gradlew wrapper --gradle-version 6.9
Hope this saves me half an hour next time this occurs.
Thanks to Cedric's hint, stopping gradle offline mode and make it online, fixed the issue for me