vanomart
vanomart

Reputation: 1839

Android Studio Lint - set API level for lint

Here is the thing. I have an app that is compatible with API 15 and above, but since it's pretty big and I've already hit a 65k methods limit, I had to make it a descendant of MultiDexApplication class. This slows down the build times a bit, so I had to implement some optimization to speed up the process. I have the following code in my manifest, which significantly reduce build times when building for API >= 21 (taken from some other SO thread):

    productFlavors {
        dev {
            minSdkVersion 21
        }
        prod {
            minSdkVersion 15
        }
    }

Everything is working perfectly, but the problem is that during the development, Android studio thinks that my minSdkVersion SDK level is 21 (correctly), and the lint does not show me incompatible API (15-21). What I really want is to be able to build with minSdkVersion set to 21 (fast build), but set the "lint minSdkVersion" to 15, so I see the parts of the code that are not compatible with older API than 21. I tried to google it and also to look into AS lint preferences but I didn't find anything useful. Thanks for any suggestions. My current solution is to switch minSdkVersion in dev flavor to 21 and check if there's any error, but this is not really what I want.

Upvotes: 8

Views: 1195

Answers (1)

Alex Lipov
Alex Lipov

Reputation: 13958

This gist answers your question. It shows how to build the project with whatever dev minimum SDK value, while maintaining production minimum SDK value for Lint warnings.

To summarize the post, you can dynamically calculate the minSdkVersion value:

int minSdk = hasProperty('devMinSdk') ? devMinSdk.toInteger() : 15
apply plugin: 'com.android.application'

android {
    ...
    defaultConfig {
        minSdkVersion minSdk
        ...
    }
}

In this example, we're checking if devMinSdk property defined, and if true - we're using it. Otherwise, we default to 15.

How do we pass devMinSdk value to build script? Two options:

Using command line:

./gradlew installDebug -PdevMinSdk=21

Using Android Studio preferences:

Go to Preferences (Settings on Windows) -> Build, Execution, Deployment -> Compiler -> put -PdevMinSdk=21 in Command-line Options text box.

Android Studio compiler options

Upvotes: 7

Related Questions