I also faced the same problem. I couldn't find any proper solution for this. After lot of searching I found a solution. So I am giving a complete guide for this issue.
Many thanks to Prasenjit Banerjee for helping me.
Complete guide for use API Keys and avoid Leaked GCP API Keys security issue in Google Play Console :
First of all you need to follow API security best practices for add restrictions and securely using API keys.
Then follow Set Up an Android Studio Project for add API keys to local.properties and use them in AndroidManifest.xml & Main program.
Finally access API keys As a variable in your AndroidManifest.xml file :
<application>
.
.
.
.
<activity>
.
.
.
.
</activity>
<meta-data
android:name = "keyValue"
android:value = "${KEY}"/>
</application>
- Access API keys in
MainActivity.kt and type in the below code to get the KEY value from the meta-data in AndroidManifest.xml (Find solution from this article in GeeksforGeeks ) :
Note: Don't use BuildConfig class to get these API keys because this class expose those properties as variables.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//get the KEY value from the meta-data in AndroidManifest
val ai: ApplicationInfo = applicationContext.packageManager
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
val value = ai.metaData["keyValue"]
val key = value.toString()
//for testing only
Toast.makeText(applicationContext, key, Toast.LENGTH_LONG).show()
// use this key to initialize places sdk
Places.initialize(applicationContext, key)
}
}