About HMS API Key
When using HMS (Huawei Mobile Services), some kits need API key. API key can be get from AppGallery Connect -> [Project Setting] -> [General Information]
Duplicate definitions of HMS API key in Android source code
Inside source code, API key is usually being used like following.
[Case 1]
MainActivity.kt
// For example : private val API_KEY = "CgB6e3x9iW/qiE9l9wAUPK0e/bJQe5uIgTlYUD4bPc8gzjriSVxDDzX2fAVjCVdUHkP+tan0Xi0sf4tj7t11TJJe"
private val API_KEY = "Your API key"
// If using map kit
MapsInitializer.setApiKey(API_KEY)
[Case 2]
strings.xml
<!-- For example : <string name="api_key">CgB6e3x9iW/qiE9l9wAUPK0e/bJQe5uIgTlYUD4bPc8gzjriSVxDDzX2fAVjCVdUHkP+tan0Xi0sf4tj7t11TJJe</string> -->
<string name="api_key">Your API key</string>
MainActivity.kt
// If using map kit
MapsInitializer.setApiKey(getString(R.string.api_key))
However, these cases have to put API key into source code directly.
Actually, when using HSM, you have to download agconnect-services.json from AppGallery Connect and put it under app folder.
If you open agconnect-services.json, you can find that API key is already included.
There will be duplicate definitions of API key. It is not good for API key management in source code.
Solution
The best solution is to use the API key in agconnect-services.json directly and do not put another definition into source code. The following is an example.
MainActivity.kt
val api_key = AGConnectServicesConfig.fromContext(applicationContext).getString("client/api_key")
// If using map kit
MapsInitializer.setApiKey(api_key)