Distinctive features of the development mode and release environment mode on Android

I am creating an Android application and want to support several environment variables that I can configure depending on whether I am in development mode or release mode. For example, I need to call a web service, and the URL will be slightly different in any mode. I would like to externalize this and other settings so that I can easily change them based on my target deployment.

Are there any recommendations or anything in the SDK to help with this need?

+53
android development-environment release-management
Nov 16 '09 at 17:36
source share
8 answers

According to this stackoverflow post , in the SDK Tools version 17 (we are at 19 like this record) adds the BuildConfig.DEBUG constant, which is true when creating the dev assembly.

+28
May 19 '12 at 4:31
source share
β€” -

The following solution assumes that the manifest file always sets android:debuggable=true during development and android:debuggable=false to release the application.

Now you can check this attribute value from your code by checking the ApplicationInfo.FLAG_DEBUGGABLE flag in ApplicationInfo obtained from PackageManager .

The following code snippet may help:

 PackageInfo packageInfo = ... // get package info for your context int flags = packageInfo.applicationInfo.flags; if ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { // development mode } else { // release mode } 
+43
Jul 08 '10 at 14:10
source share

@viktor-bresan Thanks for the helpful solution. It would be more helpful if you included a general way to get the current context of the application to make it a fully working example. Something along the lines below:

 PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0); 
+9
Jan 18 '11 at 1:02
source share

I would watch isDebuggerConnected

+3
Nov 16 '09 at 17:44
source share

How about something like the code below ...

 public void onCreate Bundle b ) { super.onCreate(savedInstanceState); if ( signedWithDebugKey(this,this.getClass()) ) { blah blah blah } blah blah blah } static final String DEBUGKEY = "get the debug key from logcat after calling the function below once from the emulator"; public static boolean signedWithDebugKey(Context context, Class<?> cls) { boolean result = false; try { ComponentName comp = new ComponentName(context, cls); PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(),PackageManager.GET_SIGNATURES); Signature sigs[] = pinfo.signatures; for ( int i = 0; i < sigs.length;i++) Log.d(TAG,sigs[i].toCharsString()); if (DEBUGKEY.equals(sigs[0].toCharsString())) { result = true; Log.d(TAG,"package has been signed with the debug key"); } else { Log.d(TAG,"package signed with a key other than the debug key"); } } catch (android.content.pm.PackageManager.NameNotFoundException e) { return false; } return result; } 
+3
Dec 01 '10 at 2:41
source share

Android build.gradle works well with Debug and Release Environment.

Add the following code snippet to the build.gradle file

 buildTypes { debug { buildConfigField "Boolean", "IS_DEBUG_MODE", 'true' } release { buildConfigField "Boolean", "IS_DEBUG_MODE", 'false' } } 

Now you can access the variable as shown below

  if (BuildConfig.IS_DEBUG_MODE) { { //Debug mode. } else { //Release mode } 
+3
Dec 09 '16 at 4:37
source share

Today, I accidentally came across another method that seems really straightforward. Look at Build.TAGS, when the application is designed for development, it is evaluated in the "test keys" String.

Not much easier than string comparison.

Also, Build.MODEL and Build.PRODUCT evaluate the string "google_sdk" on the emulator!

+1
Dec 03 2018-10-12T00:
source share

Here is the method I use:

http://whereblogger.klaki.net/2009/10/choosing-android-maps-api-key-at-run.html

I use it to switch the debug log and the map API key.

0
Aug 25 '10 at 16:23
source share



All Articles