Secure your API Using NDK
Secure your API Using NDK
NDK means Native Development Kit this is kind of kit you can use C and C++ code using this Development kit
so let’s start our steps how can you use NDK and secure your API from other person so let’s start
Step-1
Click on SDK Manager and open SDK Manager From Screen shot below
Step-2
then after make sure that you have 2 SDK Tools which are NDK(Side by side)& CMake if you haven’t that then install that SDK Tools
Step-3
Right Click on Main and create new directory Named jni (JAVA NATIVE INTERFACE) we will store our c files in this folder so we can use that
Step-4
Create Android.mk file in jni folder and paste this code into Android.mk
==>
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := keys
LOCAL_SRC_FILES := keys.c
include $(BUILD_SHARED_LIBRARY)
≤==
Step-5
Create Application.mk file in same directory and paste this code
==>
APP_ABI := all
≤==
Step-6
Create one more file name keys.c file and paste this code into your keys.c file
so I create one method here which is Java_com_example_secureapi_ActivityName_MethodName
so basically this is package name of your application com.example.secureapi and (_)underscore replace with (.)dot and then after ActivityName if you want your key in MainActivity then write Java_com_example_secureapi_MainActivity_MethodName and then after Method Name you can get your api key using MethodName if my Method Name is GetApi then my Function name will be Java_com_example_secureapi_MainActivity_GetApi
==>
#include <jni.h>
JNIEXPORT jstring JNICALL
Java_com_example_secureapi_ActivityName_MethodName(JNIEnv *env, jobject instance) {
return (*env)-> NewStringUTF(env, “TmF0aXZlNWVjcmV0UEBzc3cwcmQx”);
}
≤==
after completing this steps your file structure will be
ok So now Done from NDK Side
Step-7
Last step Go to buld.gradle(Module.secureapi.app)
then add this code
==>
ndkVersion ‘23.0.7599858’
externalNativeBuild {
ndkBuild {
path ‘src/main/jni/Android.mk’//path of Android.mk file
}
}
≤==
check ndk version from this path : : C:\Users\USER\AppData\Local\Android\Sdk\ndk
Step-8
So Finally We done our code so jump on Android Code
==> By Using System.loadLibrary(“keys”); you can load your c file in android then after using public native String GetApi(); this method return one String API key you can use it as you want to use
No comments