facebook / react-native

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

[Android] ReactNative-application.cmake build failed when in the library project. #35996

Closed jsonhu closed 1 year ago

jsonhu commented 1 year ago

Description

when I create CMakeLists.txt in the library project, and the content of the CMakeLists.txt as follows:

cmake_minimum_required(VERSION 3.18.1)

# Define the library name here.
project(appmodules)
# This file includes all the necessary to let you build your application with the New Architecture.
include(${REACT_ANDROID_DIR}/cmake-utils/ReactNative-application.cmake)

target_link_libraries(${CMAKE_PROJECT_NAME})

after that I executed gradle sync and the build failed with the following result:

Could not find a package configuration file provided by "ReactAndroid" with any of the following names:

ReactAndroidConfig.cmake
reactandroid-config.cmake

Add the installation prefix of "ReactAndroid" to CMAKE_PREFIX_PATH or set "ReactAndroid_DIR" to a directory containing one of the above files. If "ReactAndroid" provides a separate development package or SDK, be sure it has been installed.

Version

0.71.1

Output of npx react-native info

System: OS: macOS 13.1 CPU: (10) arm64 Apple M1 Pro Memory: 167.31 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 19.3.0 - /opt/homebrew/bin/node Yarn: 1.22.19 - /opt/homebrew/bin/yarn npm: 9.2.0 - /opt/homebrew/bin/npm Watchman: 2023.01.02.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: Not Found SDKs: iOS SDK: Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1 Android SDK: Not Found IDEs: Android Studio: 2022.1 AI-221.6008.13.2211.9477386 Xcode: 14.2/14C18 - /usr/bin/xcodebuild Languages: Java: 11.0.16.1 - /opt/homebrew/opt/openjdk@11/bin/javac npmPackages: @react-native-community/cli: 10.1.3 => 10.1.3 react: 18.2.0 => 18.2.0 react-native: Not Found react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps to reproduce

  1. create android library module
  2. create jni/CMakeLists.txt under the main folder
  3. copy from react-native/ReactAndroid/cmake-utils/default-app-setup/CMakeList.txt into your library jni/CMakeList.txt
  4. config externalNativeBuild in library build.gradle
  5. gradle sync

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

image image
jsonhu commented 1 year ago

Is it necessary to provide a "ReactNative-library.cmake" in cmake-utils?

LukaASoban commented 1 year ago

@jsonhu did you happen to find out what the solution is here? I seem to have the same problem.

basbase commented 1 year ago

Not sure if this is the same issue, but it may give a hint: When I encountered an error about CMakeList.txt after upgrading to RN 0.71, it turned out I had a dependency that was using an ancient version of react-native-gradle-plugin. When I then added the latest version of react-native-gradle-plugin to the devDependencies of my project the build could succeed.

jsonhu commented 1 year ago

config prefab=true in library build gradle just solve my problem

renanvalentin commented 1 year ago

config prefab=true in library build gradle just solve my problem

@jsonhu where should prefab=true be set? I'm facing the same issue

christophpurrer commented 1 year ago

@jsonhu > I just ran into the same issue.

I assume you are setting up a New Architecture RN app, e.g. by following: https://reactnative.dev/docs/next/the-new-architecture/cxx-cxxturbomodules

You must set

# Use this property to enable support to the new architecture.
# This will allow you to use TurboModules and the Fabric render in
# your application. You should enable this flag either if you want
# to write custom TurboModules/Fabric components OR use libraries that
# are providing them.
newArchEnabled=true

in {pathToYourAwesomeProject}/android/gradle.properties

It is false by default

SemihGk commented 9 months ago

I had the same issue only if I do not enable the new architecture. I was also able to reproduce the problem on the sample app with react native 0.73.1.

Issue was resolved as @jsonhu suggested. Added prefab=true under buildFeatures config block.


    buildFeatures {
        prefab=true // add this
    }

    externalNativeBuild {
        cmake {
            path "$projectDir/src/main/jni/CMakeLists.txt"
        }
    }

    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }