ilomambo
ilomambo

Reputation: 8350

Using the same custom library in projects with different SDK min version

I have a custom library with several utility classes. I used it so far in a single project with min SDK 21.

Now, I want to reuse one of the classes in another project, which has min SDK 14. The class code is fine with version 14, but Gradle does not like that the library's min SDK is 21.

What is the recommended approach to this kind of problem? I'd like to reuse the code, not to duplicate it.

Upvotes: 0

Views: 84

Answers (1)

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

You can use, Manifest Merger to Override <uses-sdk> for imported libraries.

From the official documentation:

By default, when importing a library with a minSdkVersion value that's higher than the main manifest file, an error occurs and the library cannot be imported. To make the merger tool ignore this conflict and import the library while keeping your app's lower minSdkVersion value, add the overrideLibrary attribute to the tag. The attribute value can be one or more library package names (comma-separated), indicating the libraries that can override the main manifest's minSdkVersion.

For example, if your app's main manifest applies overrideLibrary like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.app"
          xmlns:tools="http://schemas.android.com/tools">
  <uses-sdk android:targetSdkVersion="22" android:minSdkVersion="2"
            tools:overrideLibrary="com.example.lib1, com.example.lib2"/>
...

Then the following manifest can be merged without an error regarding the <uses-sdk> tag, and the merged manifest keeps minSdkVersion="2" from the app manifest.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.lib1">
   <uses-sdk android:minSdkVersion="4" />
...

Upvotes: 1

Related Questions