Library project build version higher than projects using it

大兔子大兔子 提交于 2019-12-19 06:14:12

问题


I am using Android Library Project. I have set my library project build target to say 11 to use api 11 and to have forward compatibility and I am putting check for min sdk version so that app will not crash when run on os < 11

My other projects have build target set to 8 so I want to know is it the correct way to set library project build version higher than the projects with lower build target going to use it?

right now I haven't observed any crash. Just wanted to know can the library with higher build version be used in projects with lower build sdk version than library.

Thanks in advance.:)


回答1:


It is probably doable by branch checking SDK version in code level like if android.os.Build.VERSION.SDK_INT < 11 then do not run this, but not recommended by dev guide:

Platform version must be lower than or equal to the Android project

A library is compiled as part of the dependent application project, so the API used in the library project must be compatible with the version of the Android library used to compile the application project. In general, the library project should use an API level that is the same as — or lower than — that used by the application. If the library project uses an API level that is higher than that of the application, the application project will not compile. It is perfectly acceptable to have a library that uses the Android 1.5 API (API level 3) and that is used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) project, for instance.




回答2:


It is possible for you to use library supporting higher minSdkVersion than that in your project. To do so, in your projects manifest file add tools:overrideLibrary="<libraries packagename>" manifest element uses-sdk

<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" tools:overrideLibrary="<libraries packagename>" />




回答3:


Using a library with higher API level than the application is not recommended. If you referenced any API 11 only features in library and then use API 8 to compile with your app, then it should not compile at all. Do you actually have API 11 references in your library's code?

See Android docs:

In general, the library project should use an API level that is the same as — or lower than — that used by the application. If the library project uses an API level that is higher than that of the application, the application project will not compile.




回答4:


in your manifest you can simply add:

<uses-sdk
      tools:overrideLibrary="<libraries packagename>"
    />


来源:https://stackoverflow.com/questions/9658188/library-project-build-version-higher-than-projects-using-it

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