From e705471220632c9cab6f2cb810fd2e656bb3a0c6 Mon Sep 17 00:00:00 2001 From: alex-minaiev-frontegg Date: Fri, 23 Aug 2024 10:51:56 +0300 Subject: [PATCH] Fixed `Build Config` retrieving --- .../main/kotlin/com/frontegg/flutter/Utils.kt | 87 ++++++++++++------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/android/src/main/kotlin/com/frontegg/flutter/Utils.kt b/android/src/main/kotlin/com/frontegg/flutter/Utils.kt index e1f507b..cfe15be 100644 --- a/android/src/main/kotlin/com/frontegg/flutter/Utils.kt +++ b/android/src/main/kotlin/com/frontegg/flutter/Utils.kt @@ -14,37 +14,64 @@ interface ActivityProvider { val Context.constants: FronteggConstants get() { - val packageName = this.packageName - val className = "$packageName.BuildConfig" - try { - val buildConfigClass = Class.forName(className) - - // Get the field from BuildConfig class - val baseUrl = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_DOMAIN", "") - val clientId = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_CLIENT_ID", "") - - val applicationId = - safeGetNullableValueFromBuildConfig(buildConfigClass, "FRONTEGG_APPLICATION_ID", "") - - val useAssetsLinks = - safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_USE_ASSETS_LINKS", true) - val useChromeCustomTabs = safeGetValueFromBuildConfig( - buildConfigClass, "FRONTEGG_USE_CHROME_CUSTOM_TABS", true - ) - - return FronteggConstants( - baseUrl = baseUrl, - clientId = clientId, - applicationId = applicationId, - useAssetsLinks = useAssetsLinks, - useChromeCustomTabs = useChromeCustomTabs, - bundleId = this.packageName, - ) - } catch (e: ClassNotFoundException) { - Log.e(TAG, "Class not found: $className") - throw e + val mainActivity = getLaunchActivityName(this) + Log.d(TAG, "packageName: ${packageName}, mainActivity: $mainActivity") + val buildConfigClass = + getBuildConfigClass(mainActivity?.substringBeforeLast('.') ?: this.packageName) + + val baseUrl = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_DOMAIN", "") + val clientId = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_CLIENT_ID", "") + + val applicationId = + safeGetNullableValueFromBuildConfig(buildConfigClass, "FRONTEGG_APPLICATION_ID", "") + + val useAssetsLinks = + safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_USE_ASSETS_LINKS", true) + val useChromeCustomTabs = safeGetValueFromBuildConfig( + buildConfigClass, "FRONTEGG_USE_CHROME_CUSTOM_TABS", true + ) + + return FronteggConstants( + baseUrl = baseUrl, + clientId = clientId, + applicationId = applicationId, + useAssetsLinks = useAssetsLinks, + useChromeCustomTabs = useChromeCustomTabs, + bundleId = this.packageName, + ) + } + +fun getLaunchActivityName(context: Context): String? { + val launcherIntent = context.packageManager.getLaunchIntentForPackage(context.packageName) + val launchActivityInfo = launcherIntent!!.resolveActivityInfo(context.packageManager, 0) + return try { + launchActivityInfo.name + } catch (e: Exception) { + null + } +} + +fun getBuildConfigClass(packageName: String): Class<*> { + if (!packageName.contains('.')) { + throw ClassNotFoundException("Invalid package name: $packageName. Failed to retrieve BuildConfig class.") + } + + val className = "$packageName.BuildConfig" + + return try { + Class.forName(className) + } catch (e: ClassNotFoundException) { + Log.d(TAG, "Class not found: $className, checking parent namespace") + + val parentPackageName = packageName.substringBeforeLast('.') + + if (parentPackageName.isNotEmpty()) { + getBuildConfigClass(parentPackageName) + } else { + throw ClassNotFoundException("Failed to retrieve BuildConfig class for package: $packageName after checking all namespaces.") } } +} fun safeGetNullableValueFromBuildConfig( buildConfigClass: Class<*>, @@ -56,7 +83,7 @@ fun safeGetNullableValueFromBuildConfig( field.get(default) as T } catch (e: Exception) { Log.e( - TAG, "Field '$name' not found in BuildConfig, return default $default" + TAG, "Field '$name' not found in BuildConfig, return null" ) null }