From c383224c9938781e5585a33768e690b863036cbf Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 5 Sep 2024 19:22:38 +0800 Subject: [PATCH] Show gmpApiKey in MainActivity --- .../sample/MainActivity.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/sample-app/src/main/java/com/google/secrets_gradle_plugin/sample/MainActivity.kt b/sample-app/src/main/java/com/google/secrets_gradle_plugin/sample/MainActivity.kt index 77e1165..97cd0c7 100644 --- a/sample-app/src/main/java/com/google/secrets_gradle_plugin/sample/MainActivity.kt +++ b/sample-app/src/main/java/com/google/secrets_gradle_plugin/sample/MainActivity.kt @@ -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() + } } \ No newline at end of file