The release and beta versions need to be handled differently during the development process. For example, we want the beta version to print log information or to output some log files, but the official version does not want to output this information. Using version-controlled branching is a method. However, it is more laborious to merge between branches in this way. There is a simple way to detect whether the APK is debug or release in the code. 1. Check the status of the current APK
1 /**2 * Determine if the current application is a debug state3 */4 5 Public Static BooleanIsapkindebug (Context context) {6 Try {7ApplicationInfo info =context.getapplicationinfo ();8 return(Info.flags & applicationinfo.flag_debuggable)! = 0;9}Catch(Exception e) {Ten return false; One } A}
2. Detect other applications
1 /**2 * Detect if other apps are in debug mode. 3 */4 Public Static Booleanisapkdebugable (Context context,string packagename) {5 Try {6PackageInfo Pkginfo =Context.getpackagemanager (). Getpackageinfo (7PackageName, 1);8 if(Pkginfo! =NULL ) {9ApplicationInfo info=Pkginfo.applicationinfo;Ten return(info.flags&applicationinfo.flag_debuggable)!=0; One } A -}Catch(Exception e) { - the } - return false; -}
The officially released version should remove the debug code, but using the above judgment can avoid bad effects due to negligence. The ideal should be based on the different ways of packaging, to remove some part of the code.
Whether Android code detection is debug or release