Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug and production resources

Some Android libraries such as Google Analytics use resources for configuration purposes (e.g. ga_trackingId).

In these cases, I have different values for debug and production. What I currently do is manually comment the production values when I'm debugging, and viceversa. It looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--  DEBUG -->
    <string name="ga_trackingId">UA-12345678-1</string>
    <integer name="ga_dispatchPeriod">1</integer>
    <bool name="ga_debug">true</bool>

    <!--  PRODUCTION -->    
    <!--string name="ga_trackingId">UA-87654321-1</string>
    <integer name="ga_dispatchPeriod">120</integer>
    <bool name="ga_debug">false</bool-->
</resources>

This way of switching configuration is tedious and error-prone, and generates unnecessary repository changes if I'm not careful. Is there a better way?

(e.g.: on iOS I use conditional compilation with the IF DEBUG macro)

like image 702
hpique Avatar asked Mar 06 '13 08:03

hpique


2 Answers

Under the src folder you probably have a main folder where you store all shared stuff. But you can have specific resources for flavours or build types.

Put a folder named debug under the src folder where you will place a copy of your xml file but with with proper content. You have to maintain the folder structure under debug so the global_tracker.xml needs to be placed in ../src/debug/res/xml

Your folder structure should look like this:

enter image description here

Android Studio will notice that this xml file have multiple versions.

This is what you should see in AS:

enter image description here

You can use this for all kind of resources, i.e., have multiple versions of the same file and it will be "magically" chosen properly.

like image 165
Wallace Avatar answered Oct 19 '22 13:10

Wallace


I had a similar issue with Google Maps keys where they depend on the signature. What I did was to use the ant script which generates/copies resources to the project conditionally. You can include the ant script in Eclipse under the Project>Properties>Builders

If you need to use the DEBUG value in the code, you can create a java file with static values that will be included conditionally too.

Please comment if ant environment variables worked properly (you can see the "Build type: " message in console after execution of the script).

<project name="build-res">
<property name="conditional.resources.dir" value="myresources" />
<property name="keys_file" value="res/values/keys.xml" />

<target name="copy-release" if="${build.mode.release}" >
    <property name="build.type" value="Release" />
    <echo message="Build type: ${build.type}" />
    <property name="google.maps.key" value="nanana-value-for-release" />
    <copy file="${conditional.resources.dir}/Release.java" tofile="gen/com/example/project/BuildInfo.java" />
</target>

<target name="copy-debug" if="${build.mode.debug}">
    <property name="build.type" value="Debug" />
    <echo message="Build type: ${build.type}" />
    <property name="google.maps.key" value="lalala-value-for-debug" />
    <copy file="${conditional.resources.dir}/Debug.java" tofile="gen/com/example/project/BuildInfo.java" />
</target>

<target name="build-res" depends="copy-debug,copy-release">
    <echo file="${keys_file}" message="&lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;resources&gt;&lt;string name='google_maps_key'&gt;${google.maps.key}&lt;/string&gt;&lt;/resources&gt;" />
</target>
</project>
like image 37
Krzysztof Avatar answered Oct 19 '22 13:10

Krzysztof