Quantcast
Channel: Blog | Object Partners
Viewing all articles
Browse latest Browse all 93

Android Development for iOS Developers

$
0
0

Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed a lot since then, and those days are long gone. Android Studio is now the official IDE, Kotlin is growing in popularity and will seem instantly familiar to a Swift developer, and the emulators are actually quite good including features that the iOS simulators don’t even try to support (biometric authentication anyone?).

One of the big hurdles with getting into Android development can be the overwhelming amount of information to dig through. There’s a lot of history to sift through and a lot of Stack Overflow posts that are no longer relevant or are just plain incorrect. Here I’ll try to provide some helpful resources and some tips to hopefully make it easier to get started and jump into Android development if you come from an iOS background. If you’d like to skip the tips, the list of resources are linked at the end of the post.

When you first open Android Studio and start to get a test project up and running, you’ll probably be asked to choose a minimum version number you’ll support. Android Studio kindly provides a version distribution guide to help you select your minimum version. The API version numbers vs the Android version numbers can quickly get confusing, but here’s a helpful chart and additional documentation to help you navigate the versions. The version guide hasn’t been updated since May 2019 but Project Treble is improving system updates. The last few projects I’ve worked on we’ve used a minSdkVersion of 21.

You’ll also notice a section of your project called “Gradle Scripts” which contains a build.gradle file which is where you’ll manage your dependencies (similar to CocoaPods or Carthage but as a first class citizen in Android Studio). Don’t be afraid to use well supported frameworks such as dependency injection frameworks or networking frameworks. Google recommends a few such as Dagger2 (others prefer Koin), Retrofit, etc. (see Fetch Data). Be careful when adding dependencies to your gradle file. When you’re scouring the internet for help with building your app, you might see examples that suggest adding something like this to your gradle file’s dependencies section:

  1. implementation 'com.android.support:recyclerview-v7:26.1.0'

Unfortunately, you’ll see this a lot and it’s not exactly what you want. Whenever possible, you’ll want to favor androidx over the older support library, e.g.

  1. implementation 'androidx.recyclerview:recyclerview:1.1.0'

This allows you to use newer APIs with backwards compatibility support. The support library is being deprecated in favor of androidx in order to make these libraries more lean and maintainable.

Now that we have project set-up behind us, let’s get to the fun part which is choosing your app architecture and building your app! When creating a new project in Android Studio, you’re given the option to start with a “Fragment + ViewModel” option which can be a good default for a lot of apps and this template follows Google’s architectual recommendations. I’d definitely encourage reading Google’s “Guide to app architecture”. Don’t be afraid to utilize Fragments, LiveData, ViewModels, and Navigation Graphs. You get a lot of benefits from Google’s recommended architecture so avoid straying too far from their suggestions unless you have good reason to.

Finally, let’s talk about Kotlin and layout files. It’s a great language and if you’re familiar with Swift, you’ll quickly start to feel at home using Kotlin after reading through the “Getting Started” Kotlin guide. However, some things won’t be immediately obvious and you’ll have a lot of Swift habits you’ll try to find analogues for in Kotlin that just aren’t there (👋 bye bye guard statements 😢 ). The first thing you’ll probably be looking to do is unwrap nullable properties with an if let statement. One way you can do this in kotlin is by using the let closure:

  1. val fruits: List? = null
    
  2. fruits?.let {
    
  3.     print("There's fruit!")
    
  4. } ?: print("We're out of fruit...")

If you need something like a struct from Swift, check out data classes:

  1. data class User(val name: String, val age: Int)

Do you want an enum with extra functions and initializers? Maybe skip over Kotlin’s enum and take a look at sealed classes:

  1. sealed class Expr
    
  2. data class Const(val number: Double) : Expr()
    
  3. data class Sum(val e1: Expr, val e2: Expr) : Expr()
    
  4. object NotANumber : Expr()

Do you want to make a class a singleton? Check out companion objects:

  1. class MyClass {
    
  2.     companion object Factory {
    
  3.         fun create(): MyClass = MyClass()
    
  4.     }
    
  5. }

Are you looking for protocols but not finding them? Check out interfaces:

  1. interface MyInterface {
    
  2.     fun bar()
    
  3.     fun foo() {
    
  4.       // optional body
    
  5.     }
    
  6. }

Lastly, I want to touch on layout files. Layout files are XML representations of your UI that you can string together in a Navigation Graph (the Android equivalent to Storyboards). What’s great about Android layout files is that you can manipulate the XML directly without using the layout builder. I recommend tapping the “Text” tab in the lower left-hand corner when viewing a layout file so you can see the XML and the preview next to each other.

Android Studio Layout suggestion

There are many layout types that you can leverage such as LinearLayout or RelativeLayout. More recently, Android has introduced something that will be familiar to AutoLayout users called ConstraintLayout. It’s okay to use any of these and mix and match them for your use case. Sometimes LinearLayout does everything you need without the overhead of maintaining the constraints in a ConstraintLayout. If you dive into ConstraintLayout, you’ll probably find out fairly quickly there aren’t inequality constraints. However, you can achieve something similar using chains.

I hope this filled in some holes that reading through tons of documentation can leave when starting something new. I’ve linked all of the resources from the post below as well as some others I’ve found helpful. Good luck and happy coding!

Helpful Resources


Viewing all articles
Browse latest Browse all 93

Trending Articles