Programming Languages in Android

The first question for an aspiring Android programmer is certainly

“What programming language should I learn to program in Android”?

The answer is less simple than any impatient programmer would like to listen: Android runs on every type of device, from the obvious mobile phones to watches, tablets, cars, televisions, home security systems and many more.

This variety of targets can be achieved only at the cost of separating tasks, in particular separating everything that is related to the user inteface from the logic of the application: this way what is the “juice” of your program stays more or less the same, while the appearance can fit different screens and different expectactions.

So there isn’t only “one” language to program in Android, but a few different languages to achieve different purposes. You don’t need to master everything at perfection to get started, but if you want to have an easier life (mind: easier doesn’t mean easy) as a developer, you certainly need to grasp something of all of them.

I will list them in three sections:

the core, i.e languages to manage the logic of your programs

the appearance, to manage the app UI.

the useful extras: languages to manage specific parts of a program, that you may or may not want to learn in depth, at first.

The core: Java and Kotlin

JAVA

Until the Google I/O of May 2017, when Google announced the support to Kotlinjava as first class and fully supported programming language for Android, Java would have been your only cup of coffee to pick for this section.

Now the things are slightly changed, but nonetheless Java remains a “must have” for any Android programmer: most, if not all, the documentation for Android is in Java, most of all non-native libraries for Android are in Java, the source code is in Java, 99.9% of books about Android are in java, most of the tutorials on the web are in Java.

Java is a statically typed, object-oriented, super-verbose programming language, the type of programming language that comes with tons of curly braces and semicolons.

Java compiles bytecode that can be then read by the target devices running a Java Virtual Machine. So you write your code once and then it can run everywhere a java virtual machine is or, as someone says, “write it once, debug everywhere”.

The portability of Java across many devices is the main reasons it was picked as main language for Android.

Created by James Gosling, launched by Sun Microsystems in 1995, Java is under the control or Oracle Coporation since 2009.

So, can you skip learning Java? I think that for a few years the answer is no, because you would have a very hard time at trying to learn, program and debug Android without understing what most of programmers have written about it, even if in the end you want to program in Kotlin.

I understand your feelings, if you come from something more friendly and less verbose than Java (almost everything modern), but look at the bright side: it’s the most popular language out there, you won’t be alone.

Mind: everything changes and Kotlin has a few features that can undermine Java in the long run.

You can learn more about Java here.

KOTLIN

kotlin_android3Kotlin is the new kid on the block: it’s concise, it’s elegant , it promises a world without Null Pointer Exceptions unless you want to throw them explicitly (maybe because you are a nostalgic javista and it reminds of your youth).

As stated on the Kotlin official web page, it is 100% interoperable with Java and Android, in fact, in the context of Java and Android, Kotlin runs on the Java Virtual Machine exactly like Java, hence its compatibility and the possibility to use Kotlin along with the existing libraries for Java and Android.

So what does change with Kotlin? It is designed to write less lines of code and leverage immutability and limit nullability, so in short “less errors”.

It seems Kotlin is quite similar to Swift, so maybe it could be a good choice for IOS developers courious to give a try to Android.

Kotlin will be shipped with Android Studio (the Android official IDE) starting with the release 3.0, now in beta, but it can also be installed as plugin in recent versions of the stable version of Android Studio.

Kotlin is developed by Jetbrains, the famous IDE-for-everything company, the same that produce Android Studio, by a team based in Saint Petersburg: the name of the programming language derives indeed from an island of the same name off the city.

More resources on Kotlin:
Kotlin and Android
FAQ on Kotlin and Android
Resources to learn Kotlin

 

The appearance: XML

xmlAll the layouts, images, menus and various views forming the UI of a common android app, are defined in XML files. Every of them it’s an xml element with attributes to tweak its appearance.

Not only strictly graphic elements are defined in XML, but everything that can change the appearance of the app, so also strings that can vary according to the language of the user: in Android you can easily configure an app to use entirily different sets of strings for English and, for example, Italian or Japanese, simply providing them in the app resources in the appropriate folder.

Furthermore, XML files are used to store resources like dimensions, colours, styles of graphic elements and so on: the possibility of customizations based on devices, language, screen sizes, orientations of the screen and so on are infinite.

XML is very similar to HTML and probably very easy for most of experienced developers, but also beginners shouldn’t be too scared of it.

The only thing that it is sometime difficult, it’s to find the list of values that a single attribute of an XLM element in android can have: this is sometimes a matter of googling and looking on stack overflow rather than looking at the documentation.

 

The useful extras: Gradle DSL (a.k.a Gradle version of Groovy), SQLite

Gradle DSL (Groovy)

GradleLogoRegAndroid Studio, the official and free Android IDE provided by Google, uses Gradle, a tool to manage builds and libraries used in a Android app.

Gradle applies plugins (first of all the Android pluging), ties your code to your libraries and signs digitally your app, so everything can be nicely packed in an APK and shipped to the app market of your choice (most probably Google Play).

Most of the files an Adroid beginner finds unsettling and mysterious are indeed gradle files, written in a language called Gradle Domain Specific Language or in short Gradle DSL. Gradle DSL is a version of Groovy a scripting language that can interoperate with Java.

The most common operations on the build.gradle files consists in adding dependencies to libraries and providing the repositories to download them: it’s something easy even for a beginner, but to grasp what Gradle is really doing it is worth to learn something more about it.

Gradle can configure different builds for different versions of an app (example: a free and a paid version of an app) that require different libraries or different resources and portions of code. Gradle can help yout to maintain “slim” the size of your app.

You can learn more about Gradle DSL here
or if you want a friendly introduction that can help you understand the principles of Gradle and a bit of Groovy quite quickly, I recommend this course from Udacity:
Gradle for Android and Java

SQLite

sqlite2Content Providers are part of the main components of the Android os. A Content Provider sits between your data and your code and tipically you use it to access data in databases. For reasons of security and modularity, you don’t want to call methods on your database directly from your code, but you call this component provided to you by Android, the Content Provider, and it in turn calls your database executing only a set of predetermined actions, so your data remain safe and sound.

If one day you want to change entirely your database, switching to something fancier, you don’t need to change your java (or kotlin) code, because the Content Provider will call always the same methods.

In theory you can use the type of databae you prefer, but Android provides you with helper classes that are meant to use SQLite.
SQLite is very light (and quite fast), so it doesn’t add too much weight to your app.
Not every programmer implements a database from scratch, many developers use ready-to-use libraries that are easier to manage, but in case you want to use only pure Android, it is worth to learn a bit of SQLite and it certainly required if you want to take the Associate Android Developer certification.

Don’t be scared of this list, and above all, don’t be scared at your first look at Android Studio: even the more simple app, it’s a little city of files, but with a bit of time you will become more familiar with it and every piece will find its right place.
There are always two ways to find your path, through your Android probems: Google and Stack Overflow, nobody can survive without them
, you are not alone.

 

 

 

 

Advertisements