paint-brush
Configuring Android Project — Little Things That Matterby@dmytrodanylyk
5,032 reads
5,032 reads

Configuring Android Project — Little Things That Matter

by Dmytro DanylykFebruary 27th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<strong>This article is a part of <em>Configuring Android Project</em>&nbsp;series:</strong>

Company Mentioned

Mention Thumbnail
featured image - Configuring Android Project — Little Things That Matter
Dmytro Danylyk HackerNoon profile picture

This article is a part of Configuring Android Project series:

  1. Little Things That Matter
  2. Version Name & Code
  3. Static Code Analyses Tools
  4. Continuous Integration

Everything we have discussed in current article is available in template project.

gitignore

When you create a new android project in Android Studio it already comes up with gitignore file, but usually it does not contain all necessary rules.

To quickly generate and download gitignore files I recommend you to use gitignore.io site. Just type in necessary keywords like — Android, Intellij and click generate button.

Check out gitignore file in template project.

tools folder

If you have some 3rd party scripts, rulesets or other files which are related to your project don’t just drop them in root directory — it will create a mess.(especially for those who are using Project View, not Android View)

Try to create a folder (e.g. tools) and put all those files into this folder.

Usually I put there custom gradle scripts files, rules for proguard and static code analysis tools like pmd, findbugs, lint.

Check out tools folder in template project.

flavors

Flavors are used to create builds with different set-up. In most cases I immediately set-up two flavors — dev and prod which have different:

  • applicationId
  • versionCode / versionName
  • server endpoints
  • google services keys

<a href="https://medium.com/media/150ffe9e2eac6e98f04e23425871a4a7/href">https://medium.com/media/150ffe9e2eac6e98f04e23425871a4a7/href</a>

Check out productFlavors in template project.

keystore

A keystore is a binary file that contains one or more private keys used to sign your application.

When running or debugging your project from the IDE, Android Studio automatically signs your APK with a debug certificate generated by the Android SDK tools.

There are several issues when using local debug keystore:

  • expiration date of 365 days
  • installing application from multiple computers requires uninstalling first
  • google services require keystore SHA-1 fingerprint

That’s why I usually generate debug keystore and commit it to version control system.

<a href="https://medium.com/media/7d4160e8568fb62440027f97bd777ee0/href">https://medium.com/media/7d4160e8568fb62440027f97bd777ee0/href</a>

Check out signingConfigs in template project.

proguard

On Android proguard is used for three things:

  • shrink unused code — helps you to survive the 64k limit
  • optimize code & APK
  • obfuscate code — makes your APK difficult to reverse engineer

The problem is that obfuscation and code optimization significantly increase compilation time and make debugging harder.

That’s why it is better to use different proguard rules for release and debug build:

  • rules-proguard.pro
  • rules-proguard-debug.pro

<a href="https://medium.com/media/cbe51bd728c582af5c1df256ac72b760/href">https://medium.com/media/cbe51bd728c582af5c1df256ac72b760/href</a>

Proguard rules for debug build must have following lines to force proguard ignore warnings, skip code obfuscation and optimization:

<a href="https://medium.com/media/c3d6ea14f4008e3de0f5ee2205dd4ae2/href">https://medium.com/media/c3d6ea14f4008e3de0f5ee2205dd4ae2/href</a>

For release version it will be much harder to setup proguard rules, because almost every library has it’s own specific rules. Fortunately there is open source repository called — android-proguard-snippets which contains proguard rules for all major libraries.

<a href="https://medium.com/media/7a10e36468e5b5690187ce6d02c1fcec/href">https://medium.com/media/7a10e36468e5b5690187ce6d02c1fcec/href</a>

Check out rules-proguard.pro and rules-proguard-debug.pro in template project.

strict mode

Android StrictMode helps you to detect different kinds of problems:

  • closable object is not closed
  • file reading / network requests performed on main thread
  • uri exposed

Whenever such problem is detected it can show you appropriate log or crash your application, depending on your configuration.

I suggest you to turn it on only for debug build and use detectAll methods to detect all kind of problems.

<a href="https://medium.com/media/e507e64b63df84012de67395421b43f3/href">https://medium.com/media/e507e64b63df84012de67395421b43f3/href</a>

Here is example of log when you forgot to close SQLiteCursor:

<a href="https://medium.com/media/865507608541eeb989184fb6a8612c92/href">https://medium.com/media/865507608541eeb989184fb6a8612c92/href</a>

Check out StrictMode in template project.