Android

Android App Development: Kotlin vs Java

In this post, we feature a comprehensive article about Android App Development: Kotlin vs Java.

1. Overview

In this post, we look at how to develop Android Apps with Android Studio using Kotlin and Java. Eclipse setup for Android is also included in this article. Kotlin vs Java analysis is explained with the pros and cons of each programming language.

 

2. Android App Development: Kotlin vs Java

2.1 Prerequisites

Java 8 is required on the Linux, windows or mac operating system. Eclipse Oxygen can be used for this example. Android Studio 3.4.2 is used for the creation of Java and Kotlin applications. Gradle 5.4.1 is used by the Android studio for building the code. Eclipse 4.7 (Oxygen) can be used for Android development.

2.2 Download

You can download Java 8 from the Oracle web site . Eclipse Oxygen can be downloaded from the eclipse web site. Android Studio can be downloaded from the android studio link. Gradle is bundled with Android studio.

2.3 Setup

2.3.1 Java Setup

You can set the environment variables for JAVA_HOME and PATH. They can be set as shown below:

Java Setup

JAVA_HOME=”/jboss/jdk1.8.0_73″
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH

2.3.2 Gradle Setup

The environment variables for Gradle are set as below:

Gradle Setup

GRADLE_HOME="/opt/gradle/gradle-5.4.1/bin"
export GRADLE_HOME=$GRADLE_HOME\bin\
export PATH=$PATH:$GRADLE_HOME

2.3.3 Android Studio Setup

Download the Android studio from the website based on your operating system. Install the android studio and launch the android studio.

2.3.4 Eclipse Setup

The ‘eclipse-java-oxygen-2-macosx-cocoa-x86_64.tar’ can be downloaded. The tar file is opened by double click. The tar file is unzipped by using the archive utility. After the unzipping, you will find the eclipse icon in the folder. You can move the eclipse icon from the folder to applications by dragging the icon.

2.4 Running Gradle

You can check the version of the gradle by using the command gradle –version. The command for running Gradle is as below:

Run Command

gradle --version

The output of the executed command is shown below.

Kotlin vs Java - Gradle version
Gradle version

2.5 Running Android Studio

You can click on the Android Studio icon to launch Android Studio. Android studio screen pops up. The screen shot below shows the Android studio screen.

Kotlin vs Java - Android Studio Launch
Android Studio Launch

2.6 Running Eclipse

You can click on the eclipse icon to launch eclipse. The eclipse screen pops up as shown in the screen shot below:

Kotlin vs Java - Running Eclipse
Running Eclipse

You can select the workspace from the screen which pops up. The attached image shows how it can be selected.

Kotlin vs Java - Eclipse Workspace
Eclipse Workspace

You can see the eclipse workbench on the screen. The attached screen shot shows the Eclipse project screen.

Kotlin vs Java - Eclipse IDE
Eclipse IDE

2.6.1 Android Plugin setup on Eclipse

Go to Help menu and click on Software Updates. In the form, click on the Available software. Type the website details for ADT Plugin in the screen presented in the field work with.

Kotlin vs Java - Eclipse ADT Setup
Eclipse ADT Setup

Click on the next icon. It will launch the next screen as shown below:

Kotlin vs Java - Eclpse ADT Complete
Eclpse ADT Complete

Once it is complete, click on the next icon. It will show another screen as shown below:

Kotlin vs Java - Eclipse ADT License
Eclipse ADT License

The license screen pops up. The license agreement is accepted as shown below:

Kotlin vs Java - ADT Software Installation
ADT Software Installation

Security warning pops up. Click on Install Anyway button. The screenshot below shows the security warning.

Eclipse Android Security Warning

Message to restart the eclipse pops up as shown below. Click on the button Restart now. The setup is complete.

Eclipse Restart

2.7 Hello World in Android Java

You can start creating a new project in Android Studio by clicking on new project on Android Screen. It shows a new window as shown below:

Android Studio Empty Activity

Project details are filled out in the screen which comes up after clicking the next icon. The screen shows the project details:

Android Java Project

Build process is invoked from the screen which comes up. The popped up screen is shown below:

Android Java Build

From the menu, click on the Run app. It shows the device setup for the emulator. The screenshot is attached below which shows the device setup.

Android Device Setup

Android x86 option is selected for the virtual device. The screen shot below shows the android x86 setup.

Android x86 device sdk setup

