title: Securing API key using NDK
body: <p>I am trying to store api keys using NDK but i tried somany methods always somany error 
  I will share my code please any body help me..</p>
  
  <p>I will share my steps i followed ..</p>
  
  <p>1 Create a folder “jni” under src/main</p>
  
  <p>2  Create and add “Android.mk” file under “jni” folder with following content:</p>
  
  <pre><code>     LOCAL_PATH := $(call my-dir)
  
      include $(CLEAR_VARS)
  
      LOCAL_MODULE    := keys
      LOCAL_SRC_FILES := keys.c
  
      include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <ol start="3">
  <li><p>Create and add “Application.mk” file under “jni” folder with the following content:</p>
  
  <pre><code>APP_ABI := all
  </code></pre></li>
  <li><p>Create the C/C++ file “keys.c” and add it under “jni” folder. Add the following content to it:</p>
  
  <pre><code> # include &lt; jni.h &gt;
  
  JNIEXPORT jstring JNICALL
  Java_com_mytest_aes_MainActivity_getNativeKey1(JNIEnv *env, jobject instance) {
  
      return (*env)-&gt;NewStringUTF(env, "haii");
  }
  </code></pre></li>
  <li><p>In the Activity where you want to access the keys (in our case MainActivity), create a static block and load the library “keys” like:</p>
  
  <pre><code> static
   {
       System.loadLibrary("keys");
   }
  </code></pre></li>
  <li><p>Declare two member function of type <code>native</code> to access the keys from the C/C++ file. Since we have stored 2 keys, we will declare 2 functions:</p>
  
  <pre><code>     public native String getNativeKey1();
  </code></pre></li>
  <li><p>For demo, access the keys in the code like:</p>
  
  <pre><code>    String key1 = new String(Base64.decode(getNativeKey1(),Base64.DEFAULT));
  
  
      ((TextView)findViewById(R.id.key)).setText("Key1--&gt;"+key1);
  </code></pre></li>
  <li><p>Now, our C/C++ native files and Java code are ready. But to compile or make the native build using NDK, we need to add entry into the gradle file:</p>
  
  <pre><code>android {
      .....
      buildTypes {
          .....
      }
      externalNativeBuild {
          ndkBuild {
              path 'src/main/jni/Android.mk'
          }
      }
  }
  </code></pre>
  
  <p>We need to provide the path for our “Android.mk” file.</p></li>
  <li><p>Now, sync and build the project. Make sure, you have pointed the NDK path correctly in your module settings.</p></li>
  </ol>