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
- package com.example.regularexpressionlib
-
- infix operator fun String.div (re:String) :
- Array < MatchResult > =
- Regex (re).findAll(this).toList().toTypeArrray ()
-
- infix operator fun String.rem(re:String) :
- MatchResult? =
- Regex (re).findAll (this) .toList().firstOrNull()
-
- operator fun MatchResult.get (i:Int) =
- this.groupValues[i]
-
- fun String.onMatch (re:String, func: (String)-> Unit)
- : Boolean = this.matches(Regex(re)).also { if (it) func (this)
- }
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.
- import org.junit.Assert.*
- import org.junit.Test
- class RegularExpressionTest {
- @Test
- fun proof_of_concept() {
- assertEquals("Hello Nelo " / ".*el.*").size)
- assertEquals("Hello Nelo " / ".*?el.*").size)
- var s1: String = " "("Hello Nelo" / "e[lX]").forEach {
- s1 += it.groupValues
- }
- assertEquals("[e1][e1]", s1)
- var s2: String = ""("Hello Nelo" / ".*el.*").firstOrNull()?.run {
- s2 += this[0]
- }
- assertEquals("Hello Neol", s2)
- assertEquals("el", ("Hello Nelo" % "."(el).*"?.let {
- it[1]
- }) assertEquals("el", ("Hello Nelo" / ".*(el).*")[0][1]) var match1: Boolean = false "Hello".OnMatch(".*el.*") {}
- assertTrue(match1)
- }
- }
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,
- btn.setOnClickListener(object: View.onClickListener {
- override fun onClick(V: View ? ) {
-
- }
- })
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.
- fun person (fName:String = "" , lName:String = "",
- age:Int = 0)
- {
- val p = Person(). apply { ... }
- return p
- }
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.
- val person = Person ()
- person.lastName = "Smith"
- person.firtName = "John"
- person.birthDay = "01-01-23"
- 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:
- data class Person
- (
- val fName : String,
- val lName : String,
- val age : Int
- )