AAR Dependency for Lint Tests

77 views
Skip to first unread message

kiran puppala

unread,
Sep 23, 2021, 10:12:57 AM9/23/21
to lint-dev
Hi Tor, 

Our Lint Detector code depends on the resources of an external maven dependency. We want to test this Detector. Can you please help us with how to add external AAR dependency to lint test files?

What I have tried so far:
1. I have tried adding this dependency using Gradle test file. But the test cannot resolve this dependency. 

val APP_LEVEL_GRADLE = gradle(
            "app/build.gradle",
            """
                ..... 
                .....
                dependencies {
                    //Some external aar dependency
                    implementation 'com.example:dependency:x.x.x'
                }
            """).indented()

 val TOP_LEVEL_GRADLE = gradle(
            "build.gradle",
            """
                buildscript {
                  repositories {
                    google()
                    mavenCentral()
                  }
                  dependencies {
                    classpath 'com.android.tools.build:gradle:4.2.2'
                  }
                }

                allprojects {
                  repositories {
                    mavenCentral()
                    google()
                    // Dependency artifactory
                    maven(url = "http://dependency_artifactory")
                  }
                }
                """
            ).indented()

lint()
            .files(
                TOP_LEVEL_GRADLE,
                APP_LEVEL_GRADLE,
                XML_RESOURCE_USAGE
            )
            .issues(CustomResourceDetector.ISSUE)
            .run()
            .expectClean()

2. There is no file type for AAR like TestFiles.jar for jar dependency. 
3. I tried referencing lint API documentation but can't find anything relevant to this. 


 


Tor Norbye

unread,
Sep 23, 2021, 11:11:45 AM9/23/21
to kiran puppala, lint-dev
This is a pretty tricky corner case since it has a lot of build system detail in it. When you say "implementation 'com.example:dependency:x.x.x'", Gradle does a lot of heavy lifting -- executing build scripts to find which repositories to connect to, looking at the project graph to figure out which version to load (it won't always be the one asked for here, since if another dependency depends transitively on a higher version -- that one will be used instead -- unless you've configured a custom version resolution strategy, etc etc etc. Gradle will also download a bunch of artifacts, and in the case of AAR, unzip it into a local folder hierarchy -- and it's this unzipped set of folders that is passed to lint. Lint does not try to replicate all of the above machinery -- it would for sure not match Gradle's implementation over time and would lead to subtle problems.

So instead, you have to configure the model yourself.

There are some built-in tests which do things in this area which you may find helpful.

A fairly straightforward usage is in RestrictToDetectorTest:

Here we register the jar file that would have been found inside the aar file manually to the classpath, and we also register the aar dependency in a gradle test file. Lint's test machinery will associate the two with each other -- so calls which resolve into the jar file found in libs/exploded-aar/my.group.id/mylib/25.0.0-SNAPSHOT/jars/classes.jar will be understood to correspond to the library my.group.id:25.0.0-SNAPSHOT. (AGP *used* to unpack AAR files in paths like that in the project directory -- some years ago it stopped doing that; they're now in ~/.gradle/ something and the paths do not encode the library coordinates like this anymore, but it's still used here.)

A more complicated but also flexible approach is used in PrivateResourceDetectorTest:

Here you can see we're manually configuring the libraries. 

--
You received this message because you are subscribed to the Google Groups "lint-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lint-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lint-dev/d8534386-3932-4a1c-8264-d5316be1dc0en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages