Gradle dependencies for different api levels

Unfortunately, I have to support Android 2.3, but I want to use a third-party ui widget with min-sdk 14 (Android 4.0).

Is there a way to enable dependency using min-sdk 14?

In the code, I would check Build.SDK_INT to determine whether to use the ui widget with a minimum SDK of 14 or fallback user interface widgets.

+5
source share
2 answers

This is not possible ... If you have a dependency, then inside your compilation. inside your apk. You can choose whether you want to use this third-party library with if (SDK_INT), but lib will be compiled in your application as well.

0
source

Unfortunately, since you must include the dependency, it will always be compiled into your project.

You can split your project into separate projects. If you created something like BaseApp and IceCreamSandwichApp , you could set different minSdkVersions for each along with a different set of dependencies.

Thus, you must have modules in your application:

 your-app/BaseApp 

and

 your-app/IceCreamSandwichApp 

And your gradle files look something like this:

your-app / settings.gradle

 include ':BaseApp', ':GingerbreadApp' 

your-app / BaseApp / build.gradle

 android { buildToolsVersion '22.0.1' defaultConfig { minSdkVersion 9 compileSdkVersion 21 targetSdkVersion 21 } ... dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) } } 

your-app / IceCreamSandwichApp / build.gradle

 android { buildToolsVersion '22.0.1' defaultConfig { minSdkVersion 14 compileSdkVersion 21 targetSdkVersion 21 } ... dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile project(":BaseApp") compile 'the.third.party:lib:here:1.0.0' } } 
0
source

Source: https://habr.com/ru/post/1206360/


All Articles