Android custom lint does not work automaticly - android

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

Related

NoClassDefFoundError when running unit test with Gradle task

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

What errors get detected in "build" in Android Studio - role of Gradle

I'm transitioning over from Eclipse to Android Studio and I find that I'm still confused about the different components of Android Studio regarding detecting errors. I have a project with about 20 Java files and 30 XML files. I recently ran a clean and build and got
Gradle build finished with 5 error(s) in 13s 397 ms
Two XML files were indicated and in both of them there was whitespace in front of the first element with an error saying ...
Xml declaration should precede all document content
So I fixed those and ran the build again with no errors.
I then ran Lint and found 8 more XML files with errors (NB, errors, not warning) saying "Element selector doesn't have required attribute:layout_height ". This error was apparently due to these files being in a layout folder instead of a drawable folder, although why that didn't cause problems in Eclipse is unclear.
My Questions:
Why does Gradle Build only detect some errors, but other have
to be found via lint?
What categories of errors will be found via Gradle Build?
How hard is it to add something to the Gradle Build script to find
all errors?
Edit: actually this is also true for regular Java files - I'll get "0 errors" in the Gradle build and then I'll step into a source file in the debugger and see 4 errors from Lint.
Why does Gradle Build only detect some errors, but other have to be
found via lint?
Lint errors are not compile errors, but code issues, and by default AndroidStudio will not check those. (Same goes for standard javac).
What categories of errors will be found via Gradle Build?
Gradle will detect all compile time errors, annotation processing, packaging errors, dex, signing, incorectly defined xml, incorrect file naming, etc.
How hard is it to add something to the Gradle Build script to find all errors?
Surprisingly easy in fact, source
To enable lint checks when compiling add in your application level build.gradle
android {
lintOptions {
// set to true to turn off analysis progress reporting by lint
quiet false
// if true, stop the gradle build if errors are found
abortOnError true
// if true, only report errors
ignoreWarnings false
}
...
}
If this will not work for U, add lint after each make, to do this you can follow this answer
I will add that this config will detect all enabled inspections with severity warning and error under:
File > Other Settings > Default Settings > Editor > Inspections
Android Studio will run code inspections checks live so all lint warnigns / errors are displayed in code while editing, lint errors are marked with red underscore, and warnings by marking code fragment with yellow background. Even if lint errors are marked the same as compile time errors they will not stop build by default.

Class not found: Empty test suite when running unit tests in Android Studio

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

"Could not initialize class" error running lint target from gradle

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

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