5

I am following this post to solve an error from my RN 0.59 app. What I did was add 2 lines to

  tools:replace="android:appComponentFactory"
  android:appComponentFactory="whateverString"

AndroidManifest.xml under android/src/main. Here is the xml file after change:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.emps_app">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme"
      tools:replace="android:appComponentFactory" //<<===added 2 lines
      android:appComponentFactory="whateverString">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

</manifest>

But the react-native run-android still throws the error:

* What went wrong:
Execution failed for task ':app:generateDebugBuildConfig'.
> org.xml.sax.SAXParseException; systemId: file:/C:/D/code/js/emps_app/android/app/src/main/AndroidManifest.xml; lineNumber: 14; columnNumber: 52; The prefix "tools" for attribute "tools:replace" associated with an element type "application" is not bound.

My RN app was working fine before throwing error about version of Android library.There are several posts about the error but none of them seems working in my case. My emulator is running with Android 8.1/API 27. Also please keep in mind that RN 0.60 will support Androidx.

Here is the build.gradle (under android/app/. comment removed for easy reading):

apply plugin: "com.android.application"

import com.android.build.OutputFile


project.ext.react = [
    entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.emps_app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-gesture-handler')
    implementation project(':react-native-keychain')
    implementation project(':react-native-linear-gradient')
    implementation project(':react-native-device-info')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

Here is the top level build.gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'

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

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}
user938363
  • 9,990
  • 38
  • 137
  • 303
  • 2
    add `xmlns:tools="http://schemas.android.com/tools"` in Manifest section – IntelliJ Amiya Jun 19 '19 at 06:35
  • The error is a different one: `* What went wrong: Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: Program type already present: android.support.v4.graphics.drawable.IconCompatParcelizer Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.`.It mentions a duplicate class presented. What to find the duplicate class? – user938363 Jun 19 '19 at 07:29
  • If adding `android.useAndroidX=true android.enableJetifier=true` to gradle.properties, then there are error with module `react-native-gesture-handler` build saying missing packages. – user938363 Jun 21 '19 at 00:29
  • I rebuilt the project and upgrades the RN to 0.59.9. The problem disappears for now. None of the solutions I found online worked in my case. – user938363 Jun 24 '19 at 19:06

2 Answers2

1

Please do not used below line in AndroidManifest.xml.

tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"

But for run android app you can use android studio. Because sometime it's not run through commandline. So you can used android studio and run same as android application but you need to open YourApp>android> Run your app.

Jyubin Patel
  • 1,373
  • 7
  • 17
1

I'm running the RN upgrade to 0.60.0 gauntlet myself :-) Something that stung me was my build.gradle listed a dependency with a '+' in its version number: so gradle started using a newer version than I had realized, and that version had duplicate classes.

From a commandline, run ./gradlew app:dependencies in the android subdirectory of your project.

I'd suspect that the react-native dependency is pulling something that might be inadvertently colliding with something.

brismith
  • 696
  • 3
  • 18
  • Very likely. There is a post online saying that without any code change, the merger error suddenly appears. Something must be happening behind the scene. – user938363 Jun 25 '19 at 19:18
  • For me in particular, it was the com.google.android.gms:play-services dependencies: my build.gradle said '+', and things changed on 17 June (https://developers.google.com/android/guides/releases), which is right about the time things went haywire for me. So for me, I explicitly specified the versions: 16.1.0 for base, etc. – brismith Jun 25 '19 at 19:24