Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
psionic12 opened this issue Feb 26, 2021 · 27 comments
Open

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

psionic12 opened this issue Feb 26, 2021 · 27 comments

Comments

@psionic12
Copy link

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

@mweststrate
Copy link

cc @passy

@karol-bisztyga
Copy link

karol-bisztyga commented Mar 1, 2021

Having the same issue :/ Anyone? @passy ?

@mweststrate
Copy link

might be related to facebook/flipper#1968

@mweststrate
Copy link

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

@karol-bisztyga
Copy link

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

@karol-bisztyga
Copy link

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

@passy
Copy link
Member

passy commented Mar 2, 2021

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
Copy link
Member

passy commented Mar 2, 2021

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

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

@psionic12
Copy link
Author

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.

@passy
Copy link
Member

passy commented Mar 2, 2021

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.

@karol-bisztyga
Copy link

karol-bisztyga commented Mar 2, 2021

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?).

@passy
Copy link
Member

passy commented Mar 2, 2021

Direct link to the commit you referenced: karol-bisztyga/rnfbjni@dfbdb2a

@psionic12
Copy link
Author

@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.

@karol-bisztyga
Copy link

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).

@passy
Copy link
Member

passy commented Mar 3, 2021

@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
Copy link
Member

passy commented Mar 3, 2021

@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.

@karol-bisztyga
Copy link

karol-bisztyga commented Mar 4, 2021

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
Copy link

karol-bisztyga commented Mar 4, 2021

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
Copy link

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.

@prakashjais99
Copy link

@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.

@barthap
Copy link

barthap commented May 19, 2021

@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.

@vibinjoby
Copy link

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

@iurysza
Copy link

iurysza commented Nov 1, 2021

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?

@nagendar-pm
Copy link

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'
}

@MoamberRaza
Copy link

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

@audriusscorchsoft
Copy link

audriusscorchsoft commented Oct 17, 2022

Just extend android/grafle.properties by adding:

android.prefabVersion=2.0.0

@gutenye
Copy link

gutenye commented May 19, 2024

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests