36

My team and I develop Android apps and have decided on coding guidelines that all should follow. I therefore started implementing custom lint rules as per the following links:

The problem that I'm having is actually implementing these lint rules on a project basis. When I run ./gradlew clean build test install , as specified the rules apply and all is well. However when I build the aar library with ./gradlew aarWrapper:assemble and add it to my libs folder the linting does not work.

I added the following to my build.gradle file to add the library

repositories {
    flatDir {
            dirs 'libs'
        }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name: 'aarWrapper-debug', ext: 'aar')
}

I'm not sure what I'm missing but when I run ./gradlew lint it runs the linter but not with my custom rules... Any help, tips or advice is greatly appreciated.

EDIT 1

Here is the terminal output when running gradle.

:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareAarWrapperDebugLibrary UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2421Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72421Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2421Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUi2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUtils2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportFragment2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportMediaCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2421Library UP-TO-DATE
:app:prepareComAndroidVolleyVolley100Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidAnswers138Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidBeta121Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlytics261Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlyticsCore2310Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase961Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement961Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesLocation961Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMaps961Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesTasks961Library UP-TO-DATE
:app:prepareIoFabricSdkAndroidFabric1312Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:transformClassesWithDexForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:validateSigningDebug
:app:packageDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 7.881 secs

EDIT 2

Forked project: https://github.com/apertomove/linette

build.gradle: https://github.com/apertomove/linette/blob/apertomove-linette/build.gradle

EDIT 3 In addition to the links above I found this post written by Jason Atwood. We too have a CI server running jenkins in which we can run our checks and inform developers of errors based off of our lint rules. This works great, however, it is one step to far. It would be much more valuable and time save to run lint checks from the library when running our projects out of Android Studio, instead of committing our code only to find out that our project breaks rules.

hopeman
  • 2,778
  • 3
  • 18
  • 33
  • Does running `gradle dependencies` show the aarWrapper as a compile time dependency? – RaGe Sep 28 '16 at 21:36
  • @RaGe Yes I believe so, above I added the terminal out put after running `./gradlew assembleDebug` on my project: line 5 `:app:prepareAarWrapperDebugLibrary UP-TO-DATE` – hopeman Sep 29 '16 at 09:13
  • can you post the original gradle file to the custom lint project..you might have done something like forget to include an android-lint prebuilt jar for example – Fred Grott Sep 30 '16 at 12:03
  • @FredGrott hey here is a link to the github project and gradle file https://github.com/hopeman15/linette/blob/dev/build.gradle – hopeman Sep 30 '16 at 12:06
  • Hey @hopeman did you manage to solve this issue? I'm facing the same problem here :/ – Daniel Nazareth Dec 09 '21 at 18:56
  • @hopeman Did you found any solution? – sak May 30 '22 at 15:13
  • @sak I'm no longer working for my former employer. Currently my team is using static tools such as [detekt](https://detekt.dev/) and [kotlinter](https://github.com/jeremymailen/kotlinter-gradle). These are much better solutions and with [detekt](https://detekt.dev/) you can more easily create your own checks. Hope this helps. – hopeman May 31 '22 at 20:33
  • @hopeman Thanks but i found the solution. – sak Jun 04 '22 at 05:19

2 Answers2

1

I have written a post on how to add and integrate custom lint rules to your android app, you can have a look. It also has links to github repos showing how its done in an android project. Link to post

The easiest way for me was to set ANDROID_LINT_JARS path in the gradlew file and point it to the custom lint jar which is checked in to VCS system, so that you can run it locally as well before pushing the code. Hope it helps.

Infant Dev
  • 1,659
  • 8
  • 24
  • 48
  • Thanks for your reply, I added ANDROID_LINT_JARS... to my gradlew file as described above, but was still unable to achieve the desired result. I tried this with both a .jar and .aar library... – hopeman Nov 11 '16 at 08:53
  • Unfortunately is the project a company project and therefore I cannot share the code. I did however setup a public repository with the exact same "gradle setup" as our company project. You can find it [here](https://github.com/hopeman15/StackOverFlow) One rule for example is that textview ids should be prefixed with "text_" this however isn't picked up by the library – hopeman Nov 11 '16 at 10:19
  • Hey, you have entered the path of your lint.jar incorrect. $APP_HOME is the working directory, not the app folder. Have a look at https://github.com/richaagr/CustomLintDemoApp to see the structuring. – Infant Dev Nov 14 '16 at 05:18
  • sorry, I've been extremely busy, I'll get back to you sometime during the day – hopeman Nov 16 '16 at 08:44
  • I fixed the path but no cigar, so I changed the structure of the project and moved the libs folder to the $APP_HOME like in github.com/richaagr/CustomLintDemoApp however I had the same result, the library didn't do any checks – hopeman Nov 16 '16 at 10:05
0

@hopeman copy the jar file to the /.android/lint folder. Android will pick your custom lint rules.

Madhu
  • 11
  • 2
  • thanks for you feedback, as you can see above in my description, this isn't my problem. Your suggestion does work, but isn't what I'm looking for. The problem here is that every developer has to clone the repository and continually check for new updates... Our jenkins server is great and can update the library in the app project automatically when we make changes to the linting project, so I'm still looking for a solution that enables me to add the library to my app project and check the rules on a per project basis. – hopeman Feb 07 '17 at 13:13
  • @hopeman Did you find fix for this issue- "Custom linting rules failing with com.android.tools.build:gradle:2.3.0". I have been facing the same issue. Any help would be appreciated. – Madhu Mar 23 '17 at 19:54
  • @Mandu sorry nothing yet, as soon as we get it figured out I'd be more than happy to share. Happy coding... – hopeman Mar 24 '17 at 10:53
  • @hopeman thanks! if i figure it out..i will share it as well. – Madhu Mar 24 '17 at 20:02