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)
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
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
C # and Java Overrides Story
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
[Java] Change language and locale to English in JVM options
Use Java included with Android Studio to build React Native
[Android, Java] Convenient method to calculate the difference in days
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
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
Automatically insert `@SuppressWarnings` 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?
Reintroducing Java 8 available from Android Studio 2.4
[Android] Convert Android Java code to Kotlin
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
Determining if a custom keyboard is enabled in Android Studio (Java)
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
Difference between final and Immutable in Java
Easy to make Slack Bot in Java
Java reference to understand in the figure
java core: HotSpot compiler and C heap
Try to implement n-ary addition in Java
Link Java and C ++ code with SWIG
Convert all Android apps (Java) to Kotlin
Calling java from C ++ on Android NDK
How to do base conversion in Java