Reading about the best way to save an api key in Android, I came to the conclusion that a good way would be to save it using C++ in the NDK.
To hide a key in the NDK it would be something like this:
#include <string.h>
#include <jni.h>
jstring Java_com_riis_sqlndk_MainActivity_invokeNativeFunction(JNIEnv* env,
jobject javaThis) {
return (*env)->NewStringUTF(env, "pass123");
}
The question is... how do I indicate in an Android project that I want to write in c++?
Here the tutorial in question: see tutorial
Cheers
It may not be completely foolproof but it is a good option, in order to load a module, first go to your SDK Manager since you have to download the 3 packages shown in the image:
Your project must be created to include this support, for this, when you start you can see a button where you select this option,
Include C++ Support
.When creating your project, you can see in the structure of your project a folder called
src/main/cpp
in which a file callednative-lib.cpp
.If you check your MainActivity file, it has an example of how to implement the module in your application:
You can see 2 new things in the code of a MainActivity class, firstly this block, which is used to configure 'native-lib' when starting the application.
and the implementation of a native method implemented by the native-lib library.
To configure the NDK in your project, add the path of the NDK to your file
local.properties
where the path of the SDK is located, with this your project will be ready.I found an excellent tutorial in Spanish to do this: How to run C code from Android with the NDK
Try the SecureKeys library , I haven't tried it yet, but it looks good if it works:
To save keys:
And to retrieve the key:
SecureEnvironment.getString("key_api_googlemaps");
Security in keeping the keys