Skip to content

Commit

Permalink
Show gmpApiKey in MainActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Sep 5, 2024
1 parent a118a40 commit c383224
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,47 @@

package com.google.secrets_gradle_plugin.sample

import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.secrets_plugin.sample.BuildConfig

/**
* Dummy activity. See build.gradle.kts for sample usage of the plugin.
*/
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val buildConfigApiKey = BuildConfig.gmpApiKey
val manifestApiKey = getMetaValueFromManifest()

val textView = TextView(this).apply {
@Suppress("SetTextI18n")
text = """
GMP API Key in BuildConfig: $buildConfigApiKey
GMP API Key in AndroidManifest: $manifestApiKey
""".trimIndent()
}
val linearLayout = LinearLayout(this).apply {
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
addView(textView)
}
setContentView(linearLayout)
}

private fun getMetaValueFromManifest(
key: String = "com.google.android.geo.API_KEY"
): String? {
return runCatching {
val appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
appInfo.metaData.getString(key)
}.getOrNull()
}
}

0 comments on commit c383224

Please sign in to comment.