Android Studio Flamingo: Skip production credentials toggle behavior for unit test configurations
0 reputation · 28 Dec 2020, 22:37 UTC
0 reputation · 28 Dec 2020, 22:37 UTC
The goal is to prevent production API keys from being exposed during unit test execution when the “Skip production credentials” run‑configuration option is enabled in Android Studio Flamingo (2022.2.1).
Currently the option only replaces values in the merged manifest for the debug build variant; unit test configurations that run via the JUnit test runner continue to use the original production values unless a separate testBuildType is defined. This leaves an unresolved decision about whether the IDE should automatically apply the same placeholder substitution to test JVMs or require manual configuration.
Should the “Skip production credentials” toggle automatically affect unit test run configurations? If not, what is the recommended way to achieve credential substitution for tests without defining a custom testBuildType? Are there any drawbacks or side‑effects of extending the toggle to cover test configurations?34775 reputation · 29 Dec 2020, 03:04 UTC
debug). Because unit tests execute on a local JVM via the JUnit runner rather than on a device using the merged manifest, they do not interact with the manifest-substitution logic applied by the IDE.
The toggle primarily targets the build process for the APK's manifest. Unit tests, however, often pull values directly from compiled classes or resource files. If your production keys are injected via BuildConfigField or hardcoded in the build variant, the test JVM will see the original values defined in your build script unless explicitly overridden for the test task.
If you want to avoid creating a new testBuildType, the most reliable way to inject test-specific credentials is through Gradle properties or environment variables. This allows you to keep your production configuration clean while swapping values during execution.
build.gradle to check for a specific property during the test task:
tasks.withType(Test).configureEach {
val testKey = project.findProperty("testApiKey") ?: "placeholder_key"
systemProperty("API_KEY", testKey)
}
val apiKey = System.getProperty("API_KEY")
./gradlew testUnitTest -PtestApiKey=my_test_key_123
Extending the IDE-level toggle to cover test JVMs presents several risks:
To provide a more specific code snippet, are your credentials currently managed via manifestPlaceholders, BuildConfigField, or loaded from a local properties file?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.