O'Reilly logo

Android NDK Beginner's Guide by Sylvain Ratabouil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – calling C code from Java

The native library mylib that we are going to create will contain one simple native method getMyData() that returns a basic character string. First, let's write the Java code to declare and run this method.

  1. Open MyActivity.java. Inside main class, declare the native method with the native keyword and no method body:
    public class MyActivity extends Activity {
        public native String getMyData();
    ...
  2. Then, load the native library that contains this method within a static initialization block. This block will be called before Activity instance gets initialized:
    ...
        static {
            System.loadLibrary("mylib");
        }
    ...
  3. Finally, when Activity instance is created, call the native method and update the screen content with ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required