The android sdk license screen pops up. The agreement for the license is accepted. Once the sdk is setup. It shows the message done. The screenshot below shows the “Done” message on component installer screen.

Android x86 Device Setup Finish

After the completion of the setup, emulator screen pops up after running the app. The screen shot shows the emulator screen.

2.8 Hello World in Android Kotlin

You can start creating a new project in Android Studio by clicking on new project on Android Screen. It shows a new window which is in the screen shot below:

Android Empty Activity

Kotlin Project details are filled out in the screen which comes up after clicking the next icon. Kotlin Language is selected. The screen shows the project details:

Android Kotlin Project Details

Kotlin Project Build process is invoked from the screen which comes up. The popped up screen is attached below:

Android Kotlin Build

From the menu, click on the Run app. It shows the device setup for the emulator. The screenshot is attached below which shows the device selection form.

Android Kotlin x86 Device selection

Android Accelerated x86 device is selected for the emulator. Android x86 Emulator screen pops up after running the app. The screen shot shows the emulator screen.

Android Kotlin Application

2.9 Code Conventions

The Java code conventions for classes and objects can be found at the Google site. Kotlin Code conventions can be obtained from Android site.

2.10 Best Practices

Developers need to maintain good code quality by using different architectural patterns such as MVP, MVVM, and MVM. Unit testing needs to be done for every android app. Functional UI tests need to be developed and added to the tests repository. Performance needs to be factored while writing the code. APK size can be reduced as the footprint of the mobile app is very important. Internationalization can be handled by using strings.xml.

2.11 Advantages

Java and Kotlin are statically typed languages used for programming. Android apps can be developed in any programming language. They can be executed on JVM.

2.11.1 Advantages using Kotlin

  • Kotlin is Java Virtual Machine compatible language. Kotilin, when compiled, generates bytecode in java.
  • Kotlin can be used to generate markup such as HTML and XML. Anko is UI library based on Kotlin for creating UI components.
  • Kotlin performs better than Java.
  • Kotlin has features such as lambdas to support inline functions.
  • In Kotlin, creating extensions is very easy.
  • Coroutines in Kotlin help in managing multiple threads.
  • Creating classes with properties and getters & setters for them is very easy in this language.
  • Class delegation in Kotlin helps in having multiple inheritance
  • Primitive typed variables are objects in this language.
  • Kotlin has good support for smart casting.

2.11.2 Advantages using Java

  • Java has good support for checked exceptions.
  • Implicit conversions is possible in Java.
  • Multiple inheritance in java can be achieved through interfaces.
  • Non-private and public properties can be created for classes in java.
  • Java can handle static types and members.
  • Java has a ternary operator.
  • Wildcard types can be handled in Java.
  • Annotation processing framework is an advanced feature which is available in java.
  • Java has huge community support in terms of the number of developers. Java issues can be easily solved from different forums. Java questions in Stack Overflow are over 1.52 million. Java is ranked number 1 according to TIOBE index.

2.12 Disadvantages

2.12.1 Disadvantages using Java

  • Java does not provide support for inline functions.
  • In java, object types and primitive types are different.
  • Before casting in java, you need to verify the type.
  • Java mobile apps performance is not good compared to Kotlin.

2.12.2 Disadvantages using Kotlin

  • Kotlin has no features such as checked exceptions.
  • All types are non nullable in kotlin.
  • There are no null pointer exceptions.
  • Kotlin has no support for static members.
  • Kotlin does not have a ternary operator.
  • Annotations are missing in this language.
  • Kotlin is a new programming language and few members in the community. Kotlin questions are around 21,096 as of March 2019. Beginners might have a tough time in getting answers to their issues. Kotlin is at very low rank 43 on TIOBE index.

3. Kotlin vs Java – Conclusion

Java can be used for general programming needs. First time programmer in Android can start with Java to get started. Kotlin is useful for rapid prototyping mobile applications in Android. Kotlin and Java are interoperable and Java code can be invoked from Kotlin. You can use java frameworks in Kotlin.

4. Download the Source Code

Download
You can download the full source code of this example here: Android App Development: Kotlin vs Java

Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Devcount
4 years ago

Hey,

Thanks for putting together this post on Kotlin vs Java for Android development. I particularly find your thoughts on Android Studio setup interesting.

I’m glad to find another amazing mobile app development blogger.

Cheers.

Waheed
4 years ago

I am a Java developer learning Kotlin for Android. Once you get used to Koltin syntax and approach then it is fun.

Back to top button