Social

Why I started used data binding

After my first blog post about Fastest way for creating RecyclerView Adapter, a couple of you post a comment why I don’t use Data Binding instead of Butterknife. To be honest, I didn’t try to use it simply because I’m used to using Butterknife. I know now that I was terribly wrong, so for every developer like me, here is post to try to convince you to start using data binding.

Gradle is awesome build tool, and I’m so happy that Android Studio uses it (who ever programmed in Eclipse knows what I’m talking about). I would like to share some tips and tricks.

Update multiple libs version at once

This is really useful with Google’s Android support library, but you will find other examples as well. First, you will need to create extra property, in my example, I name it androidSupportVersion. After that, use this property with your dependencies put a $ sign at the beginning and surround it with curly brackets.

The cool thing about this is that Android Studio will still tell you when a newer version is available.

BuildConfig

This class is automatically generated, and here (with Gradle) you can store some global variables, for example, probably the most common use of this is to have two different API endpoints.

Resources

Similar to previous part, we can use Gradle for generating resources, for example, different name of the app.

You can use all resource types as the type for resValue: array, attr, bool, color, declare-styleable, dimen, fraction, id, integer, plurals, string, style.

Manifest placeholders

We can also define something useful and use it in our app manifest.

1

2

3

4

defaultConfig{

...

manifestPlaceholders=[mapKey:"b279b6400b0bf87fba17c98249a66df0"]

}

And now we can use it in manifest like this:

1

2

3

&lt;meta-data

android:name="com.google.android.maps.v2.API_KEY"

android:value="${mapKey}"/&gt;

Signing configs

We can store keystore private info under gradle.properties

1

2

3

4

keystore=/home/myname/keystore/mykey

keystore_password=mypass

key_alias=mykey

key_password=keypass

and it to the gradle file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

signingConfigs{

release{

storeFile file("${keystore}")

storePassword"${keystore_password}"

keyAlias"${key_alias}"

keyPassword"${key_password}"

}

}

buildTypes{

release{

signingConfig signingConfigs.release

..

}

}

Do you need SHA-1 certificate fingerprint? You could use a keytool, but there is a lot simpler solution. Just run gradle:

1

./gradlew signingReport

Git in Gradle

You can use Git SHA or build time inside your Gradle, for example, you can add Git SHA to your versionNameSuffix just for debug build type.

Hello world.

First of all, this is my first blog post, so here is a quick intro. I’m will try to share some interesting blog posts about cool things that I know.

We all hate (or at least I do) writing boilerplate code so I would like to share this quick way for writing RecyclerView Adapter. Before we create an adapter, we need to prepare 2 simple things; one is a class that will be our model, and second will be a layout for an item in the RecyclerView.

Now the fun stuff

Now go to Android Studio > Preferences > Editor > File and code templates and click on that “+” icon for adding the new one.

Where is my adapter?

Now that we are all set, we need to finally create that adapter. Go to the File > New > and select your RecyclerView adapter template.

You will see a dialog like this, fill name for you adapter class, item class, and finally your item layout, click OK and… magic. The boring part of your adapter class is already written, all you have to do is make sure that you Inject your views and fill it with data (the fun part).