Open psionic12 opened 3 years ago
cc @passy
Having the same issue :/ Anyone? @passy ?
might be related to https://github.com/facebook/flipper/issues/1968
Reporting errors is not always actionable. If a sample project could be shared that'd be great
this should reproduce https://github.com/karol-bisztyga/rnfbjni
I checked with RN 0.64.0-rc.4 and I got the same error.
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
this should reproduce https://github.com/karol-bisztyga/rnfbjni
Thanks for that! I'm playing around with this right now.
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.
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.
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?).
Direct link to the commit you referenced: https://github.com/karol-bisztyga/rnfbjni/commit/dfbdb2a4f598536cf4c6f8a73c915a3966d9530a
@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.
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).
@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
+}
@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 infbjni
(injni
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.
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?
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
):
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?
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 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 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.
Adding jcenter() under allprojects in build.gradle worked for me
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?
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'
}
very simple go to build.gradle of project and change enablHermes true -> false.....
Just extend android/grafle.properties by adding:
android.prefabVersion=2.0.0
I fixed it by adding this to my module build.gradle
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
...
arguments "-DANDROID_STL=c++_shared"
}
}
}
}
Issue description
Created a brand new ndk project, CMake reported an error:
Code example
System Info
CMake version: 3.10.2 Gradle Plugin version: 4.1.2 Gradle version: 6.5