fix(android): allow BuildConfig lookup when applicationId != namespace#70
Draft
airowe wants to merge 2 commits into
Draft
fix(android): allow BuildConfig lookup when applicationId != namespace#70airowe wants to merge 2 commits into
airowe wants to merge 2 commits into
Conversation
`Context.fronteggConstants` resolves the host app's generated BuildConfig
via `Class.forName("${context.packageName}.BuildConfig")`. On apps where
the Gradle `applicationId` (== `context.packageName` at runtime) differs
from the AGP `namespace` (the Java package the generated BuildConfig is
emitted into), this throws `ClassNotFoundException` at module init time.
This is a common pattern for white-label / multi-flavor RN apps that
share a Java source tree but ship under different applicationIds.
Resolution is now a small, ordered fallback:
1. Existing behaviour — try `<applicationId>.BuildConfig` first.
2. If that misses, look up a `<meta-data>` entry on `<application>`
named `com.frontegg.reactnative.BUILD_CONFIG_PACKAGE` and try
`<that value>.BuildConfig`.
If both miss, the existing `ClassNotFoundException` is rethrown — but
the error log now points consumers at the manifest override they need.
For apps where applicationId == namespace (the common case) this is a
no-op: the first candidate matches and the meta-data lookup never runs.
Consumer wiring for the white-label case:
<application ...>
<meta-data
android:name="com.frontegg.reactnative.BUILD_CONFIG_PACKAGE"
android:value="com.example.shared" />
</application>
Extracts the pure candidate-list logic from `resolveBuildConfigClass` into `buildConfigClassCandidates(primary, fallback)` so it can be exercised without an Android `Context`, then adds a JUnit 4 test suite covering: - single candidate when no fallback is configured - single candidate when fallback is present but blank/whitespace - ordered two-candidate list (applicationId first, fallback second) - deduplication when applicationId equals fallback `resolveBuildConfigClass` keeps its existing signature and continues to read the manifest meta-data via `Context.packageManager`; only the candidate generation moves into the testable helper. Also adds `testImplementation 'junit:junit:4.13.2'` — first test dependency in this module, so `android/src/test/` source set is now active. Runs via the standard `./gradlew test` task.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a single-step fallback so
Context.fronteggConstantscan locate the host app's generatedBuildConfigwhen itsapplicationIddiffers from its AGPnamespace. Existing apps see no behaviour change.Problem
Context.getPackageName()returns the GradleapplicationIdat runtime, but AGP emits the generatedBuildConfigunder the module'snamespace. For apps where these differ — common in white-label / multi-flavor RN apps that share a Java source tree but ship under different applicationIds — the SDK throwsClassNotFoundExceptionat module init time before any JS code runs.In our case (Healthie + 8 white-label flavors):
applicationId = com.healthie.app.<flavor>,namespace = com.main, BuildConfig lives atcom.main.BuildConfig, lookup fails.Fix
resolveBuildConfigClassnow tries the candidates in order:<applicationId>.BuildConfig— the existing behaviour. For apps whereapplicationId == namespacethis matches immediately and step 2 never runs.<meta-data>entry on<application>namedcom.frontegg.reactnative.BUILD_CONFIG_PACKAGE, suffixed with.BuildConfig.If both miss, the original
ClassNotFoundExceptionis rethrown — but the error log now points consumers at the manifest override they need.The pure candidate-generation logic is extracted into
buildConfigClassCandidates(primary, fallback)so it can be unit-tested without an AndroidContext.Consumer wiring (only for the white-label case)
Tests
This PR introduces the first
android/src/test/source set in the module, withtestImplementation 'junit:junit:4.13.2'. NewUtilsTest.ktcovers four scenarios:Runs via
./gradlew :react-native_frontegg:test.Manual test plan
FronteggRNModulewithout error; no behaviour change vs. master.applicationId = com.foo.appandnamespace = com.foo.shared+<meta-data android:name="com.frontegg.reactnative.BUILD_CONFIG_PACKAGE" android:value="com.foo.shared" />, the module initialises successfully (before this PR:ClassNotFoundExceptionat init).Alternatives considered
FronteggRN.setBuildConfigClass(BuildConfig::class.java)fromMainApplication.onCreate. More explicit, but adds a required call site for every host app and an ordering constraint (must run before the JS engine attaches the module).The manifest meta-data approach was the least invasive: zero changes for apps where
applicationId == namespace, a single declarative line for everyone else, and no API surface on the JS or Kotlin side.Related
Part of a small batch we found while integrating the SDK — JVM 17 target and
currentActivitydeprecation are separate PRs.