androidreact-nativedetox

Struggling to get Android build working due to minSdkVersion


I think I must be missing something here, as far as I'm aware I've tried these approaches after reading a number of articles and I can't seem to get things working. I'm triggering these builds manually at the moment, using the command that detox would use which is ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug though I've also tried using npx detox build --configuration android.emu.debug directly too.

My error is a typical minSdkVersion mismatch:

uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [com.facebook.react:react-native:0.64.0] /home/iw651/.gradle/caches/transforms-2/files-2.1/354c8f3d479b5a1203bfff874da058bc/jetified-react-native-0.64.0/AndroidManifest.xml as the library might be using APIs not available in 16
        Suggestion: use a compatible library with a minSdk of at most 16,
                or increase this project's minSdk version to at least 21,
                or use tools:overrideLibrary="com.facebook.react" to force usage (may lead to runtime failures)

build.gradle
So the things that are confusing me somewhat, are firstly my project's minSdkVersion is set to at least 21... This is the top of my /android/build.gradle file:

buildscript {
    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 23
        compileSdkVersion = 29
        targetSdkVersion = 29
        kotlinVersion = '1.3.61'
        ndkVersion = "20.1.5948944"
    }

Within my android/app/build.gradle I have the following:

defaultConfig {
     minSdkVersion rootProject.ext.minSdkVersion
     targetSdkVersion rootProject.ext.targetSdkVersion
     multiDexEnabled true
     ...
}

So really I believe the following has been done. But it's obviously still throwing an error.

or increase this project's minSdk version to at least 2

tools:overrideLibrary
I'm not exactly sure how to do this, I've tried setting this in my /android/app/src/debug/AndroidManifest.xml file. I've tried a few permutations:

<uses-sdk minSdkVersion="16" tools:overrideLibrary="com.facebook.react"/>
<uses-sdk minSdkVersion="21" tools:overrideLibrary="com.facebook.react"/>
<uses-sdk tools:overrideLibrary="com.facebook.react"/>

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

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-sdk minSdkVersion="16" tools:overrideLibrary="com.facebook.react"/>
    <application
            android:usesCleartextTraffic="true"
            tools:targetApi="28"
            tools:ignore="GoogleAppIndexingWarning">
        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>
</manifest>

None of those permutations seem to help either.

use a compatible library with a minSdk of at most 16
This just leaves me with this option, for which I'm going to raise a PR against the package in question. But it still doesn't help me build until a new release is made.

Can anyone see what I'm missing? Or could some caching be getting in the way between builds?


Solution

  • The issue is that RN 0.64 increased its minSdkVersion from 16 to 21. Many RN libraries have minSdkVersion hard-coded at 16, which is causing Detox Android builds to fail.

    I am unsure why regular Android builds succeed and Detox builds fail, but the fix is to edit the library's android/build.gradle and change it to read minSdkVersion from the root project:

    minSdkVersion rootProject.hasProperty('minSdkVersion') ? rootProject.minSdkVersion : 16
    

    Ideally this will be fixed in the core library through a PR (don't forget to make one). In the meanwhile, you can fork the project yourself, fix the bug and use the fixed project by referencing it in package.json:

    "<lib-name>": "github:<username>/<lib-name>"
    

    Alternatively you can use patch-package to apply the fix to node_modules/<libname>/android/build.gradle when running npm install.

    This seems to be a quite common error in RN libraries. In our mid-sized RN project I've had to patch three libraries, including react-native-webview.