Gradle dependencies for different api levels

吃可爱长大的小学妹 提交于 2021-02-18 11:26:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!