android - 我怎么知道应用程序正在 Android Studio 中的 Debug模式下运行?

标签 android android-studio

在 Android Studio 中,我可以在 IDE 中单击“运行”按钮来运行应用程序,该应用程序在 Debug模式下运行,但我如何以编程方式知道有 Debug模式?就像下面的代码。

If (IsDebug()){
   Toast.makeText(getApplicationContext(), "This is in debug, it will diplay some prompt information",Toast.LENGTH_LONG).show();
}else{
   Toast.makeText(getApplicationContext(), "This is release edition, it will not diplay debug information",Toast.LENGTH_LONG).show();
}

最佳答案

为简单起见,您可以使用 BuildConfig,例如 if (BuildConfig.DEBUG) {...}

BuildConfig 是在编译时在 AndroidStudio 中点击“运行”时生成的,如下所示:

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "YOUR APP";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "YOUR VERSION";
}

但是如果您在您的库项目中使用它,则会报告一些错误。 http://www.digipom.com/be-careful-with-buildconfig-debug/

如果你想要更高级的方式,你可以在你的build.gradle中定义。 增强您的 BuildConfig http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/

关于android - 我怎么知道应用程序正在 Android Studio 中的 Debug模式下运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801903/

相关文章:

Java:有没有办法在使用 post 上传文件时计算下载吞吐量(在特定时间间隔内)?

java - 完成或从辅助 Activity 中退出应用程序?

android - 使用 robospice google http 客户端在 Post 中发送 JSON

java - Android - 从 Internet 下载 KML 文件导致文本出现乱码

android - Gradle:创建lib.aar文件后如何执行任务?

android - 如何避免每次运行 Espresso 时出现 'Gradle build'

android - 首选项屏幕为白色

android-studio - 如何在 Android Studio 中创建新的 res 文件夹

android - GenyMotion 模拟器不会超出白屏范围

android - 无法将文件或应用程序添加到android的/system/xbin