如何定义共享构建变量?

发布于 2025-01-20 12:30:22 字数 696 浏览 3 评论 0原文

我正在尝试为每种构建类型设置自定义变量。

我的 build.gradle 文件如下所示:

buildTypes {
    each {
        buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
    }
    debug {
        buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
    }
    release {
        buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

我希望通过 final String sharedUrl = BuildConfig.SHARED_URL; 在调试和调试阶段都可以访问代码中的 SHARED_URL在发布版本中。那么,我怎样才能实现这个目标呢?

PS 我知道我可以在两个版本中复制变量 SHARED_URL ,但我不想进行样板化。

I'm trying to set up custom variable for each build type.

My build.gradle file looks like this:

buildTypes {
    each {
        buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
    }
    debug {
        buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
    }
    release {
        buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

And I want to have an access to the SHARED_URL in my code by final String sharedUrl = BuildConfig.SHARED_URL; both in the debug and in the release build. So, how can I achieve this goal?

P.S. I'm understand that I can just copy variable SHARED_URL in the both builds, but I don't want to boilerplating.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2025-01-27 12:30:22

您可以将常用的 buildConfigField 放入 defaultConfig 中:

android {
  defaultConfig {
    buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
  }

  buildTypes {
      debug {
        buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
      }
      release {
        buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
      }
  }
}

SHARED_URL 现在可用于所有构建变体。

You can put your common buildConfigField in defaultConfig:

android {
  defaultConfig {
    buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
  }

  buildTypes {
      debug {
        buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
      }
      release {
        buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
      }
  }
}

SHARED_URL will now be available for all build variants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文