Tabs vs spaces for code indentation

One argument which some people might give in favor of using tabs is that it allows the viewer to decide how the code should appear to them. And hence, it separates the logic (indentation) from its appearance. The biggest flaw in that argument is that it renders the concept of line length limits meaningless. Line length limits and the associated rules of wrapping bring a good structure to the code.  Someone looking at a piece of code with 3-levels of nesting and tab-length set to 8 will see a very different line length from someone using a tab-length of 2. Always expand tabs.

File size should always be of “long” type

A 32-bit signed int can deal with ~2GB worth of data. And if your code is not going to deal with files larger than 2GB, why worry? But what if  someone wants to use the same code for a video file some day? Or What if someone writes another code to iterate over all the files in the BASE_DIR directory? Most likely they will be inclined to use int for the final sum as well. Adding integers in most languages results in int and automatic overflows into a negative number (and even worse, back to a positive number). The caller code might think that BASE_DIR does not exist. Therefore, the best future-proofing is to never have file size stored as an integer. Even, Android platform got it wrong with StatFs#getBlockSize and corrected it by adding StatFs#getBlockSizeLong.

Google I/O 2016: Android notes

General

  1. Multitasking – multi-window mode and picture-in-picture mode. This includes the ability to launch window in adjacent activity, and drag and drop between activities.
  2. Notifications – Custom quick settings tile for an activity
  3. Multi-locale – users can specify locale beyond their primary locale
  4. ScopedDirectoryAccess for the shared storage
  5. New file-level encryption mode (as opposed to block-level encryption) and the corresponding Direct Boot
  6. Java 8, Jack, and ndk support in Gradle
  7. GCC deprecated in favor of Clang

TechTalk on Image size/compression

  1. PNG – Get image down to 256-bit palette (if possible) or compress them using Zopfli. Test the difference with butteraugli
  2. Convert PNG to Vector drawable using Potrace. Vector drawable are natively supported on Android 5.0 and above. Use compat to use them on the older versions. Or generate PNG for the older versions (not recommended)
  3. WebP is another option (Note: Speaker failed to mention that WebP is natively supported only on Android).
  4. Avoid JPEGs. They are usually larger in size.

Read More

My rm -rf moment

Yes, it did happen, and no, I am not stupid enough to execute rm -rf *. It was a bit more convoluted than that. I was trying to prepare a customized SD card image for Android emulator. The fastest way to do that was to mount the SD card on my GNU/Linux machine and modify the files. The files on SD card have root as the owner. Therefore, the easiest way to maintain a clean state was to do all modifications using sudo. My script included a command rm -r ${base_dir}/${old_file}. While executing the script, due to an error both the variables were not set. It took me a few hours to recover; I never committed the broken script, so, I did not harm anyone else. But since then my bash scripts have always included set -o nounset.

Android command-line: gradle and testing

For android projects, some engineers use Android Studio (new), some use Eclipse with ADT (old), few like me still savor command line, this blog post is about handling (building, installing and testing) android projects from command line.

  1. To create android project
  2. After changing to directory test_android_app (cd test_android_app), fix a bug

     
  3. Some useful gradle commands
  4. The code will be in src directory, eg. for the Main activity, code is in src/main/java/net/ashishb/TestAndroidApp/Main.java and test is in src/androidTest/java/net/ashishb/TestAndroidApp/MainTest.java
  5. For testing, modify the Main class and add square method,

    And in MainTest
  6. Modify build.gradle to add a testing config

     
  7. Run the test(s)
  8. Complete code can be seen at https://github.com/ashishb/android_gradle_demo

Android, Gradle and compile-time only dependencies

Android plugin for Gradle does not support Java-style compile time only dependencies.

After spending a few hours on trying to build android app targeted for Amazon SDK (without using Amazon’s Android specific plugin but just their jar stubs for maps, ADM and Home widget), I finally found that the one way to support compile-time dependencies is following.

For application project

For the library project

References

  1. https://stackoverflow.com/questions/16613722/gradle-configurations-not-working-as-expected-in-new-android-build-system
  2. http://stackoverflow.com/a/24157721