Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert existing C++ (.h and .cpp) files to java for Android [closed]

Tags:

java

c++

android

I am developing for Android under Eclipse. I have some c++ (.h and .cpp) files that I wish to use in my Android application. I have read Programmming in C/C++ with the Java Native Interface and now I know how to make .so file and import it to Android. But the main problem is that:

Is there any tool that converts c++ (.h and .cpp) file to java style files which I can compile and create .so library ? I have heard about JNI - javah and javac can that tools help me and if they can so HOW ?


Edited:

javah is a useful tool that creates a C-style header file from a given class. The resulting header file describes the class file in C terms. Although it is possible to manually create the header file, this is almost always a bad idea. javah knows exactly how Java types and objects map into C types. For example, an int in Java maps to a long in C, and a long in Java maps to a 64-bit value, _int64, in native code. How to use this javah?

Anybody have expiarance of how to use functions that are implemented in .so library in android application ? If anybody have code examples or useful articles links please give.

Thanks in advance !

like image 631
Viktor Apoyan Avatar asked May 10 '11 11:05

Viktor Apoyan


People also ask

How do I open a CPP file on Android?

💻 Can I open & view files CPP on Linux, Mac OS, or Android? Yes, you can use the free GroupDocs Viewer on any operating system that has a web browser. Our CPP viewer works online and does not require any software installation.

Can C++ compile to Android?

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework.

Can I make Android app using C++?

C++ can be used for Android App Development using the Android Native Development Kit(NDK). However, an app cannot be created totally using C++ and the NDK is used to implement parts of the app in C++ native code. This helps in using C++ code libraries for the app as required.

How does JNI work on Android?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).


2 Answers

You don't "convert C++ file to java style". You need to create a JNI wrapper around your existing C++ code. This JNI wrapper is actually C++ code that can be called by Java.

By wrapper I mean that you shouldn't have to modify your existing C++ code base. This wrapper, or better said this binding should generally be very thin. The wrapper code is only meant to expose existing functionalities, not to implement them. It is better to leave the implementation in the (portable) C++ code base.

If the code base isn't too large, then I recommend that you write this wrapper by hand, as explained in The JavaTM Native Interface Programmer's Guide and Specification

Now, if you are trying to bind a large library, it may be problematic. So, in regard to tools, I haven't used that, but have a look at SWIG, and the relevant SWIG Java documentation.

According to the homepage description, it's what you're asking for:

SWIG is typically used to parse C/C++ interfaces and generate the 'glue code' required for [Java, Python, PHP, ...] to call into the C/C++ code.

javah can be useful in certain cases, but it's not what you ask for. It extracts JNI boiler plate code out of native declarations found in Java classes. Regarding javac, it's the Java compiler, that's irrelevant.

like image 117
olivierg Avatar answered Nov 14 '22 22:11

olivierg


When developing for Android you are not limited to using the Java language. Why not use C++ directly? See e.g. the Android NDK.

like image 40
ildjarn Avatar answered Nov 14 '22 21:11

ildjarn