Android using React Native on Ubuntu

By John Keyes

July 27, 2021 at 10:21

android

Install JDK

I followed these steps for how to install Open JDK 8 on Ubuntu.

Emulator Not Running

After launching AVD Manager the emulators wouldn’t run. First it was “unable to locate abd” and then it was not able to run “avd”.

A partial fix for this was to add my user to the kvm group to give access to /dev/kvm.

sudo apt install qemu-kvm
sudo adduser $USER kvm

Via Stack Overflow

At the time of writing I still hadn’t figured out the abd issue.

Multidex

If you get an error similar to:

Too many field references: 131000; max is 65536.

You will need to enable multi-dex. I added the following to the project in build.gradle:

defaultConfig {
  ...
  multiDexEnabled true
}

See Enable multidex for apps with over 64k methods

Running the App

To run the app first you’ll need to ensure Metro is running. This bundles the JavaScript and other assets into a single file to deploy on the emulator.

For FaceItDown use the start command:

npm run start

The .apk also needs to be installed on the emulator.

npm run android

Minumum SDK

When building FaceItDown there was an error:

Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [:react-native-community_geolocation] /datadisk/toyota/toyota-faceitdown-app/node_modules/@react-native-community/geolocation/android/build/intermediates/library_manifest/debug/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.reactnativecommunity.geolocation" to force usage (may lead to runtime failures)

The reason behind this error was the android/build.gradle had the following settings:

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    ...

    defaultConfig {
        ...
        minSdkVersion minSdkVersion
        targetSdkVersion targetSdkVersion
        ...
    }

and the root project build.gradle had the following:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}

The values for minSdkVersion and targetSdkVersion are not being set. To fix this we removed the _SdkVersion values from android/build.gradle and added the following to android subproject in the root build.gradle:

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

Now the minSdkVersion and targetSdkVersion are referenced from the root properties.

Update from Elliot

The error was being thrown because a node module (react-native-community/geolocation) that was being added to the project as a module dependency (it gets built along with the app rather than it being a pre-built binary) https://bitbucket.org/tapadoo/toyota-faceitdown-app/src/255a98fc6bf573c8b81deb901dee0b098f246dd2/android/settings.gradle#lines-7 had a minSDK lower than the projects, which was then in conflict with the minimum requirements needed for that project to build.

To fix that you updated the subprojects block within android/build.gradle to set those SDK values to all subprojects that were being built to ensure they were all using the same SDK version, which included the main android/app/build.gradle project.

Last updated: July 27, 2021 at 10:21