Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Why Using Kotlin instead of Java?

Kotlin offers a unique advantage over standard Java, especially in the case of Android:

  • Kotlin is fully interoperable with Java.
    • Java and Kotlin can be used in the same project seamless.
    • No need to wait for 3rd party libraries to be ported to Kotlin.
  • Kotlin offers advanced features which are fully compatible with older Android versions:
    • Reified Generics 
    • Lambdas
    • Function as first class citizens.
    • Advance Collection library.
    • Immutability.
  • Kotlin runtime is small (latest is about 900 KB).
  • Nullability builtin as a language feature –  instead as a constant source of Null Pointer Exceptions. 
  • It is officially supported by Google, (alongside Java, and C/C++):
    • Google now has Kotlin specific documentation.
    • Google supports Kotlin friendly android extensions. 
  • The language design favours developer productivity 
  • The language is backed by JetBrains, which means Android Studio offers excellent tooling.

Table of Contents

How to start

You need Android Studio 3.3.x, or later. You may either start with a new project or an existing project.  

...

Start with an

...

Existing Project

  1. Goto Tools|Kotlin|Configure Kotin in Project

  2. You will present with a dialog with two options. Choose "Android with Gradle":
     
  3. Choose the "app" to enable Kotlin integration (choose the latest version corresponding to the plugin on your IDE):

...

Code Block
languagegroovy
themeConfluence
titlebuild.gradle (app)
linenumberstrue
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

// omitted... 

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}


Start with a New Project

  1. Start with the new project wizard on Android studio.
  2. Choose "Kotlin" language:

Typical Integration Steps

  1. Follow the initial setup as described the Android Integration documentation, take especially care of:
    1. Dependencies
    2. The handling of native libraries.
    3. Upgrading pro-guard.
  2. Implement the GreenIDListener interface on your activity, for example:

    Code Block
    languagejava
    themeConfluence
    titleGreenIDActivity.kt
    class GreenIDActivity :  AppCompatActity(), GreenIDListener {
    	
    	override fun onGreenIDSuccess(resultCode: Int, verificatationToken: String?, verificationStatus: String?) {
        	// Your business logic here 
    	}	
    
    	override fun onGreenIDFailed(resultCode: Int, message: String?, description: String?) {
    		// Handle all failures
    		when (result) {
    			ResultCode.NO_NETWORK -> noNetwork()
    			ResultCode.BACK -> userPressedBack()
    			else -> handleGeneralGreenIDError(resultCode,message)
    		}
    	}
    }
    


    Note
    • Override is a required keyword in Kotlin.
    • When overriding a function, you do not have specify the access scope:
      • Public stays public,
      • Protected stays protected!


  3. Write a function to start the GreenID™ SDK:

    Code Block
    languagejava
    titleGreenActivity.kt
    // In GreenActivity.kt
    
    private fun startGreenID() {
    
    	val configBundle = Bundle().apply {
    		putString("accountId", "account-id-as-provided")
    		putString("baseUrl", "https://simpleui-uat1test-au.vixverify.com/") // Change this
    		putString("apiCode", "api-code-as-provided")          
    		putString("countryCode", "XX")
    		putString("mode", "onboarding")         
    	}
    
        try {
        	GreenID.getInstance(applicationContext).start(configBundle, this)	
        catch(e: GreenIDConfigException) {
    		// You have to handle this!
    	}
    }


  4. Start the GreenID™, for example on a button/view click:

    Code Block
    // onCreate: 
    
    startGreenIdBtn.setOnClickListener { startGreenID() }


Why Using Kotlin instead of Java?

Kotlin offers a unique advantage over standard Java, especially in the case of Android:

  • Kotlin is fully interoperable with Java.
    • Java and Kotlin can be used in the same project seamless.
    • No need to wait for 3rd party libraries to be ported to Kotlin.
  • Kotlin offers advanced features which are fully compatible with older Android versions:
    • Reified Generics 
    • Lambdas
    • Function as first class citizens.
    • Advance Collection library.
    • Immutability.
  • Kotlin runtime is small (latest is about 900 KB).
  • Nullability builtin as a language feature –  instead of a constant source of Null Pointer Exceptions. 
  • It is officially supported by Google, (alongside Java, and C/C++):
    • Google now has Kotlin specific documentation.
    • Google supports Kotlin friendly android extensions. 
  • The language design favours developer productivity 
  • The language is backed by JetBrains, which means Android Studio offers excellent tooling.

Kotlin Resource for Java/Android Developers

...