问题
unfortunately I have to support Android 2.3, but I want to use a third party ui widget that has min-sdk 14 (Android 4.0).
Is there a way to include the dependency with min-sdk 14?
In code I would check Build.SDK_INT
to determine whether to use the ui widget with min SDK 14 or a fallback UI widget.
回答1:
It is not possible... If you have a dependency it is inside you compilation. inside your apk. You can choose if you want to use this third party lib with if(SDK_INT) but the lib will be compiled and in your app.
回答2:
Unfortunately, since you have to include a dependency it will always be compiled into your project.
You could break your project out into separate projects. If you created something like a BaseApp
and a IceCreamSandwichApp
you could set different minSdkVersions
for each along with a different set of dependencies.
So you would have to modules in your app:
your-app/BaseApp
and
your-app/IceCreamSandwichApp
And your gradle files would 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'
}
}
来源:https://stackoverflow.com/questions/26787246/gradle-dependencies-for-different-api-levels