Where Should I Put Kotlin Code in an Android Project?

Based on the results of a poll of 371 Android developers, the majority of responses endorse using “src/main/java” for Kotlin code in Android only projects.  My answer is always it depends, but let’s see why you would use “src/main/java” the majority of the time, and why you’d want to use “src/main/kotlin” in some cases.

Why src/main/java?

79% of people say it should go in “src/main/java”.  Here’s why:

  • The official documentation Android Developer Documentation recommends it, and shows additional configuration you must add to support a “src/main/kotlin” source set.
    android {
       sourceSets {
           main.java.srcDirs += 'src/main/kotlin'
       }
    }
    
  • When you create a new project Kotlin project in Android Studio, it uses “src/main/java” as the location for your Kotlin code.
  • I personally really like it for the reason that all of your source is in a single place, making it easier to find related code in a project mixed with Java.  This can help maintain consistency to have all your code in one location for any project that has or had Java code in the past.
  • There are no longer issues with the Kotlin Gradle Plugin which prevented you from mixing Kotlin and Java code in the same directory.

Why src/main/kotlin?

21% of people say it should go in  “src/main/kotlin”.  Here’s why:

  • Some projects like the clean separation of the languages into different directories.  While this is nice idea, from a pragmatic standpoint, having all your code in a single place is the ideal for discovery and browsing in my opinion.
  • Some projects are 100% Kotlin, so having this directory structure clearly calls this is a single to only write code in Kotlin.  This comes with the configuration overhead of each module, but if you share Gradle config between multiple projects, you’d only have to do this once.
  • OkHttp does it, but realize this is because it’s a Kotlin only project that is meant to be used on multiple platforms (including the JVM & Android).

Kotlin Source Sets beyond Android

The answers above only apply for Android only projects.  Kotlin is more than just a programming language used on Android.  It’s being used for Kotlin Multiplatform to create code that runs on iOS, it’s used in Kotlin Native to get blazing fast, low level speed and Kotlin JS to run in your Node server or web application.

You can see how some Kotlin Multiplatform projects are using source source sets such as “commonMain” where common code used for all platforms is kept, and then you can have additional source sets like “iosMain” for platform specific implementations.

I’m not going to pretend that I’m a Kotlin Multiplatform expert, but check out the official documentation, and the KaMPKit Github Repo from TouchLab to see a sample project of Kotlin Multiplatform.

Toggling to your Debug Activity

Toggling to your Debug/Diagnostic Activity made easy.
android:taskAffinity=”com.handstandsam.app.diagnostic”

AndroidManifest.xml, Activity declaration, add a taskAffinity property…

<activity
    android:name="com.handstandsam.app.diagnostic.DiagnosticActivity"
    android:icon="@drawable/ic_launcher_diagnostic"
    android:label="Diagnostic"
    android:launchMode="singleTask"
    android:taskAffinity="com.handstandsam.app.diagnostic"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>    

I just had a breakthrough regarding the definition of debug activities in your app. If you specify a different taskAffinity for your debug activity, it’ll show appear in your task launcher like a separate app allowing you to toggle back and forth, making it super easy to jump around.

Thanks to the library Chuck for using this method so I was able to figure out! https://github.com/jgilfelt/chuck/blob/master/library/src/main/AndroidManifest.xml

Determine TLS Version & Cipher Suite Used in OkHttp Calls

Working with SSL Handshakes is no fun (to an application developer like me), but with an OkHttp 3 Interceptor, and the nicely typed TlsVersion and CipherSuite objects, it becomes a lot less painful.

If you want to know which TLS version and Cipher Suite was ACTUALLY used for a specific request, use this OkHttp 3 Interceptor:

 

You’ll get Logcat output that looks like this:

OkHttp3-SSLHandshake: TLS: TLS_1_2, CipherSuite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA

With OkHttp 3, you can specify what TLS version(s) and Cipher Suite(s) you want your calls to support (But it only works on Lollipop and higher).

Check out the OkHttp 3 documentation on HTTPS for more info.

Note: You can get newer versions of TLS (Like 1.1 and 1.2) working on < Lollipop, but that has to be done outside the OkHttp 3 configuration unfortunately. See how to do that here.  Maybe this is a good reason to bump your minSdk to 21 🙂

