My week at Droidcon Zagreb and App Builders

Last week was quite intense for me. For the first time I had two conferences in two countries in 3 days. I’m definitely too old for this 😜


Droidcon Zagreb

Droidcon Zagreb started like this:

The moment they dropped us at the hotel, we got a bag full of gifts and candy!

There were plenty of interesting talks and I perceived that the general code quality level in the Android community is increasing. Rx is getting very popular: not considering my talk and Sasa Sekulic’s one, also Mouna Cheikhna from BlaBlaCar talked about how they use Rx in their Android app:

Florina Muntenescu showed how to use RxJava for MVVM:

If you are wondering, yes, more than 25% of the speakers were women. This shows how much Droidcon Zagreb is working in the right direction: kudos, folks!

Another topic than seems to be quite accepted in the community are MVP and MVVM: finally they are widely used, for better app design and to improve testability.

This is definitely my favorite takeaway from Droidcon Zagreb: Use the right tools! Don’t be afraid of trying and failing, experiment, challenge the old ways, have fun!

App Builders

I’ve been living in the Geneva area for a few months now and I’m always looking for developers to talk to and share experience with. I created the Google Developer Group Geneva and I started attending Swiss conferences: Voxxed Zurich a few weeks ago and the newborn App Builders this past week.

App Builders was quite different from the other conferences I’m used to. I usually attend Droidcons and Java oriented conferences. App Builders was all about apps, no matter on which platform — Android, iOS — or technology — Java, Swift, C#.

Being a cross platform conference, the atmosphere was different from a typical Droidcon: attendees and speakers came from different backgrounds with different experiences to share.

The technical level of the talks was high, as expected, but something that pleasantly surprised me were the non-technical talks: I saw very few of them at Android conferences. At some point, it was like being at a TEDx event: very inspiring!

The Android track was full of interesting talks and I met a few new GDEs… all Spanish 😀

This was the first edition of App Builders and I think it was a great conference. The final touch was the github repo for slides, videos and feedback https://github.com/swissmobidevs/appbuilders16 . I love it 😀

Takeaways

iOS developers seem a little bit happier than Android developers

They surely are very fond of their platform, but they suffer Xcode 😁 (however, they have an alternative with JetBrains’ AppCode). They don’t struggle to support all those glitchy Samsung devices, because… well… they have to deal with just a couple of phones and tablets. They don’t need to support old devices and OS versions because they simply don’t exists: 95% of the market runs the latest or the previous version of the OS and that’s it.

Most of them migrated to the new language, Swift, the moment it went out, while most of us are still complaining about how Kotlin is 2% slower at build time, but if you ask me, a modern language that allows you to create more elegant and more readable code it’s just worth the longer build time, because you build it once, but you have to read and maintain it for months!

A lot of iOS devs envy our Google Play Store rollout feature, alpha/beta channels and stats, but at the same time they managed to overcome the problem, as Spotify and others do:

I think we can learn a lot from the iOS community. We should complain less and act more, we should be braver, jump on the next new thing, try it, embrace it and push it to our companies if we like it, instead of waiting that the thing gets mainstream and everybody else is using it already. We must experiment more, instead of following others.

We should be able to influence the way manufacturers address the market, through a sensible feedback on how difficult it is to support 2+ years old versions of Android, on dozens of different models. If we stop supporting 3 years old Android versions, maybe manufacturers will start thinking about keeping their devices updated. Who knows?!

If we start using Kotlin today as our main language on Android, we will be able to have an impact on future improvements of the language itself, and on the enhancement of all the development tools we’d love to have and, maybe, getting Google involved, for the future of the Android community, for the greater good.

Happy coding 😊

Running Android Unit Tests before Git Push

This is a quick note about a Git hook I use to double check that everything is fine before pushing to our remote Git repo.

This covers a paranoid scenario, I know, but sometimes a refactoring can go wrong and lose pieces between production code and tests: every now and then, one of my tests goes out-of-sync and when I push to Github/CI, I get some silly error because of a typo somewhere. This trick will re-run every Unit Test when I give git push and will actually push only if tests pass.

