-
Notifications
You must be signed in to change notification settings - Fork 0
Lab 2
This lab is meant to familiarize ourselves with Android Studio's support for Kotlin. We will write some basic Kotlin functions, run those functions and observe their output, and convert some Java code to Kotlin using automated tooling.
-
Create a new Kotlin file named
hello-world.kt
within your app's primary src directory -
Write a Kotlin function named
helloWorld()
that prints"Hello World!"
to the console when invoked -
Call this new function from your
MainActivity.onCreate()
method -
Deploy your app to a device/emulator and observe the
"Hello World!"
output in theRun
tool window -
Within
hello-world.kt
, createmain()
function that invokeshelloWorld()
-
Run the
main()
function and observe the output in theRun
tool window -
Update your
helloWorld()
function to use Android'sLog.d(tag, message)
method rather than Kotlin'sprintln()
- For the
tag
parameter, you can use any String you wish. A common convention is to use the name of the class that is calling the method - For the
msg
parameter, pass"Hello World!"
- For the
-
Deploy your app again, and this time observe the output from the
Logcat
tool window- Use the search box in the
Logcat
window to search for the tag you used when invokingLog.d()
- Use the search box in the
-
Within your primary src directory, create a new Java class named
SampleClass
- Right-click on the class name declaration, and click the
Generate
option - Generate implementations for
toString()
,equals()
, andhashCode()
- Right-click on the class name declaration, and click the
-
In the
Project
tool window, right-click theSampleClass.java
file name and selectConvert Java File to Kotlin File
- Once the conversion is complete, review the changes
- You can then delete the file if you wish
-
Convert
settings.gradle
tosettings.gradle.kts
-
Convert
build.gradle
tobuild.gradle.kts
- Modify your
helloWorld()
function to take a custom greeting as a parameter - Modify your
helloWorld()
function to use a String Template to format the output message
Create a new Kotlin Scratch File, implement helloWorld()
, and invoke the function to observe the interactive output
Convert app/build.gradle
to app/build.gradle.kts
.
After you convert the file extension, you'll need to update the invocation of the include
function.
In Groovy, there is some syntactic sugar that allows calling a function with a single paramter without using any parentheses.
So, when we see include ':app'
in the Groovy version, this is actually invoking the include
function with the parameter :app
.
When converting to Kotlin then, we need to update this invocation to use the parentheses include(":app")
In the generated build.gradle
file, there is a custom Gradle task defined named "clean".
When converting a Groovy-based Gradle buildscript to a Kotlin-based build script, the syntax for defining a custom Gradle task changes.
To update the implementation of that custom task, use the following code:
task<Delete>("clean") {
delete(rootProject.buildDir)
}
In Groovy-based .gradle
files, we cause use single, or double, quotes for String literals
However, in Kotlin-based buildscript files, we must use double quotes. So when converting from .gradle
to .gradle.kts
it's often necessary to first update any strings to use double quotes
This can be done several ways:
- By right-clicking a Java file in the
Project
tool window and selecting the conversion option - Navigating to
Menu
->Code
->Convert Java File to Kotlin File
- Using the hotkey assigned to the
Convert Java File to Kotlin File
action - Use the hotkey for the
Action Lookup
action, start typingConvert Java File...
and selecting the desired action
Scratch files can be a great way to quickly prototype some code without building/running your whole project.
After you implement a function in the scratch file, don't forget to invoke the function. If you don't explicitly invoke the function, you will not see the desired output.