在 Android 上了解 tools:overrideLibrary

Understanding tools:overrideLibrary on Android

我的主应用程序 minSdk="14",Android v. 4.16.1 的 Facebook SDK minSdk="15"

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.dmitriev_m.sample_android"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.facebook.android:facebook-android-sdk:4.16.1'
    compile 'com.android.support:appcompat-v7:25.1.0'
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.dmitriev_m.sample_android">

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="25"
        tools:overrideLibrary="com.facebook, android.support.customtabs" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

documentation 关于合并 AndroidManifest.xml 文件的内容如下:

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.

但我的合并清单看起来像是应用覆盖了 Facebook SDK 的清单文件(最小 SDK 为 14):

运行 这个应用程序安全吗?我的意思是当 Facebook SDK 从 Android API 15 调用方法时,应用程序应该崩溃,不是吗?

是的,如果库调用 API 15 函数而设备只有 API 14,它会崩溃。如果设备是 API15 或更高版本,它仍然可以工作。

不过在这种情况下我不会担心。 14 是 Anroid 3.x。只制造了极少数 3.x 台设备 - Android 平板电脑直到 4.0 发布后才开始流行。 3.x 型号的使用比例不到所有设备的 .1%。如果他们不工作,真的值得花时间和精力来支持他们吗?老实说,我只是将您的 minsdk 提高到 15 或更高。 (提高到 19 只会消除 10% 的全球市场,这将严重偏向亚洲、非洲和南美洲的低收入国家。如果你的主要目标是美国和欧盟,可能不值得支持成本那个)。