DJIのサンプルプログラムGSDemoのSDKバージョンを4.16.1にしてエラーが出るときの対処法

DJI Mobile SDKを学ぶうちにGSDemoのアプリを動かすことになった．今回タイトルのような事態となったので対処法を調べた．
公式のサンプルではSDKが4.15となっており，現時点(2022年6月6日)で発表されているSDK4.16.1には対応していない．
今回4.16.1にバージョンをあげたときにエラーが出たので，自分が行った対処法を記録する．
# 実行環境
| OS | AndroidStudio |
|:-:|:-:|
| Windows 10 Home | Bumblebee 2021.1.1 RC1 |
# java.lang.RuntimeException: Manifest merger failed with multiple errors, see logsがでたとき
### ①AndroidManifest.xmlのMerged Manifestをクリックする(画像赤枠)
![ManifestMergerFailed.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2671458/19184f77-a7e8-a592-d4d5-b30ae4fd6c08.png)
### ②表示されるエラーを確認(画像赤枠)
![MergeManifestError.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2671458/84d62121-235c-b1ae-65d3-5de0f2fc91b2.png)
今回はuses-sdk:minSdkVersion 19 cannot be smaller than version 21 declared in libraryと表示されていたのでModuleのbuild.gradleのminSdkVersionを変える
```diff_gradle
defaultConfig {
-       minSdkVersion 19
+       minSdkVersion 21
        targetSdkVersion 30
        multiDexEnabled true
        ndk {
            // On x86 devices that run Android API 23 or above, if the application is targeted with API 23 or
            // above, FFmpeg lib might lead to runtime crashes or warnings.
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }
        multiDexEnabled true //Mention in the doc
    }
```
# com.google.android.gms.internal.zzbglが見つからないといわれたとき
Moduleのbuild.gradleに追加
```gradle
implementation 'com.google.android.gms:play-services-maps:18.0.2'
```
# Failed resolution of: Lcom/google/android/gms/common/internal/zzbqがでたとき
Moduleのbuild.gradleに追加
```gradle
implementation 'com.android.support:multidex:1.0.3'
```
Projectのbuild.gradleのallprojectsにsubprojectsを追加
```diff_gradle
allprojects {
    repositories {
        google()
        mavenCentral()
    }
+   subprojects {
+       project.configurations.all {
+           resolutionStrategy.eachDependency { details ->
+               if (details.requested.group == 'com.google.android.gms'
+                       && !details.requested.name.contains('multidex') ) {
+                   details.useVersion "12.0.1"
+               }
+           }
+       }
+   }
}
```
# まとめ
最後に対処前と対処後のbuild.gradleの全文を載せる．
特に言及はしていないが，dependenciesの中に記述しているimplementationの中で`implementation 'com.google.android.gms:play-services:12.0.1'`以外はバージョンを上げている．
## 対処前
```gradle:build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
```
```gradle:build.gradle(Module)
apply plugin: 'com.android.application'

repositories {
    mavenLocal()
}

android {
    compileSdkVersion 30
    buildToolsVersion '30.0.2'
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 30
        multiDexEnabled true
        ndk {
            // On x86 devices that run Android API 23 or above, if the application is targeted with API 23 or
            // above, FFmpeg lib might lead to runtime crashes or warnings.
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }
        multiDexEnabled true //Mention in the doc
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dexOptions {
        incremental = true
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }

    packagingOptions {
        doNotStrip "*/*/libdjivideo.so"
        doNotStrip "*/*/libSDKRelativeJNI.so"
        doNotStrip "*/*/libFlyForbid.so"
        doNotStrip "*/*/libduml_vision_bokeh.so"
        doNotStrip "*/*/libyuv2.so"
        doNotStrip "*/*/libGroudStation.so"
        doNotStrip "*/*/libFRCorkscrew.so"
        doNotStrip "*/*/libUpgradeVerify.so"
        doNotStrip "*/*/libFR.so"
        doNotStrip "*/*/libDJIFlySafeCore.so"
        doNotStrip "*/*/libdjifs_jni.so"
        doNotStrip "*/*/libsfjni.so"
        doNotStrip "*/*/libDJICommonJNI.so"
        doNotStrip "*/*/libDJICSDKCommon.so"
        doNotStrip "*/*/libDJIUpgradeCore.so"
        doNotStrip "*/*/libDJIUpgradeJNI.so"
        exclude 'META-INF/rxjava.properties'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}


dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.squareup:otto:1.3.8'
    implementation('com.dji:dji-sdk:4.15', {
        /**
         * Uncomment the "library-anti-distortion" if your app does not need Anti Distortion for Mavic 2 Pro and Mavic 2 Zoom.
         * Uncomment the "fly-safe-database" if you need database for release, or we will download it when DJISDKManager.getInstance().registerApp
         * is called.
         * Both will greatly reducing the size of the APK.
         */
        exclude module: 'library-anti-distortion'
        //exclude module: 'fly-safe-database'
    })
    compileOnly 'com.dji:dji-sdk-provided:4.15'

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.core:core:1.3.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.annotation:annotation:1.2.0'

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.13.2'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'com.google.android.gms:play-services:10.2.1'
    implementation 'com.google.android.gms:play-services-ads:10.2.1'
    implementation 'com.google.android.gms:play-services-auth:10.2.1'
    implementation 'com.google.android.gms:play-services-gcm:10.2.1'
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
}
```
## 対処後
```gradle:build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
    subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'com.google.android.gms'
                        && !details.requested.name.contains('multidex') ) {
                    details.useVersion "12.0.1"
                }
            }
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
```
```gradle:build.gradle(Module)
apply plugin: 'com.android.application'

repositories {
    mavenLocal()
}

android {
    compileSdkVersion 31
    buildToolsVersion '30.0.3'
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 31
        multiDexEnabled true  //Mention in the doc
        ndk {
            // On x86 devices that run Android API 23 or above, if the application is targeted with API 23 or
            // above, FFmpeg lib might lead to runtime crashes or warnings.
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dexOptions {
        incremental = true
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }

    packagingOptions {
        doNotStrip "*/*/libdjivideo.so"
        doNotStrip "*/*/libSDKRelativeJNI.so"
        doNotStrip "*/*/libFlyForbid.so"
        doNotStrip "*/*/libduml_vision_bokeh.so"
        doNotStrip "*/*/libyuv2.so"
        doNotStrip "*/*/libGroudStation.so"
        doNotStrip "*/*/libFRCorkscrew.so"
        doNotStrip "*/*/libUpgradeVerify.so"
        doNotStrip "*/*/libFR.so"
        doNotStrip "*/*/libDJIFlySafeCore.so"
        doNotStrip "*/*/libdjifs_jni.so"
        doNotStrip "*/*/libsfjni.so"
        doNotStrip "*/*/libDJICommonJNI.so"
        doNotStrip "*/*/libDJICSDKCommon.so"
        doNotStrip "*/*/libDJIUpgradeCore.so"
        doNotStrip "*/*/libDJIUpgradeJNI.so"
        exclude 'META-INF/rxjava.properties'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}


dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.squareup:otto:1.3.8'
    implementation('com.dji:dji-sdk:4.16.1', {
        /**
         * Uncomment the "library-anti-distortion" if your app does not need Anti Distortion for Mavic 2 Pro and Mavic 2 Zoom.
         * Uncomment the "fly-safe-database" if you need database for release, or we will download it when DJISDKManager.getInstance().registerApp
         * is called.
         * Both will greatly reducing the size of the APK.
         */
        exclude module: 'library-anti-distortion'
        //exclude module: 'fly-safe-database'
    })
    compileOnly 'com.dji:dji-sdk-provided:4.16.1'

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'androidx.core:core:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.annotation:annotation:1.3.0'

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.13.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.android.gms:play-services-maps:18.0.2'
    implementation 'com.google.android.gms:play-services:12.0.1'
    implementation 'com.google.android.gms:play-services-ads:21.0.0'
    implementation 'com.google.android.gms:play-services-auth:20.2.0'
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
}
```