3

I have developed a small android application using react-native. For what it was developed, it works fine. Now to accommodate a small requirement, we need to call a library written in C. I am not sure if there will be a way to call the C code from JavaScript! But I still wanted to ask you all.

Is there a way to do this from react-native? Like I want to call some functions in the C library. If this possible, could you please suggest how? How could I start to test a very basic setup?

2 Answers 2

0

You are going to have to write some native code, search for how to call a C lib on Android, then look for react native way to link a native code with a js call.

Native Module link https://facebook.github.io/react-native/docs/native-modules-android.html

StackOverflow question on how to run c/cpp code on android how to use c/cpp library/functions in android

Sign up to request clarification or add additional context in comments.

12 Comments

I am a little confused. What is the first link that you gave? It shows some Java code? Is this relevant?
React native lets you write your own native code, and then call it on your js file. You are going to have to write Java to call the C library, and create a native module based on that java, and use the first link to integrate with your js file.
This is more advanced react native code, you first gonna need to understand how React Native works, and its code flow
So you mean, react-native communicates with Java which communicates with C?
yeah, behind every js component you call, there is already a java code, react let's write you write your own native code if they don't have already implemented.
|
0

Basically it includes 3 parts.

JavaScript to Java or Kotlin

check out React Native's guide to Android Native Module.

Java or Kotlin to C

Calling C functions from Java is enabled with a mechanism called Java Native Interface (JNI) . check out how to add C and C++ code here.

see more here https://thebhwgroup.com/blog/react-native-jni

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.