You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中BuildConfig.BUILD_TYPE始终为Release,调试时获取错误URL的问题

Why is BuildConfig.BUILD_TYPE Always "release" in Debug Variant?

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 buildTypes block, 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 Restart to 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 sure debug is selected.
  • If using a Run/Debug configuration, verify that the configuration is set to use the debug build 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.gradle for any custom buildConfigField entries for BUILD_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:05:04