Month: February 2015

Introducing Prefser

2015-02-22 Android, RxJava No comments

I’ve recently released Prefser, which is a wrapper for Android SharedPreferences with object serialization and RxJava Observables.

Prefser wraps SharedPreferences and thanks to Java Generics provides you simpler API than classic SharedPreferences with only two methods:

void put(String key, Object value);
<T> T get(String key, Class classOfT, T defaultValue);

Classic SharedPreferences allows you to store only primitive data types and set of strings.

Thanks to Gson serialization, Prefser allows you to store:

  • Primitive data types (boolean, float, int, long, double)
  • Strings
  • Custom Objects
  • Lists
  • Arrays
  • Sets

In addition, Prefser transforms OnSharedPreferenceChangeListener into Observables from RxJava:

Observable<String> observe(final SharedPreferences sharedPreferences);
Observable<String> observeDefaultPreferences();

You can subscribe one of these Observables and monitor updates of SharedPreferences with powerful RxJava.

Moreover, you can observe single preference under a specified key with the following method:

<T> Observable<T> observe(final String key, final Class classOfT, final T defaultValue)

prefser_observable_diagram

For more details, examples and download instructions visit GitHub webiste of the project at: https://github.com/pwittchen/prefser.
I’ve provided sample app using Prefeser in this repository, which you can check to get familiar with the project.
Library is open source, has Unit Tests, Travis CI job and is available on Maven Central Repository. You can depend on it through Maven or Gradle.

Unit Testing Support for Android Studio 1.1

2015-02-15 Android, Unit Tests No comments

As official Android website says:

Version 1.1 of Android Studio and the Android gradle plugin brings support for unit testing your code. This feature is still considered experimental, we encourage you to try it and file any bugs you find in our bug tracker.

It’s great news, because now we can write Unit Tests with JUnit 4.12 without any “hacks” or tricky configuration, which had to be done earlier.
We can execute two kinds of tests:

  • Unit Tests – pure Java tests without Android dependencies written in JUnit, which can be run in Android Studio or CLI via Gradle without emulator or attached device. These tests have to be located in: /src/test/java/ directory.
  • Android Instrumentation Tests – tests for code, which have dependencies to Android Context or Android API and have to be executed on Android emulator or physical device like phone or tablet. These tests have to be located in: /src/androidTest/java/ directory.

Read more at: http://tools.android.com/tech-docs/unit-testing-support
and check Gradle Plugin User Guide: http://tools.android.com/tech-docs/new-build-system/user-guide

You can see examples of pure Java tests written in JUnit for Android library in one of my projects at: https://github.com/pwittchen/kirai/
(check library/src/test/java/ directory).

NetworkEvents v. 1.0.2

2015-02-15 Android No comments

I’ve recently released NetworkEvents v. 1.0.2.
In case, you don’t know it, it’s an Android library listening network connection state and change of the Wifi signal strength.
This version has small improvements:

  • Updated ping method in NetworkHelper class.
  • Detection of Internet access in WiFi network works faster and is more reliable.
  • Added example of usage of NetworkEvents with Dagger in example-dagger directory in the repository of the project.

Right now, we check Internet connection in a different way. Instead of sending HEAD or GET request to a specific website like www.google.com, we just ping 4.2.2.2 IP address. If you want to know why, read an article about 4.2.2.2: The Story Behind a DNS Legend.

Check new version of the project on GitHub: https://github.com/pwittchen/NetworkEvents
Read previous post about this library: http://blog.wittchen.biz.pl/introducing-networkevents/

Hello Kotlin!

2015-02-07 Android, Kotlin No comments

I’ve recently played with Kotlin in order to create simple “Hello World” Android app using this new, fancy language from JetBrains. Kotlin is another programming language based on JVM, which can work with Java. It’s aim is to reduce code boilerplate and create projects faster. In my simple Android project, I’ve also used KotterKnife for injecting views. I’ve also added one simple ActivityUnitTestCase. Unfortunately, I couldn’t convert it into Kotlin, because I wasn’t allowed to call constructor of inherited class using super() method. You can read related StackOverflow thread in order to view more details. In order to use Kotlin, I followed instructions from article on JetBrains blog. I’ve also needed Kotlin plugin for Android Studio.

Check out my GitHub repository: https://github.com/pwittchen/HelloAndroidKotlin
View more samples in JetBrains repository: https://github.com/JetBrains/kotlin-examples

It’s also worth mentioning that there is a project created in Kotlin by JetBrains called Spek which is a specification framework for the JVM and you can use it for writing unit tests in the JVM projects in order to get human readable output.

Furthermore, you can read document about Using Project Kotlin for Android by Jake Wharton: https://docs.google.com/document/d/1ReS3ep-hjxWA8kZi0YqDbEhCqTt29hG8P44aA9W0DM8/.

In order to view basic code constructions in Kotlin, you can browse the following repository: https://github.com/FutureProcessing/kotlin_showandtell.

Kotlin is quite interesting language, which may be some kind of Swift for Android. Basic setup of Android project in Kotlin is definitely easier than project setup in Groovy. It looks promising and I’m looking forward to hearing about new updates concerning this language.