在具有不同 SDK 最小版本的项目中使用相同的自定义库
Using the same custom library in projects with different SDK min version
我有一个带有多个实用程序 classes 的自定义库。到目前为止,我在 min SDK 21
的单个项目中使用了它。
现在,我想在另一个项目中重用 class 中的一个,它有 min SDK 14
。 class 代码适用于版本 14,但 Gradle 不喜欢库的最小 SDK 是 21。
解决此类问题的推荐方法是什么?我想重复使用代码,而不是复制它。
您可以使用 Manifest Merger 覆盖导入库的 <uses-sdk>
。
来自官方文档:
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.
例如,如果您应用的主清单应用 overrideLibrary,如下所示:
<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"/>
...
然后可以合并以下清单,而不会出现关于 <uses-sdk>
标签的错误,并且合并后的清单保留应用程序清单中的 minSdkVersion="2"
。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lib1">
<uses-sdk android:minSdkVersion="4" />
...
我有一个带有多个实用程序 classes 的自定义库。到目前为止,我在 min SDK 21
的单个项目中使用了它。
现在,我想在另一个项目中重用 class 中的一个,它有 min SDK 14
。 class 代码适用于版本 14,但 Gradle 不喜欢库的最小 SDK 是 21。
解决此类问题的推荐方法是什么?我想重复使用代码,而不是复制它。
您可以使用 Manifest Merger 覆盖导入库的 <uses-sdk>
。
来自官方文档:
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.
例如,如果您应用的主清单应用 overrideLibrary,如下所示:
<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"/>
...
然后可以合并以下清单,而不会出现关于 <uses-sdk>
标签的错误,并且合并后的清单保留应用程序清单中的 minSdkVersion="2"
。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lib1">
<uses-sdk android:minSdkVersion="4" />
...