The Script

Create a file named pre-push and place it in your_repo/.git/hooks . Be sure the file is executable: chmod +x pre-push

Copy this snippet into the file:

https://gist.github.com/hamen/f080a06b6283c61da637cf81388f4b4e

This uses ./gradlew clean test, but you can use ./gradle check or any other Gradle task you use to run your tests.

Conclusions

Git hooks are quite handy in lots of scenarios and there are plenty of them. Check out http://githooks.com/.

Personally, I find pretty useful my pre-push hook and, obviously, lolcommits! 😀

Android Library AAR and Javadoc

As an Android developer, I’m used to ask Android Studio/Intellij Idea for documentation constantly. I have even replaced the shortcut: now, it’s F1, the old-fashion help key. When I want to know about a method or a class, I hit F1 and the JavaDoc shows up: smooth. How does this works? Story time!

The Catapush user

Could you point me to the new method when a method gets deprecated. It’s easier for me to replace the old API this way.

And I was like: WTF!? I hate when this happens to me and that’s why I’m taking the time to save the hassle to Catapush users!!! The method is @Deprecated and there is a nice “Please, use this other method” in the Javadoc. I see it! It’s here! Why is he telling me there is no JavaDoc!? And then it hit me…

ME: Are you saying that AAR files don’t contain JavaDoc?

HardReality: Yes, Ivan, little naive Android developer

Most of the time, during your Android development, the documentation shows up because you are using open source libraries. Our IDE retrieves the sources.jar file under the hood and shows the JavaDoc directly from the source code.

Catapush is closed source (for now 😉 ) so we are only distributing the AAR file using Jitpack. AAR means no JavaDoc in the library. No sources.jar means no JavaDoc retrieved from the source code.

HTML JavaDoc

As first step, I tried to create an HTML version of the JavaDoc just for the sake of having it published. With Jitpack, this is a no-brainer. Simply add this to your build.gradle:

task javadoc(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += configurations.compile
}

// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives javadocJar
}

Push your library to Github, build it with Jitpack and you will have your HTML doc at https://jitpack.io/com/github/USER/REPO/VERSION/javadoc/

Problem solved! No! I find HTML doc unpractical. I want it in my IDE.

The Interface trick

The only way to have doc in the IDE is to distribute it as source code. When you work with closed source libraries, this is not an option, but… you can publish a part of the source code.

I extracted interfaces for every class that is part of the public API of the library and I documented the interfaces, instead of the implementations.

public interface ICatapush {

/**
* Initializes Catapush
*
*
@param context Application Context. Catapush should be initialized in your class that extends Android Application,
* passing <b>this</b> as {
@link Context};
*
@param callback You can receive a callback call when the initialization is completed.
*/
void init(@NonNull Context context, @NonNull Callback<Boolean> callback);
/**
* Starts Catapush. This is the final method of the chain.
* Be sure to call this after {
@link #setAppKey(String)},
* {
@link #setUser(String, String)}, {@link #setPush(Notification)} and {@link #setGcmSenderId(String)}.
*/
void start();
}

Once every interface was documented, I could publish the sources.jar via Jitpack, with a few lines in my build.gradle:

// build a jar with source files
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'

include("**/ICatapush.java")
}
artifacts {
archives sourcesJar
}

Sync, push and build and you are done! Every time you import Catapush in your project, Android Studio/Intellij Idea will retrieve the sources.jar under the hood and, when you ask for documentation, the magic will happen:

Javadoc copied from the Interface to the implementation

Our IDE is smart enough to inherit the Javadoc from the interface when we ask it on the actual implementation.

Conclusions

Extract a few interfaces, add your Javadoc, add a few lines to your Gradle file and you are good to go. Documentation is important for your users and your co-workers: don’t let them down.

I don’t really know why there is no support for documentation in the AAR format and, right now, it doesn’t matter anymore:

Android community, like Nature, always finds a way.

Installing Git Cola on OSX

