facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.28k stars 24.22k forks source link

Fresh React Native CLI init project does not work on Android with a Mac M1 Max #33016

Closed xlangit closed 2 years ago

xlangit commented 2 years ago

Description

I have a Mac m1 Max, trying to get a fresh project running,it is runing IOS without problem, but no luck on android, the error message I'm getting is the following when trying to build and run using react-native run-android --verbose The following errors are reported:

I did install java with sdkman:

| 8.0.322 | zulu | installed | 8.0.322-zulu


error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.

Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01

Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01

Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01

Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/03 to old ns http://schemas.android.com/sdk/android/repo/addon2/01

Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01

Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/03 to old ns http://schemas.android.com/sdk/android/repo/repository2/01

Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/03 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01

Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01

Warning: unexpected element (uri:"", local:"base-extension"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level>


FAILURE: Build failed with an exception.


* What went wrong:

Execution failed for task ':app:processDebugResources'.

> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.

   > Failed to transform appcompat-1.3.0.aar (androidx.appcompat:appcompat:1.3.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-runtime}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/d7f19fb48ed68be2921ae87dc8e58f8e/transformed/appcompat-1.3.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #1: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.

   > Failed to transform core-1.5.0.aar (androidx.core:core:1.5.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.status=release}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/540dbfb90b248d7b59cd154497c15f99/transformed/core-1.5.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #0: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.

   > Failed to transform core-1.5.0.aar (androidx.core:core:1.5.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-runtime}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/540dbfb90b248d7b59cd154497c15f99/transformed/core-1.5.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #0: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.

   > Failed to transform core-1.5.0.aar (androidx.core:core:1.5.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.status=release}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/540dbfb90b248d7b59cd154497c15f99/transformed/core-1.5.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #0: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.

   > Failed to transform core-1.5.0.aar (androidx.core:core:1.5.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.status=release}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/540dbfb90b248d7b59cd154497c15f99/transformed/core-1.5.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #0: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.

   > Failed to transform core-1.5.0.aar (androidx.core:core:1.5.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.status=release}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/540dbfb90b248d7b59cd154497c15f99/transformed/core-1.5.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #0: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.

   > Failed to transform core-1.5.0.aar (androidx.core:core:1.5.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.status=release}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/540dbfb90b248d7b59cd154497c15f99/transformed/core-1.5.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #0: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.

   > Failed to transform core-1.5.0.aar (androidx.core:core:1.5.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.status=release}.

      > Execution failed for AarResourcesCompilerTransform: /Users/xlan/.gradle/caches/transforms-3/540dbfb90b248d7b59cd154497c15f99/transformed/core-1.5.0.

         > AAPT2 aapt2-4.2.2-7147631-osx Daemon #0: Daemon startup failed

           This should not happen under normal circumstances, please file an issue if it does.


* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.


* Get more help at https://help.gradle.org


BUILD FAILED in 16m 10s


    at makeError (/Users/xlan/projects/baredemo/node_modules/execa/index.js:174:9)

    at /Users/xlan/projects/baredemo/node_modules/execa/index.js:278:16

    at processTicksAndRejections (node:internal/process/task_queues:96:5)

    at async runOnAllDevices (/Users/xlan/projects/baredemo/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5)

    at async Command.handleAction (/Users/xlan/projects/baredemo/node_modules/@react-native-community/cli/build/index.js:192:9)

  --

Version

0.67.1

Output of npx react-native info

info Fetching system and libraries information... (node:10552) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency (Use node --trace-warnings ... to show where the warning was created) System: OS: macOS 12.2 CPU: (10) arm64 Apple M1 Max Memory: 32.65 GB / 64.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 17.4.0 - /opt/homebrew/bin/node Yarn: 1.22.17 - /opt/homebrew/bin/yarn npm: 8.3.1 - /opt/homebrew/bin/npm Watchman: 2022.01.24.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.2 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.2, iOS 15.2, macOS 12.1, tvOS 15.2, watchOS 8.3 Android SDK: API Levels: 30, 31, 32 Build Tools: 30.0.2, 32.0.0, 32.1.0 System Images: android-32 | Google APIs ARM 64 v8a Android NDK: Not Found IDEs: Android Studio: 2021.1 AI-211.7628.21.2111.8092744 Xcode: 13.2.1/13C100 - /usr/bin/xcodebuild Languages: Java: 1.8.0_322 - /Users/xlan/.sdkman/candidates/java/current/bin/javac npmPackages: @react-native-community/cli: Not Found react: 17.0.2 => 17.0.2 react-native: 0.67.1 => 0.67.1 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps to reproduce

react-native init baredemo cd baredemo react-native run-android --verbose

Snack, code example, screenshot, or link to a repository

my android/build.grade

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

buildscript { ext { buildToolsVersion = "30.0.2" minSdkVersion = 29 compileSdkVersion = 30 targetSdkVersion = 30 ndkVersion = "21.4.7075529" } repositories { google() mavenCentral() jcenter() } 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 { maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url("$rootDir/../node_modules/react-native/android") } maven { // Android JSC is installed from npm url("$rootDir/../node_modules/jsc-android/dist") } mavenCentral { // We don't want to fetch react-native from Maven Central as there are // older versions over there. content { excludeGroup "com.facebook.react" } } google() maven { url 'https://www.jitpack.io' } jcenter() } }

my android/app/build.grade

apply plugin: "com.android.application"

import com.android.build.OutputFile

/**

project.ext.react = [ enableHermes: false, // clean and rebuild if changing ]

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

/**

/**

/**

/**

/**

android { ndkVersion rootProject.ext.ndkVersion

compileSdkVersion rootProject.ext.compileSdkVersion

defaultConfig {
    applicationId "com.baredemo"
    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"
    }
}
signingConfigs {
    debug {
        storeFile file('debug.keystore')
        storePassword 'android'
        keyAlias 'androiddebugkey'
        keyPassword 'android'
    }
}
buildTypes {
    debug {
        signingConfig signingConfigs.debug
        if (nativeArchitectures) {
            ndk {
                abiFilters nativeArchitectures.split(',')
            }
        }
    }
    release {
        // Caution! In production, you need to generate your own keystore file.
        // see https://reactnative.dev/docs/signed-apk-android.
        signingConfig signingConfigs.debug
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

// 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:
        // https://developer.android.com/studio/build/configure-apk-splits.html
        // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
        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 =
                    defaultConfig.versionCode * 1000 + versionCodes.get(abi)
        }

    }
}

}

dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" // From node_modules

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
    exclude group:'com.facebook.fbjni'
}

debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
    exclude group:'com.facebook.flipper'
    exclude group:'com.squareup.okhttp3', module:'okhttp'
}

debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
    exclude group:'com.facebook.flipper'
}

if (enableHermes) {
    def hermesPath = "../../node_modules/hermes-engine/android/";
    debugImplementation files(hermesPath + "hermes-debug.aar")
    releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
    implementation jscFlavor
}

}

// 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.implementation into 'libs' }

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

xlangit commented 2 years ago

Any info, or guide will help on this issue. Thanks

cortinico commented 2 years ago

I don't have a M1 Mac so I can't really try this. Any guide/support from the community would be more than appreciated at this stage 🙏

kepocnhh commented 2 years ago

My environment - Monterey 12.2.1 (21D62) aarch64 I have the same issue when using the com.android.tools.build:gradle:7.0.2 plugin:

AAPT2 aapt2-7.0.2-7396180-osx Daemon #0: Daemon startup failed

But when using version 7.1.0, there is no such problem.

PS: I am not building React Native. Only java 11.0.14 + gradle 7.3.

xlangit commented 2 years ago

Ok I did change on my baredemo/android/gradle/wrapper/gradle-wrapper.properties this line distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip

to use the latest gradle version

and in this file baredemo/android/build.gradle

I did change this line classpath("com.android.tools.build:gradle:7.1.0")

to use the plugin as you suggested.

One thing I don't quite get if the plugin version is the same as the gradle version, and what version should we use.

But after cleaning my gradle folder -rf ~/.gradle
I did try again the build using react-native run-android --verbose and It works. Thanks a lot, I know need to try these with another project or and old proyect and see what is going on.

LorenzoInvernizzi commented 2 years ago

i encountered the same issue on a new mac pro M1. Above comment fixed the issue for me. The only thing is that the first time i run it i got the error:

Unable to install /Users/XX/Documents/XX/android/app/build/outputs/apk/debug/app-debug.apk com.android.ddmlib.InstallException: Unknown failure: Exception occurred while executing 'install': java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.pm.PackageManagerInternal.freeStorage(java.lang.String, long, int)' on a null object reference

I just rerun it and it worked. I thing it is because the simulator didn't finish to boot. Now i will do the same mods to a big project, lets hope it works

devandres-tech commented 2 years ago

Same as everyone, encountered the same problem on a brand new Mac M1 pro chip, I only had to add this line: classpath("com.android.tools.build:gradle:7.1.0") So basically I just changed the gradle version to 7.1.0, no need to change the distributionUrl. Well, it worked for me in my case.

jrnkng commented 2 years ago

I did both things described above and it finally solved the issue. Thanks!

cortinico commented 2 years ago

Closing as there is nothing much actionable here. We're about to release RN 0.69 which will contain logic to default your NDK version to 24, which has full support for M1. Once you also use the Zulu JDK 11, you should be all good building for M1 on Android

gaweki commented 1 year ago

i just curious this happen for my m1 2020 ram 16. it had been solved by upgrade these classpath("com.android.tools.build:gradle:7.2.1")

distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip

the error caused by this version

classpath("com.android.tools.build:gradle:4.2.2")

allprojects { repositories { exclusiveContent { // We get React Native's Android binaries exclusively through npm, // from a local Maven repo inside node_modules/react-native/. // (The use of exclusiveContent prevents looking elsewhere like Maven Central // and potentially getting a wrong version.) filter { includeGroup "com.facebook.react" } forRepository { maven { // NOTE: if you are in a monorepo, you may have "$rootDir/../../../node_modules/react-native/android" url "$rootDir/../node_modules/react-native/android" } } } .... } }

distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-all.zip

But my colleagues m1 not occur this error

roshansrashtasoft commented 9 months ago

Any Solution I am stuck?

Screenshot 2023-11-30 at 12 38 56 PM