简体   繁体   English

我怎么知道Android在Android Studio的调试模型下运行?

[英]How can I know an App is running under debug model in Android Studio?

In Android Studio, I can click "Run" button in IDE to run an app, the app is running under debug model, but how can I know there is a debug model programatically? 在Android Studio中,我可以单击IDE中的“运行”按钮来运行应用程序,应用程序在调试模型下运行,但我怎么能以编程方式知道调试模型? just like the following code. 就像下面的代码一样。

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();
}

For simplicity, you can use BuildConfig such as if (BuildConfig.DEBUG) {...} . 为简单起见,您可以使用BuildConfig例如if (BuildConfig.DEBUG) {...}

BuildConfig is generated in compile time when you click 'Run' in AndroidStudio, which looks like: 在AndroidStudio中单击“运行”时,编译时会生成BuildConfig ,如下所示:

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";
}

But some bugs are reported if you're using this in your library project. 但是,如果您在库项目中使用此错误,则会报告一些错误。 http://www.digipom.com/be-careful-with-buildconfig-debug/ http://www.digipom.com/be-careful-with-buildconfig-debug/

If you want a more advanced way, you can define in your build.gradle . 如果您想要更高级的方法,可以在build.gradle进行定义。 Enhancing your BuildConfig in http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/ http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/中 增强您的BuildConfig

It is possible to add BuildConfig-Variables to the Gradle-script and read them in the Code. 可以将BuildConfig-Variables添加到Gradle脚本并在代码中读取它们。

buildTypes {
    release {
        ...
        buildConfigField "int", "appMode", "0"
    }

    debug {
        ...
        buildConfigField "int", "appMode", "1"
    }
}

Read the variable like this: 像这样读取变量:

If (BuildConfig.appMode == 1){
   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();
}

Use this to check if the app is debuggable or not 用它来检查应用程序是否可调试

private boolean isDebuggable(Context ctx)
{
    boolean debuggable = false;

    PackageManager pm = ctx.getPackageManager();
    try
    {
        ApplicationInfo appinfo = pm.getApplicationInfo(ctx.getPackageName(), 0);
        debuggable = (0 != (appinfo.flags & ApplicationInfo.FLAG_DEBUGGABLE));
    }
    catch(NameNotFoundException e)
    {
        /*debuggable variable will remain false*/
    }

    return debuggable;
}

尝试这个:

boolean isDebuggable =  ( 0 != ( getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM