Java to C and C to Java in Android Studio

It's easy to call C from Java when using NDK in Android Studio, but sometimes I want to call Java from C, so a memorandum (I'm using Android Studio 3.4.1) ■ Setp1: Create a project

2019-05-29 (1).png Create a project with "File" "New" "New Projekut" of Android Studio. 2019-05-29 (3).png This time I want to call C from Java, so select "Native C ++" and click "Next" to go to the next screen 2019-05-29 (4).png Name is appropriate this time "test01" Specify the project directory to be created this time with Save location Language is created in "Java", and when the settings are complete, click "Next" to display the next screen. 2019-05-29 (5).png Here, simply select "Finish". (On this screen, you can set to use C ++ 11 or C ++ 14, but you can set it later, so ignore it) 2019-05-29 (6).png When such a screen appears, I was able to create a project for the time being. ■ Setp2: Java to C

When the project is created, it is the source to call C from Java. If you execute it, "stringFromJNI ()" is called from "MainActivity", so "Java_l_toox_test01_MainActivity_stringFromJNI" in native-lib.cpp is called. (Let's study the details separately) ■ Step 3: From C to Java

Add the function "testFunc" called from C to [MainActivity.Java].

package l.toox.test01;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {

    protected void onCreate(Bundle savedInstanceState) {

        // Example of a call to a native method
        TextView tv = findViewById(;

     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
    public native String stringFromJNI();

    public void testFunc(){

Next, the part that reads Java from C. Modify [Native-lib.cpp] as follows.


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

extern "C" JNIEXPORT jstring JNICALL
        JNIEnv *env,
        jobject in_thiz) {

    jobject my_class = env->NewGlobalRef(in_thiz);
    jclass clazz = env->GetObjectClass(my_class);
    jmethodID mobj = env->GetMethodID( clazz, "testFunc", "()V" );
    env->CallVoidMethod( my_class, mobj );
    env->DeleteLocalRef( clazz );

    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());

(Note that the argument is jobject in_thiz.)

If you run it with this, you are calling Java from C. 2019-05-29 (10).png Breaks can be seen during debug execution.

Recommended Posts

Java to C and C to Java in Android Studio
Refer to C ++ in the Android Studio module (Java / kotlin)
Java Direction in C ++ Design and Evolution
How to use ExpandableListView in Android Studio
Source to display character array with numberPicker in Android studio (Java)
[Android] Convert Map to JSON using GSON in Kotlin and Java
Notes in Android studio
3 ways to import the library in Android Studio
Differences in writing Java, C # and Javascript classes
Represents "next day" and "previous day" in Java / Android
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
[Android / Java] Screen transition and return processing in fragments
[Android Studio] How to change TextView to any font [Java]
Organize builds in C ++ / Java and Win / Linux combinations
Impressions and doubts about using java for the first time in Android Studio
[Android Studio] [Java] How to fix the screen vertically
Asynchronous processing and Web API integration in Android Studio
Java classes and instances to understand in the figure
Java in Visual Studio Code
How to convert A to a and a to A using AND and OR in Java
Reproduce Java enum in C #
Gzip-compress byte array in Java and output to file
C # and Java Overrides Story
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
[Java] Change language and locale to English in JVM options
Use Java included with Android Studio to build React Native
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
You are currently using Java 6. Solution in Android Studio Gradle
Reasons to use Servlet and JSP separately in Java development
How to develop and register a Sota app in Java
Differences in how to handle strings between Java and Perl
Try implementing Android Hilt in Java
Read Java properties file in C #
Multithreaded to fit in [Java] template
Encoding and Decoding example in Java
Java, JavaScript, C # (difference in assignment)
How to learn JAVA in 7 days
CGI in C and Dart: Introduction (1)
Automatically insert `@SuppressWarnings` in Android Studio
OkHttp3 (GET, POST) in Android Studio
[Android Studio] [Java] Learning memos & links
Log output to file in Java
StringBuffer and StringBuilder Class in Java
Understanding equals and hashCode in Java
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
Reintroducing Java 8 available from Android Studio 2.4
Encrypt with Java and decrypt with C #
Hello world in Java and Gradle
How to concatenate strings in java
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
I tried to create a simple map app in Android Studio
Introduction to Android App Development 1 Installing JDK and Android Studio for mac
How to encrypt and decrypt with RSA public key in Java
Determining if a custom keyboard is enabled in Android Studio (Java)
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale