Le_Enot
Le_Enot

Reputation: 839

Android useful debug/release macros (using gradle?)

I am writing an android app. I need to use different variable values for release and debug versions. As mentioned somewhere on this site, I do like this:

private static final boolean debug = true;
if(debug){
    // do some debug work
} else{
      // do release work
  }

but in some cases I need different values of final variables (which are members of the class) for debug/release versions and this solution obviously doesn't work. Is there any solution of this problem (except changing variables manually), maybe somehow using Gradle scripting (like buildConfigField, but in any class)?

Upvotes: 1

Views: 1835

Answers (2)

Martin Cazares
Martin Cazares

Reputation: 13705

There's also another approach to these kind of scenarios, this open source project(Android Studio Macros) allows you to do what you want plus some more complicated things by using "//<#DEBUG_AREA> and //<#/DEBUG_AREA>" tags within your code, the basic idea is that anything within those tags will be commented when you change your build variants for example if you have something like this in a for loop:

       //=========This piece of code is only for logging purposes...=========
        Log.e("LogUserInfo", "Name: " + name);
        Log.e("LogUserInfo", "Id: " + user.getId());
        Log.e("LogUserInfo", "Id: " + user.getDistance());
        //====================================================================

In stead of doing this:

if(DEBUG){
      Log.e("LogginUserInfo", "Name: " + name);
      Log.e("LogginUserInfo", "Id: " + user.getId());
      Log.e("LogginUserInfo", "Id: " + user.getDistance());
 }

With this macro you can do this(full method):

private List<String> getNamesOfUsersNearMe(String zipCode){
    List<User> users = mBusinessLogic.getUsersByZipcode(zipCode);
    if(users == null || users.size() < 1){
        return null;
    }

    List<String> names = new ArrayList<String>();
    int totalUsers = users.size();
    for(int i = 0; i < totalUsers; i++){
        User user = users.get(i);
        String name = user.getName();
        names.add(name);
        //<#DEBUG_AREA>
        Log.e("LogginUserInfo", "Name: " + name);
        Log.e("LogginUserInfo", "Id: " + user.getId());
        Log.e("LogginUserInfo", "Id: " + user.getDistance());
        //</#DEBUG_AREA>
    }
    return names;
}

And when you change your build variant to release it would become something like this:

private List<String> getNamesOfUsersNearMe(String zipCode){
    List<User> users = mBusinessLogic.getUsersByZipcode(zipCode);
    if(users == null || users.size() < 1){
        return null;
    }

    List<String> names = new ArrayList<String>();
    int totalUsers = users.size();
    for(int i = 0; i < totalUsers; i++){
        User user = users.get(i);
        String name = user.getName();
        names.add(name);
        /*<#DEBUG_OFF>
            Log.e("LogginUserInfo", "Name: " + name);
            Log.e("LogginUserInfo", "Id: " + user.getId());
            Log.e("LogginUserInfo", "Id: " + user.getDistance());
        </#DEBUG_OFF>*/
    }

    return names;
}

Which is way better in performance for long loops and makes your code cleaner by getting rid of the unnecessary code while in "release" mode, of course if you go back to "debug" it will uncomment the area and leave it the way it originally was with the "<#DEBUG_AREA>" tags...

Hope it Helps!

Regards!

Upvotes: 0

adudakov
adudakov

Reputation: 222

Sure. You can do it in any class

public final static String FINAL_STRING = BuildConfig.DEBUG ? "debug" : "release";

Upvotes: 3

Related Questions