Fragmented Podcast Intro Song Lyrics, Performed by Blueprint

The Fragmented Podcast which focuses on Android Development has a new intro song by Blueprint which is pretty nerdy/awesome. It’s new as of February 2017 and is first featured in Episode 72 about app shortcuts.  In case you want to sing along, here are the lyrics:


Null check, inject and bind, APK, lifecycle and design.
Null check, inject and bind, APK, lifecycle and design.
Null check, inject and bind, APK, lifecycle and design.
Null check, inject and bind, APK, lifecycle and design.
It’s time.

Continuous Integration for Android Using Jenkins

Dan Jarvis and I co-presented a talk entitled “Continuous Integration for Android Using Jenkins” at the Android Summit 2016 on August 26, 2016 in McLean, Virginia.  Here are our slides.  Video should be available in the coming weeks and will be posted here and on Twitter.

Some Highlights:

  • Automate everything (If you can script it, do it)
  • “There’s an app for that” on a smartphone, “There’s a plugin for that” on Jenkins
  • The Android Emulator is great for scalability in CI, it’s fast and you can configure it in many ways
  • Shard for speed

Lots of Upcoming Speaking Engagements!

I’m excited to announce I’ve been accepted to talk at a bunch of upcoming events.


Espresso: A Screenshot is Worth 1,000 Words

Do your product owners, designers and the people that pay you understand what in the world your Espresso tests are doing and why they are valuable? You’ve spent so much time and effort writing these tests and your whole team deserves to get the most benefit out of them. In this talk you’ll learn how to setup your Espresso tests to take programatic screenshots, and leverage the Robot pattern of testing for clean, readable, and maintainable tests. You’ll also learn guidelines on when it’s appropriate to write Espresso tests instead of Robolectric or Unit tests, and how to leverage mock data to make your Espresso tests run with Tesla-like speed.

I have architected the Espresso test setup for our Capital One Wallet Android team and helped execute our ongoing continuous integration efforts. We’ve seen a 4x+ speed improvement over Appium, have more maintainable tests, and now have visibility for anyone to look into our test coverage.


“Continuous Integration Tips & Tricks for Android”
This is a co-talk with Dan Jarvis, the tech lead for Capital One Wallet for Android.

Network Calls from Android Device to Laptop over USB via ADB

I’ve seen a few articles about using the “adb reverse” command which allows you to make specific network calls to your laptop over USB, but when explaining this concept to others, they had a hard time visualizing how it worked.  I’ve put together this post to help illustrate how this works and spotlight some use cases.

Android’s “adb reverse” command is available in Lollipop and higher versions of Android (Platform 21+) and it allows you to access a server running on your computer from your Android device over USB without any network (WiFi or Cellular).  This is done through a technique called a reverse proxy.

What’s a reverse proxy? (via Wikipedia)

A type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client as though they originated from the proxy server itself.

Use Case 1 : Android -> Server running on laptop

Android Device (localhost:8080) -> Server running on Computer (localhost:8081)

adb reverse tcp:8080 tcp:8081

Screen Shot 2016-02-01 at 2.42.15 PM

Use Case 2 : Android -> WireMock on laptop -> Server accessible only on laptop (i.e. VPN)

Download the WireMock JAR and run it using the following command in a Terminal

java -jar wiremock-standalone-2.0.8-beta.jar --port=8081 --proxy-all="https://internal.myapp.com"

Android Device (localhost:8080) -> WireMock running on Computer (localhost:8081)

adb reverse tcp:8080 tcp:8081

This allows you to have your physical Android device access resources that are only available through your laptop.

When you’re done, turn off all reverse proxy port mappings:

adb reverse --remove-all

Sources:

Contactless Payments on Capital One Wallet for Android!

It’s been super hard to not share this info as I’ve done presentations on Host Card Emulation (HCE) presentations, but on October 14th the feature I’ve been leading Android development efforts on launched! Capital One Wallet – Google Play Store. This is a project I’ve been a part of for almost a year and it’s so cool to see it in the wild and right along side of Android Pay, Samsung Pay and the rest. It supports Visa and Mastercard and is pretty awesome. Capital One was the first bank in the US to support contactless payments in an Android app and it’s super exciting to have been a part of that.

