2

Does anyone know of a clever way, ideally using the Eclipse/ADT workflow, to apply specific resources to a project depending on whether it was a debug or release build (i.e. in Eclipse whether the application was Run or Exported)? The common use case we run into all the time for this is with API keys (like Maps). It would be great to set up a project to have a strings.xml file specifically for all the debug strings, and then a separate one for all the release strings.

Is there any way to do this without needing to move to the ANT or Maven style of building?

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • 1
    There are a few options/ideas posted here: http://stackoverflow.com/questions/3029819/android-automatically-choose-debug-release-maps-api-key/3828864 – mbeaty Dec 22 '11 at 20:52

1 Answers1

2

Although it's not what you've asked for, I highly recommend automating the release build as you want that to be consistent and correct every time you build it.

For development you can keep using the Eclipse workflow in that case. For my own app, Rainy Days, I switch out the debug vs release map keys in the ant build. I've set the map key in a string resource and replace that string in the ant build. I then build the release using Jenkins and get the blessed apk from Jenkins if the build succeeds. The nice thing about that is that you can further automate the build with unit tests and device installation tests in an automated fashion.

IMHO you shouldn't be detecting debug builds in your code, as it adds more code paths and makes your code more complex, and even introduces overhead at runtime in some cases.

botteaap
  • 5,790
  • 3
  • 29
  • 35
  • Thank you for your thoughts in this matter. – devunwired Feb 19 '12 at 05:31
  • @bottleapp can you explain a bit in detail how you switch/replace the values from a string file ? – Sandeep Chayapathi May 15 '12 at 21:49
  • @SandeepChayapathi I use [this task](http://ant.apache.org/manual/Tasks/replaceregexp.html) to find and replace the text in the resource file. – botteaap May 16 '12 at 13:12
  • @botteaap thanks. The string resources containing the map keys, are they part of the project ? How do you manage this with regular builds from eclipse ? Im in a similar situation where I have to switch to different urls for release and debug/test builds. Im manually changing a flag before compiling. I accidentally uploaded a test build to google play, which created a havoc. Trying to figure out ways to avoid this in future. – Sandeep Chayapathi May 16 '12 at 14:07
  • instead of grep/replace I setup a ant pre-build rule to overwrite a file (a _resource_ file with integer and boolean values). On launch the _application_ would setup a series of static variables based of these values. This discussion pointed me in the right direction - http://stackoverflow.com/questions/8278282/how-can-i-attach-different-string-resource-files-during-android-ant-release-and – Sandeep Chayapathi May 16 '12 at 18:47
  • The whole point is that you NEVER build anything signed with release keys in Eclipse. Development is within Eclipse, release builds are build by Jenkins in my case. This prevents those kinds of errors. And yes you can find/replace or just overwrite. Just do on your build server what you need to do to make it a proper release. – botteaap May 16 '12 at 21:08