fbjni icon indicating copy to clipboard operation
fbjni copied to clipboard

Could not find a package configuration file provided by "fbjni"

Open psionic12 opened this issue 3 years ago • 27 comments

Issue description

Created a brand new ndk project, CMake reported an error:

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

    fbjniConfig.cmake
    fbjni-config.cmake

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

Code example

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("fbjnitest")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )
set(build_DIR ${CMAKE_SOURCE_DIR}/build)
find_package(fbjni REQUIRED CONFIG)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
        fbjni::fbjni)

System Info

CMake version: 3.10.2 Gradle Plugin version: 4.1.2 Gradle version: 6.5

psionic12 avatar Feb 26 '21 10:02 psionic12

cc @passy

mweststrate avatar Feb 26 '21 10:02 mweststrate

Having the same issue :/ Anyone? @passy ?

karol-bisztyga avatar Mar 01 '21 14:03 karol-bisztyga

might be related to https://github.com/facebook/flipper/issues/1968

mweststrate avatar Mar 01 '21 15:03 mweststrate

Reporting errors is not always actionable. If a sample project could be shared that'd be great

mweststrate avatar Mar 01 '21 15:03 mweststrate

this should reproduce https://github.com/karol-bisztyga/rnfbjni

karol-bisztyga avatar Mar 01 '21 17:03 karol-bisztyga

I checked with RN 0.64.0-rc.4 and I got the same error.

karol-bisztyga avatar Mar 02 '21 10:03 karol-bisztyga

You may need to enable prefab for Gradle to write the correct config files for you: https://developer.android.com/studio/build/native-dependencies?buildsystem=cmake#build_system_configuration

passy avatar Mar 02 '21 11:03 passy

this should reproduce https://github.com/karol-bisztyga/rnfbjni

Thanks for that! I'm playing around with this right now.

passy avatar Mar 02 '21 12:03 passy

After adding

  buildFeatures {
       prefab true
   }

I got this error: executing external native build for cmake /Users/psionic12/AndroidStudioProjects/fbjnitest/app/src/main/cpp/CMakeLists.txt

I'm pretty sure the cmake's location is right, and before adding the prefab true, the cmake works fine.

Don't know if this error is related to this issue, though.

psionic12 avatar Mar 02 '21 12:03 psionic12

So in general everything would work fine here if you did this:

diff --git a/android/app/CMakeLists.txt b/android/app/CMakeLists.txt
index b5c4522..4af6680 100644
--- a/android/app/CMakeLists.txt
+++ b/android/app/CMakeLists.txt
@@ -24,7 +24,7 @@ set(PACKAGE_NAME "my_jni_module")
     #"fbjni_DIR" to a directory containing one of the above files.  If "fbjni"
     #provides a separate development package or SDK, be sure it has been
     #installed.
