How to check if APK is signed or “debug build”?

后端 未结 10 1903
我在风中等你
我在风中等你 2020-11-29 16:34

As far as I know, in android \"release build\" is signed APK. How to check it from code or does Eclipse has some kinda of secret defines?

I need thi

相关标签:
10条回答
  • 2020-11-29 16:51

    First add this to your build.gradle file, this will also allow side by side running of debug and release builds:

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
    }
    

    Add this method:

    public static boolean isDebug(Context context) {
        String pName = context.getPackageName();
        if (pName != null && pName.endsWith(".debug")) {
            return true;
        } else {
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:53

    Another option, worth mentioning. If you need to execute some code only when debugger is attached, use this code:

    if (Debug.isDebuggerConnected() || Debug.waitingForDebugger()) { 
        //code to be executed 
    }
    
    0 讨论(0)
  • 2020-11-29 16:54

    Solved with android:debuggable. It was bug in reading item where in some cases debug flag on item was not being stored in record getting if (m.debug && !App.isDebuggable(getContext())) always evaluated to false. My bad.

    0 讨论(0)
  • 2020-11-29 16:57

    Answered by Mark Murphy

    The simplest, and best long-term solution, is to use BuildConfig.DEBUG. This is a boolean value that will be true for a debug build, false otherwise:

    if (BuildConfig.DEBUG) {
      // do something for a debug build
    }
    
    0 讨论(0)
提交回复
热议问题