Android中BuildConfig.BUILD_TYPE始终为Release,调试时获取错误URL的问题
Hey there, let's break down why your Debug build is still showing Release for BuildConfig.BUILD_TYPE—this is a super common gotcha with Android Gradle builds, and there are a few likely culprits to check:
1. Accidental Build Type Override in Gradle
Double-check your app-level build.gradle (or build.gradle.kts) file. It's easy to accidentally hardcode the build type or misconfigure variants without noticing. For example:
If you have something like this in your
buildTypesblock, it would force the build type value regardless of your selected variant:buildTypes { release { // ... your release config } debug { // Oops! This manual override overwrites the auto-generated value buildConfigField "String", "BUILD_TYPE", "\"release\"" } }
Fix: Remove any manual BUILD_TYPE field definitions—Android generates this value automatically. Let the Gradle plugin handle it natively.
2. Stale Android Studio Build Cache
Sometimes Android Studio's build cache holds onto old BuildConfig files even after you switch variants. This happens especially if you've been toggling between builds quickly.
Fix:
- Go to
File > Invalidate Caches... > Invalidate and Restartto clear the cache fully. - Alternatively, run a clean build from the terminal to ensure fresh outputs:
./gradlew clean assembleDebug
3. Misselected Build Variant
It's possible you think you're running the Debug variant, but Android Studio is actually targeting the Release variant under the hood.
Fix:
- Check the variant selector in the top toolbar (the dropdown with options like
debug,release, etc.) and make suredebugis selected. - If using a Run/Debug configuration, verify that the configuration is set to use the
debugbuild variant in its settings.
4. Library Module Dependency Conflicts
If your app module depends on a library module that defines its own BuildConfig.BUILD_TYPE, there's a small chance the library's value is overriding your app's. This is rare but can happen if the library uses the same field name.
Fix:
- Check the library's
build.gradlefor any custombuildConfigFieldentries forBUILD_TYPE. - Ask the library maintainer to remove that custom field, or rename your app's field (though ideally you should rely on the auto-generated one).
5. Product Flavor + Build Type Mix-Up
If you're using product flavors, make sure you're not accidentally selecting a variant that combines a flavor with the Release build type. For example, a variant like freeRelease would still show release as the build type, even if you intended to run a Debug build.
Fix:
- Confirm you're selecting a variant that ends with
Debug(e.g.,freeDebug,paidDebug) from the variant selector.
Once you work through these checks, your BuildConfig.BUILD_TYPE should correctly reflect the active build type. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Liru

