Wednesday, June 29, 2011

Check for Debuggable flag in Android

Many a times we as a programmers want to hard code some values in our application for test purpose.

AND MOST OF THE TIMES WE FORGET TO REMOVE THEM WHEN WE RELEASE THE APP.

Well, to overcome this issue, i am posting a small snippet of code. Using this you can remove this issue just by checking for a flag before your hard coded values. ;P

/*
 Copyright [2011] [Abhinava Srivastava]

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */


public static boolean isDebugMode(Activity context){
boolean debug = false;
PackageInfo packageInfo = null;
try {
packageInfo = context.getPackageManager().getPackageInfo(context.getApplication().getPackageName(),
PackageManager.GET_CONFIGURATIONS);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo != null) {
int flags = packageInfo.applicationInfo.flags;
if ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
debug = true;
}else
debug = false;
}
return debug;
}

So now in your application just check for the return value of this function :)

1 comment:

rakesh said...

HI Abhinav,

Thank u very much...!!

Twitter is working with u r sample consumer keys.

But when we check with our consumer & access keys generated by twitter developer(dev.twitter.com) are not working....!!

Our keys

consu key: kX7zKV5r603EEFB8l6HPQ

con secret ky: uvOulB5OJVybQpxhtd74ai3ruXlR1ARJhMrQVghXE

Accesstoken:334519839-VtHpMrdJQsYsiAGm4v9jhWHvz6M88ZNDPL2megFn

AccessTokenSecret: LQQsQCpLhLg2t6fuFq243UdUmqg0n68z9ZOs9sbU