API Keys
Using local.properties
We can use local.properties to store API keys and other sensitive information. This file is not checked into version control, so it is safe to store sensitive information here.
local.properties
l̥ build.gradle
l̥ MainActivity.java
Using ProGuard
Another methods is to use obfuscation. This is a method of hiding the API key in the code. This is not a very secure method, but it is better than nothing.
It is done by using ProGuard. ProGuard is a tool that is used to obfuscate code. It is used to make the code smaller and faster. It is also used to make the code more secure.
In order to do this, set isMinifyEnabled to true in the build.gradle file.
build.gradle
Using NDK
Step 1: Set Project view in the Android Studio
Step 2: go to app -> src -> main
Step 3: Right click on main, and select "Add C++ to module" from context menu
This will generate two files required
CMakeLists.txt
native-lib.cpp (other name can be used in place of 'native-lib')
And this will also add the following code in build.gradle
Step 4: Add this code in MainActivity to dynamically load the C++ library into our application
Step 5: Now write this line of code
At this point, getApiKey() will return null, We need to implement it in native-lib.cpp.
IDE will show an error on this line, because we have not implemented it yet.
Right click on the error and select "Create JNI function for getApiKey()"
Step 6: Now the code will look like this
Step 7: Now we need to return the API key from this function. So replace the comment with the following code
Step 8: Now we can use this API key in our MainActivity.java file