[JAVA] Use native code on Android

Last time I explained up to the setting of OpenGL ES, this time I thought I would proceed, but I found that C / C ++ code can be used on Android So, take a detour.

The OpenGL code is overwhelmingly C / C ++, so I expected that the asset could be used, but the usage was more difficult than I had imagined. I haven't figured out everything yet, but here's a summary of what I understand.

I don't have much information on the latest Android Studio, so I'm trying to find something wrong, so I'd appreciate it if you could point out any mistakes.


Prepare Android Studio 2.2.

In Android Studio 2.2, the check box "Do you want to use C / C ++?" Is displayed when creating a new project, so turn it ON first.


Check the directory structure

Check the configuration of the new project. Change the project structure on the left from "Android" to "Project" so that you can see all the files.

It is OK if you have the following files.

Try to build

Once you build it, you should see a TextView on the screen. You can see that the code in cpp / native-lib.c is called from MainActivity.java.

public class MainActivity extends AppCompatActivity {

    // native-lib.c seems to be a library at compile, and I'm loading it here.
    //Is it something like import?
    static {

    protected void onCreate(Bundle savedInstanceState) {

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);

     * native-lib.It seems to define the function implemented in c.
    public native String stringFromJNI();

Check native-lib.cpp

I think the following code is written by default. Looking at this code, It seems that the package, project name, and function name are connected with an underscore. Maybe this is the format.

It seems that it is defined by the description. The return value is also in the form of "jstring". I think this was an Android NDK or a JNI (Java native interface) type.

#include <jni.h>
#include <string>

extern "C" 
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());

Let's define a test function.

First, modify as follows so that extern "C" is applied to the whole.

extern "C" {

//Put the function here.


Write a test function as appropriate. The arguments are standard, so enter the same ones.

extern "C" {

	//The description of stringFromJNI is omitted.

    Java_quad_jp_opengl_MainActivity_sayHello(JNIEnv *env, jobject){
        std::cout << "Hello World" << std::endl;

Of course, the function is also defined for MainActivity.

public native void sayHello();

When I built it, it ended without any errors, I didn't know where to see the output of cout ... orz

For the time being, I know how to execute the process on the C side, so let's modify the previous code. I will add it here later.

Reference information

Android NDK (official page has detailed setting method)

Some pages have been translated, but the translation is not good enough and it is a little confusing. Adding C / C ++ code to your project

Recommended Posts

Use native code on Android
Use serial communication on Android
Bridge commentary on React Native Android
Use perltidy on CentOS 8
Use Flutter on Ubuntu
[Android] Notes on xml
Use mod_auth_cas on CentOS 8
Use bat on Centos.
Use mkdir on ubuntu
Use Corretto 11 on Heroku
Use cpplapack on ubuntu
[Android Studio] I want to use Maven library on Android
Customize list view on Android
Multipart transmission library on Android
Run VS Code on Docker
ROS app development on Android
Item 66: Use native methods judiciously
Use Java included with Android Studio to build React Native
Use Swift Package on Playground
Use devise on multiple models
Use PG Backups on Heroku
How to use PlantUML with Visual Studio Code (created on October 30, 2020)
Use PlantUML with Visual Studio Code
Read and generate QR code [Android]
Event handling with RxBus on Android
How to "hollow" View on Android
[Android] Get the date on Monday
Use Docker Compose on Windows 10 Home
Use partial templates (write maintainable code)
How to use Ruby on Rails
How to use Bio-Formats on Ubuntu 20.04
Watson Assistant (formerly Conversation) on Android
Use Docker on your M1 Mac
Notes on calling Installer on Android App
Easy to use Cloud Firestore (Android)
Save ArrayList using GSON on Android
Preparing to use electron-react-boilerplate on Ubuntu 20.4
Use the iostat command on CentOS 8
Use completion in Eclipse on mac
Notes on Android (java) thread processing
[Android] Receive intent on Broadcast Reciever
[Android] Convert Android Java code to Kotlin
Use Docker CE (official) on CentOS 8
Implement ripple representation on images on Android
Speed up location acquisition on Android
What wasn't fair use in the diversion of Java APIs on Android