Development in Kotlin

Introduction

 
Kotlin is production-ready for Android applications. Services, broadcast receivers, and content providers are reusable in the sense that it is more or less easy to extract them from a project and copy them to another project.
 

Starting a Library Module

 
Library projects contain one or more modules. Open Android studio, and create a new project and enable kotlin support. Open a new module in new project and choose library. A new kotlin class can be written inside the library module.
 
Creating the Library
  1. package com.example.regularexpressionlib    
  2.     
  3. infix operator fun String.div (re:String) :    
  4.  Array < MatchResult > =    
  5. Regex (re).findAll(this).toList().toTypeArrray ()    
  6.     
  7. infix operator fun String.rem(re:String) :    
  8.   MatchResult? =     
  9. Regex (re).findAll (this) .toList().firstOrNull()    
  10.     
  11. operator fun MatchResult.get (i:Int) =     
  12.   this.groupValues[i]    
  13.     
  14. fun String.onMatch (re:String, func: (String)-> Unit)    
  15.  : Boolean = this.matches(Regex(re)).also { if (it) func (this)    
  16. }    
The listing is different from all the listings. There is no class available in the above code. File artifact is a concept available in Kotlin. It generates a hidden class based on the package name. Any client of the library that imports by import com.example.regularexpressionlib.*. Infix operator define a division operator for strings. This division is not possible in Kotlin. It uses the regex class from the Kotlin libraries to find occurences of a regular expression in a search string and convert it to the array. The extension of a MatchResult is returned by previous operators. It allows for accessing the groups of a match by index.
 

Testing the library

 
The libraries must be tested during developing. The unit test is created because the main function is not allowed in the some cases. The below code run and test other unit test by Android studio context menu. println function is used to print some information on the test console.
  1. import org.junit.Assert.*  
  2. import org.junit.Test  
  3. class RegularExpressionTest {  
  4.     @Test  
  5.     fun proof_of_concept() {  
  6.         assertEquals("Hello Nelo " / ".*el.*").size)  
  7.     assertEquals("Hello Nelo " / ".*?el.*").size)  
  8. var s1: String = " "("Hello Nelo" / "e[lX]").forEach {  
  9.     s1 += it.groupValues  
  10. }  
  11. assertEquals("[e1][e1]", s1)  
  12. var s2: String = ""("Hello Nelo" / ".*el.*").firstOrNull()?.run {  
  13.     s2 += this[0]  
  14. }  
  15. assertEquals("Hello Neol", s2)  
  16. assertEquals("el", ("Hello Nelo" % "."(el).*"?.let   {  
  17.         it[1]  
  18.     }) assertEquals("el", ("Hello Nelo" / ".*(el).*")[0][1]) var match1: Boolean = false "Hello".OnMatch(".*el.*") {}  
  19.     assertTrue(match1)  
  20.    }  
  21. }   

Using Library

 
Build and Rebuild project the android library consist inside the folder module - build/ouputs/aaI. Create a new module in the client project in the new module and select impot.JAR/. AAR package and navigate to the .aaI use client. Add import com.example.regularexpressionlib.* to header and apply for new matching construct to use library in client.
 

Publishing Library

 
The process of publishing the library is complex and involves altering the build files in several places and using third party plu -ins and reposiory web sites. This makes the process of publishing libraries. Use the software suite Artifactory to establish a corporate Maven repository.
 

Listeners in Kotlin

 
To add an on-click listener to a button and provide implementation of interface View.onClickListener, use the below code,
  1. btn.setOnClickListener(object: View.onClickListener {  
  2.     override fun onClick(V: View ? ) {  
  3.         // do s. th.      
  4.     }  
  5. })  

Named Arguments

 
Using parameter names, it is not about argument order avoid overloading constructors for parameter combinations. To call more expressions, use the below named arguments.
  1. fun person (fName:String = "" , lName:String = "",    
  2.    age:Int = 0)    
  3. {    
  4.  val p = Person(). apply { ... }    
  5.  return p    
  6. }    

Scoping Function

 
The scoping fuction is used to structure the code by using the classes nd methods. The below code is about constructing a person.
  1. val person = Person ()    
  2. person.lastName = "Smith"    
  3. person.firtName = "John"    
  4. person.birthDay = "01-01-23"    
  5. val companycompany = company (" ACME ")    

Data Classes

 
Data classes are classes whose responsibility is to carry structured data. The declaring of data classes in Kotlin is shown below:
  1. data class Person    
  2. (    
  3.     val fName : String,    
  4.     val lName : String,    
  5.     val age : Int    
  6. )    


Similar Articles