10

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.:)

Harshawardhan
  • 1,521
  • 1
  • 24
  • 29

4 Answers4

9

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.

Community
  • 1
  • 1
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • 1
    As I mentioned in above comment I've used sdk check as dev guild suggests try to avoid it. Is there any way to achieve that? my library uses API 11 to have holographic theme for app running on >=3.0 and have dialog theme for app running on < 3.0 I don't want to change my app build target from 8 to 11 as its project requirement for application at the same time want to have holographic theme for 3.0 and higher so changed build target to 11 but avoiding it how to do that? please help Thanks I have used it only in alertDialog builder object to have holographic theme for 3.0 and above. – Harshawardhan Mar 12 '12 at 06:08
  • @devilcol, I also use code level SDK version check in my project (but not from android library project), as I said, it is doable but you need maintain your conditional branch in code carefully and take your own responsibility to properly test your app on different target Android system. – yorkw Mar 12 '12 at 08:34
  • I have to disagree with the sentence `If the library project uses an API level that is higher than that of the application, the application project will not compile.` because my libraries use a mix of API level 16 and 17 and the main app project still compiles just fine at API level 12. NOTE: my manifest has the `targetSDKVersion` removed. It only shows a minSDKVersion of 12. – Someone Somewhere Nov 15 '13 at 19:38
  • @yorkw how can you make your application compile if you are using a library that requires a higher SDK version? I get the error Manifest merging failed... I would like to check the SDK version in the code but this error prevents my app from compiling... – Raphael Royer-Rivard Mar 21 '14 at 18:34
4

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>" />

Ian Pinto
  • 2,199
  • 1
  • 21
  • 24
2

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.

XiaoChuan Yu
  • 3,951
  • 1
  • 32
  • 44
  • 1
    Do you actually have API 11 references in your library's code? Yes I have used it only in alertDialog builder object to have holographic theme for 3.0 and above. But using the checks for sdk version i am able to build the code on both 3.0 lower and higher. Just wanted to know the approach.. – Harshawardhan Mar 12 '12 at 05:52
0

in your manifest you can simply add:

<uses-sdk
      tools:overrideLibrary="<libraries packagename>"
    />
Elad
  • 1,523
  • 13
  • 10