I tried to do my with presentations but it always killed me that I wasn’t able to share what we were doing. I did feel like I was able to explain how HCE works on Android, but wasn’t able to give much more info at the time. Check out my presentation video and slides from Droidcon NYC.

Tap to Pay Screen
Tap to Pay
Payment Sent Screen
Payment Sent

iOS 101 for Android Developers – AnDevCon Boston 2015

by Stephen Barnes (@smbarne), Fitbit iOS Engineer

Sample Project used in talk: https://github.com/smbarne/AndroidForiOS

IMG_20150730_154830

By learning iOS and coming over to the dark side, you can learn from the iOS app, and also help do a quick implementation of a feature you already implemented on Android.

Start Developing iOS Apps Today tutorial

Hamburger menu on Android, not on iOS.

UINavigationController to Push/Pop UIViewControllers
* Use this by default for nested content

Other Extensions
* Custom Keyboards
* Photo/Media Editing
* Storage Provider
* Document picker
More: https://developer.apple.com/app-extensions/

Swift 2.0 was announced for iOS9 and Xcode 7.

App Store approval can take up to 2 weeks. On Android, it’s up within 12 hours.

——
Project Structure
* Swift uses modules
* Note: Objective-C has no namespacing so use class prefixes (recommended to be at least 3 characters, and not something like “FB”)
info.plist is required and similar to AndroidManifest.xml n Android
* No build.gradlew. All build info in *.xcodeproj

Asset Bundles:
* There are 3 buckets. Just @1x, @2x, and 3x
* The iPhone 6 is the first device to have @3x and a non-1:1 or 2:1 pixel ratio.

UIViewControllers <-> Activities (UI Building Blocks)
Fundamental view-management model for all iOS apps.

iOS is strongly architected around the MVC framework.

Fragments don’t exist on iOS, and this sort of concept doesn’t really exist.  You could do it if you use the Child View Controller Containment, but it’s not recommended unless you have significant iOS experience since this is not a common patter.
ListFragment would be done with a UITableViewController on iOS.

Closures are Apple’s Lambda implementation blocks.  These are similar to anonymous functions or AsyncTasks

Main View Components

  • PagerAdapter or NavigationDrawer -> UITabBar
  • ListView -> UITableView
  • GridView -> UICollectionView

iOS Interface Builder is your friend.  AutoLayout is almost a must.

Storyboards are powerful and worthwhile.

Core Data is a ORM with some pluses and minuses.  Designed to be easy to use and reliable.  It is the defacto way to do persistent data as it also has migrations, etc.  Contains grouping, filtering and organizing data in memory and in the UI.

Equivalent libraries:

  • AFNetworking <-> Volley
  • MagicalRecord <-> ActiveAndroid
  • SDWebImage <-> Picasso

MasterCard APIs: Payments, Security and Data – AnDevCon Boston 2015

by: Brien Buckman

global

What is MasterCard?
“We are more than credit cards, we are payment experts. We process transactions 2x quicker than the blink of an eye.”

“We <3 Android”

MasterCard is part of Android Pay, but he can’t speak much about that.

MasterCard developer zone
You can play with all of their SDKs and each one has a Sandbox available. There is also documentation and support forums.  Go to “Services” and there are about 20 on there.

MasterPass – Digital Wallet Network
A digital Wallet Service that allows consumers pay with any payment card anywhere on any connected-device.

Simplify Commerce – Accepting Payments
Makes it easy for small businesses to accept payments.

“Low, Fixed Pricing – SDKs – Plugins

Merchant Identifier – Rich Merchant Data

“Provides rich merchant descriptions based on a merchant’s acquiring name.” This makes the credit card statement entry human readable instead of “JOE COFFE 342”, it would be “Joe’s Coffee on 2nd Street”, etc.

*NEW* – “Places” aggregate anonymous spend data to give you relevant merchant-focused information.”
“Say you are traveling to the middle of no where Utah, and you want a good restaurant. They use anonymous transaction data to see what the most popular payments are.

Masters of Code” is their global hackathon. Running a lot of these events.  One is in NYC November 7-8 this year.  Get one quick, because it will sell out.

Locations API” – For determining where MasterCard is accepted.