Time for action – calling C code from Java

Let's create our first native method and call it from the Java side:

  1. Open src/com/packtpub/store/Store.java and declare one native method to query the Store. This method returns int with the number of entries in it. There is no need to define a method body:
    package com.packtpub.store;
    
    public class Store {
        static {
            System.loadLibrary("com_packtpub_store_Store");
        }
    
        public native int getCount();
    }
  2. Open src/com/packtpub/store/StoreActivity.java and initialize the store. Use its getCount() method value to initialize the application title:
    public class StoreActivity extends Activity {
        ...
        public static class PlaceholderFragment extends Fragment {
            private Store mStore = new Store(); ... public PlaceholderFragment() ...

Get Android NDK Beginner's Guide - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.