There are tons of Git GUIs out there, ready to be installed with a couple of clicks. However, over the years I have always used Git Cola because it seems to be the only one providing one specific feature I use very often:

Stage Selected Lines

Many GUIs provide only something like this:

Stage Diff Hunk

Hunks often are not small enough for my way of committing changes. I don’t like committing whole files or huge chunk of code. I prefer small commits: easy to review, easy to `git bisect` if something goes south, easy to `git cherry-pick` if necessary. I call them micro-commits and they are so radicated in my way of working that I simply cannot working without them.

Said that, Git Cola is a bit tricky to install on Mac OSX. Here are the necessary steps for you and future-me 🙂

Install MacPorts

Download and install MacPorts for El Capitan:
https://distfiles.macports.org/MacPorts/MacPorts-2.3.4-10.11-ElCapitan.pkg

Install PyQT

Once you have installed MacPorts, you need to install PyQT. Fire up a Terminal and run:

sudo port selfupdate
sudo port install py34-pyqt4

Install Git Cola

We got Python dependencies. We are almost there. A couple of commands more:

sudo pip install sphinx
brew install git-cola
git clone git://github.com/git-cola/git-cola.git && cd git-cola
make git-cola.app
rm -fr /Applications/git-cola.app && cp -r git-cola.app /Applications

Done. We can now run cola from any folder containing a Git repo and the GUI will fire up.

Tuning

Edit your ~/.bash_profile file and add the following line:

alias cola=’cola &’

This will start cola without locking the terminal.

Text replacement, ASCII emoji and Slack

Humans are creatures of habits and programmers… well, even more! Working remotely, I spent lots of hours interacting with people using a keyboard:

https://giphy.com/gifs/EtB1yylKGGAUg/html5

Not like that! I mean typing! Nowadays, typing emotions is a common thing: we use emojis. They are everywhere: Slack and Facebook have them. Not Twitter (Yeah, I know! I just can’t get over it!”).

You simply write 🙂 and you get a nice smiling face (Rocket science, eh, Twitter?)

Slack pushed it even more. They have a ton of icons and you can even add yours, but… BUT the keywords to trigger the icon replacement must start and end with “ : “, like :+1:, :-1:, :sweat:. It gets worse if you prefer other skin colors: :+1::skin-tone-5:.

I continuously type +1 or -_-’ and I want them to be translated in emoji, Lucky me, OSX has a text replacement feature:

System Preferences -> Language & Region -> Keyboard preferences -> Text

Keyboard preferences -> Text

Once you have your favorite ASCII emoticons ready to be replaced, you go to Slack and enable the Text Replacement:

Enable text replacement

Notes

With the same approach, you can even easily censor yourself, replacing words like f*ck, sh*t and so on 🙂

And now, go to setup some office prank for your colleagues >:)

PS

Fellas at https://medium.com/developers, we could use some emoji replacement here, too 😉

ADB over WiFi

A few days ago, I built a device holder to place as many devices as possible on my desk without losing my sanity (https://medium.com/@hamen/first-hardware-project-of-2016-device-holder-4ef25670c507).

One more step in that direction is solving the charging issue:

N devices, with turned on screen, wifi and gsm data vs Powered USB Hub

Poor USB Hub cannot win against a Nexus 10: that thing sucks more energy than a black hole.

My solution is a plethora of USB chargers and WiFi ADB connection. We just need Android SDK and its ADB. First thing: list the devices

adb devices -l

List of devices attached

With the device list, we can now operate on any particular device. We need to setup the connection port first:

adb -s DEVICE_ID tcpip 5555

Setting up the port

Once the port is properly configured, we can connect to the device. We need to know the device current IP address and we can find it in:

Settings -> Wi-Fi -> Advanced (in toolbar menu)

We can now connect to the device like this:

adb -s DEVICE_ID connect DEVICE_IP

Connected!

Ready to go! Fire up Android Studio and run your app.

WiFi connected device

Notes

If you want to go back to USB ADB, you can reboot your device or use:

adb usb