在运行期间检测应用程序是否处于释放模式

问题描述 投票:5回答:2

我在Dart应用程序中得到了一堆测试和调试内容,我想确保在使用pub构建发布版本时禁用这些内容。

是否有任何常量或其他方法来检查当前运行的应用程序版本是否是发布版本?

例:

if (!IS_BUILD) {
   performAutomatedDummyLogin()
} else {
   login();
}
build dart
2个回答
7
投票

assert(...);中的代码仅在已检查(开发)模式下执行。在发布模式下运行或在发布模式下构建时,不会执行此代码。

bool isRelease = true;
assert(() {
  isRelease = false;
  return true;
});

if(isRelease) {
 ...
} 

也可以看看


0
投票

我建议使用DEBUG。我更喜欢这种方法,因为它不需要另一个变量来包含isDebug或isRelease。

// release mode only
#if !Debug
    MessageBox.Show("Release mode");
#endif

// debug mode only
#if Debug
    MessageBox.Show("Debug mode");
#endif

// debug and release mode with sample values
#if DEBUG
   int[] data = new int[] {1, 2, 3, 4};
#else
   int[] data = GetInputData();
#endif
// actual code that follows after the variable setting
int sum = data[0];
for (int i= 1; i < data.Length; i++)
{
   sum += data[i];
}

有关参考,请参阅link

© www.soinside.com 2019 - 2024. All rights reserved.