Whether Android code detection is debug or release

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.