Android: Joda remove Timezone raw files

376 views Asked by At

Is it possible to strip the Timezone files from Joda-Time out of my build? I'm only using 1 in release builds and 2 in debug builds to use in a test.

My app is only available in one country and a 2MB reduction seems like a good idea getting me around the 20MB Mark.

1

There are 1 answers

0
AudioBubble On

You can get Joda-Time's source code and then you have some alternatives.


Don't use any timezone

When you build the project (mvn clean package), it generates a joda-time-2.9.9-no-tzdb.jar file, which is a jar file without any timezone data. Using this will require some adaptations:

  1. Using this jar, the only available timezone is UTC. So DateTimeZone.getAvailableIDs() and DateTimeZone.getDefault() returns just UTC. You won't be able to convert between another timezones, as UTC is the only one available.
  2. When calling some methods, I'm getting some exceptions (Resource not found: "org/joda/time/tz/data/ZoneInfoMap"). To get rid of it, I had to set the default timezone provider to UTC:

    System.setProperty("org.joda.time.DateTimeZone.Provider", "org.joda.time.tz.UTCProvider");
    

    This way, the exceptions are gone.


Choose which timezones you want

If you want to use another timezone instead of just UTC, there's an alternative.

In the pom.xml file, look for the compile-tzdb execution and comment the files that you don't want to include in the jar. In this example, I'm keeping just the southamerica file:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
            <execution>
                <id>compile-tzdb</id>
... lots of lines ...

<!-- Commenting the TZ files I don't want -->
<!-- <argument>africa</argument> -->
<!-- <argument>antarctica</argument> -->
<!-- <argument>asia</argument> -->
<!-- <argument>australasia</argument> -->
<!-- <argument>europe</argument> -->
<!-- <argument>northamerica</argument> -->
<argument>southamerica</argument>
<!-- <argument>pacificnew</argument> -->
<!-- <argument>etcetera</argument> -->
<!-- <argument>backward</argument> -->
<!-- <argument>systemv</argument> -->

Then build it with:

mvn clean package -Dmaven.test.skip=true

I'm skipping the tests because some of them uses timezones that aren't in South America files (such as Europe/London). Of course you can also change the tests if you want, but I've just chosen the fastest way (I usually don't skip tests in production builds, don't judge me).

Then, the joda-time-2.9.9.jar file will contain only South America timezones files. You can optionally include the backward file in your build if you want to include synonyms (some zones had their names changed, but the old names are kept and became synonyms in the backward file).

With this, the resulting jar will contain only South America zones and UTC. If the JVM default timezone is not one of the South America ones, DateTimeZone.getDefault() will return UTC.


If you want to reduce even more the zone files, you can edit them manually. But you must be careful to not remove important data from the timezones you want to keep. Here's a tutorial about the format of IANA tz files.

Just keep in mind that removing timezone data is not something I would recommend. All the historical data are important and there's (almost) always some timezone acting behind the scenes. You'll have to do lots of tests to make sure it doesn't affect any aspect of your application (some date-related errors, specially involving timezones, can be tricky and hard to debug). Good luck!