-#find_package(fbjni REQUIRED CONFIG)
+find_package(fbjni REQUIRED CONFIG)
 
 add_library( # Sets the name of the library.
         ${PACKAGE_NAME}
@@ -37,7 +37,7 @@ add_library( # Sets the name of the library.
         )
 
 target_link_libraries(${PACKAGE_NAME}
-#        fbjni::fbjni
+        fbjni::fbjni
         android
         log
 )
@@ -46,4 +46,4 @@ target_link_libraries(${PACKAGE_NAME}
 #                      android
 #                      log
 #                      fbjni::fbjni
-#)
\ No newline at end of file
+#)
diff --git a/android/app/build.gradle b/android/app/build.gradle
index f96cafd..1059147 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -154,6 +154,12 @@ android {
         targetSdkVersion rootProject.ext.targetSdkVersion
         versionCode 1
         versionName "1.0"
+
+        externalNativeBuild {
+            cmake {
+                arguments '-DANDROID_STL=c++_shared'
+            }
+        }
     }
     splits {
         abi {
@@ -190,6 +196,15 @@ android {
         }
     }
 
+    buildFeatures {
+        prefab true
+    }
+
+    packagingOptions {
+        exclude("**/libfbjni.so")
+        exclude("**/libc++_shared.so")
+    }
+
     // applicationVariants are e.g. debug, release
     applicationVariants.all { variant ->
         variant.outputs.each { output ->
@@ -210,7 +225,9 @@ android {
 dependencies {
     implementation fileTree(dir: "libs", include: ["*.jar"])
     //noinspection GradleDynamicVersion
-    implementation "com.facebook.react:react-native:+"  // From node_modules
+    implementation("com.facebook.react:react-native:+") {
+        exclude group:'com.facebook.fbjni'
+    }// From node_modules
     implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
     debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
       exclude group:'com.facebook.fbjni'
diff --git a/android/gradle.properties b/android/gradle.properties
index e75e6d3..81d0e4b 100644
--- a/android/gradle.properties
+++ b/android/gradle.properties
@@ -26,4 +26,4 @@ android.useAndroidX=true
 android.enableJetifier=true
 
 # Version of flipper SDK to use with React Native
-FLIPPER_VERSION=0.54.0
\ No newline at end of file
+FLIPPER_VERSION=0.78.0

However, because there are more dependencies on React Native as part of your npm libraries, you will still pull in fbjni multiple times and end up with duplicate classes.

That's a limitation of the Android build system. You cannot use prefab and non-prefab versions of the same library in one application. If you want to use fbjni >= 0.1.0, you also need to make sure that all other libraries make use of the new build mode.

passy avatar Mar 02 '21 12:03 passy

I got this building(I pushed the commit to my repo) more or less doing the same as you, that does not mean I got this fully working as I don't have a code using fbjni but I'll try to push something like that, thx anyway. I for sure will leave the repo with the commits 'before' and 'after' for anyone having similar problems 🙌

Also please, don't close this issue for now, I'd like to make sure this works and write down some thoughts etc(I know we overcame this particular problem and we could close this and open a new one, but I see you guys don't have a lot of issues here so maybe we can deal with anything on the way in this one?).

karol-bisztyga avatar Mar 02 '21 12:03 karol-bisztyga

Direct link to the commit you referenced: https://github.com/karol-bisztyga/rnfbjni/commit/dfbdb2a4f598536cf4c6f8a73c915a3966d9530a

passy avatar Mar 02 '21 14:03 passy

@passy, I create a pure native project https://github.com/psionic12/fbjni_demo, it has nothing to do with React Native, just trying to use fbjni.

In this case, when using prefab=true, as long as the implementation 'com.facebook.fbjni:fbjni:0.1.0' is added, executing external native build for cmake occurs. It seems that fbjni use some Cmake files which is not in 0.1.0 package.

psionic12 avatar Mar 03 '21 03:03 psionic12

Ok, so it is working with fbjni. Once again thanks for the help! After you mentioned prefab I was basically on a good way to build this, that one I was missing. Here is the working commit. There are, however, two things I want to mention. The former is that I don't think that code is needed(build.gradle):

dependencies {
    implementation 'com.facebook.fbjni:fbjni:0.1.0'
}

This still gives me errors about duplicate classes(I'm referring to the code from the commit I pasted above). The latter is I find the documentation about the installation a little bit incomplete(?) 🤔 maybe not the best word, I mean it could be better - those things regarding building as well as, for instance, the flow of registering a hybrid class and all that stuff to make a JNI connection in fbjni(in jni it's pretty clear).

karol-bisztyga avatar Mar 03 '21 08:03 karol-bisztyga

@psionic12

@passy, I create a pure native project https://github.com/psionic12/fbjni_demo, it has nothing to do with React Native, just trying to use fbjni. In this case, when using prefab=true, as long as the implementation 'com.facebook.fbjni:fbjni:0.1.0' is added, executing external native build for cmake occurs. It seems that fbjni use some Cmake files which is not in 0.1.0 package.

The error message isn't great but by telling you

User is using a static STL but library requires a shared STL

it's saying that you need to target a shared C++ runtime. If you add this to your project, it builds:

diff --git a/app/build.gradle b/app/build.gradle
index cd5ec28..4fa3dac 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -17,6 +17,7 @@ android {
         externalNativeBuild {
             cmake {
                 cppFlags ""
+                arguments '-DANDROID_STL=c++_shared'
             }
         }
     }
@@ -51,4 +52,4 @@ dependencies {
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
     implementation 'com.facebook.fbjni:fbjni:0.1.0'
-}
\ No newline at end of file
+}

passy avatar Mar 03 '21 10:03 passy

@karol-bisztyga

This still gives me errors about duplicate classes(I'm referring to the code from the commit I pasted above).

The latter is I find the documentation about the installation a little bit incomplete(?) 🤔 maybe not the best word, I mean it could be better - those things regarding building as well as, for instance, the flow of registering a hybrid class and all that stuff to make a JNI connection in fbjni(in jni it's pretty clear).

I'm definitely open to suggestions here. Sadly the universe of potential Gradle errors is nearly infinite and depends so much on what you are trying to build. If you just google for "duplicate classes gradle" you'll find an endless stream of cases. We can't really anticipate every one of them here, but if there's a particular advice we can give, I'd love to add that to the docs.

passy avatar Mar 03 '21 10:03 passy

I know you said that there are a lot of options but I'd like to refer to this particular example.

implementation('com.facebook.fbjni:fbjni:0.1.0')

throws

Duplicate class com.facebook.jni.CppException found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.CppSystemErrorException found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.DestructorThread found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.DestructorThread$1 found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.DestructorThread$Destructor found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.DestructorThread$DestructorList found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.DestructorThread$DestructorStack found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.DestructorThread$Terminus found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.HybridClassBase found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.HybridData found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.HybridData$Destructor found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.IteratorHelper found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.MapIteratorHelper found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.NativeRunnable found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.ThreadScopeSupport found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.UnknownCppException found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)
Duplicate class com.facebook.jni.annotations.DoNotStrip found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3)

Go to the documentation to learn how to Fix dependency resolution errors.

exclude group doesn’t help(I’ve tried excluding fbjni in react native implementation and even fbjni inside of fbjni implementation, it's excluded in flipper impl, anyway this is in the repo). Still this line gives those errors. I made it conditional checking if the path exists: android/app/.cxx where there are makefiles etc and when building for the first time it throws those errors and after that, it works(which doesn’t make much sense to me - so it's like this line is kind of needed for cmake to find fbjni but at the same time it produces errors of duplicate classes 🤔 Maybe there's a way to restrict it only to pull the config files into the .cxx dir, idk?). If I comment out this line from build.gradle it throws that it couldn’t find cmake file for fbjni.

Do you think this is related to my config or local env? Could it possibly be related to the fbjni itself?

karol-bisztyga avatar Mar 04 '21 10:03 karol-bisztyga

it turned out that replacing implementation 'com.facebook.react:react-native:+' with

implementation("com.facebook.react:react-native:+") {
	exclude group:'com.facebook.fbjni'
} // From node_modules

in following packages(in node_modules):

  • expo-updates
  • expo-splash-screen
  • expo-permissions
  • react-native-screens
  • react-native-gesture-handler
  • react-native-reanimated
  • @unimodules/react-native-adapter

does the job. But I don't think it's a good way to be honest, because in different app you'd have different packages installed, going through all of them is not quite efficient, right? Also, some things might stop working in certain libraries. At the same time, those packages mentioned above are pretty common for an RN app, aren't they?

karol-bisztyga avatar Mar 04 '21 11:03 karol-bisztyga

I think I got this working by adding

configurations {
	all*.exclude module: 'fbjni-java-only'
}

I'm not sure yet whether this is a perfect approach, but it builds after cleaning as well as afterward. Changes on my repo.

karol-bisztyga avatar Mar 04 '21 12:03 karol-bisztyga

@karol-bisztyga your repo was very helpful in the understanding of how to create C++ native module. Is there a similar repo or pointer on how to integrate JavaReact Native module over JSI. I am don't want to use bridge for Java module methods.

prakashjais99 avatar May 10 '21 13:05 prakashjais99

@prakashjais99 hi, actually I was recently doing some research about JSI native modules and published a repo with step-by-step demo. I got inspired i.a. by this issue and the repo above.

barthap avatar May 19 '21 17:05 barthap

Adding jcenter() under allprojects in build.gradle worked for me

vibinjoby avatar Aug 28 '21 16:08 vibinjoby

Adding jcenter() under allprojects in build.gradle worked for me

This works for me too! But, isn't jcenter supposed to be shutting down by February 2022? What's the root cause of this? Is there a better solution?

iurysza avatar Nov 01 '21 18:11 iurysza

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':app:mergeDebugNativeLibs'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not find com.facebook.fbjni:fbjni-java-only:0.0.3.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/com/facebook/fbjni/fbjni-java-only/0.0.3/fbjni-java-only-0.0.3.pom
       - https://repo.maven.apache.org/maven2/com/facebook/fbjni/fbjni-java-only/0.0.3/fbjni-java-only-0.0.3.pom
       - https://jitpack.io/com/facebook/fbjni/fbjni-java-only/0.0.3/fbjni-java-only-0.0.3.pom
     Required by:
         project :app > org.pytorch:pytorch_android:1.10.0

I'm getting this failure message

My dependencies are as below:

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
    implementation 'com.github.chrisbanes:PhotoView:2.3.0'
    implementation 'org.pytorch:pytorch_android:1.10.0'
    implementation 'org.pytorch:pytorch_android_torchvision:1.10.0'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

nagendar-pm avatar Jan 24 '22 15:01 nagendar-pm

very simple go to build.gradle of project and change enablHermes true -> false.....

MoamberRaza avatar Oct 01 '22 21:10 MoamberRaza

Just extend android/grafle.properties by adding:

android.prefabVersion=2.0.0

audriusscorchsoft avatar Oct 17 '22 11:10 audriusscorchsoft

I fixed it by adding this to my module build.gradle

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                ...
                arguments "-DANDROID_STL=c++_shared"
            }
        }
    }
}

gutenye avatar May 19 '24 06:05 gutenye