Hey everyone I am Harsh and I assume you have set up Kotlin and android studio reading my previous articles. If you are still not done the same the links are given below, complete this before proceeding further.
Kotlin — Installation & Setup
Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is…
Kotlin — Installing & Setting up Android Studio
In this article, I will be covering how you can install and set up an android studio such that you can get started with…
In this article, we will be seeing how can you create your first kotlin project and understanding what all files are there in the project you created.
In this article I will be covering about:-
- Creating first Kotlin project
- File Structure in Kotlin Project
Creating first Kotlin Project
Once you have the android studio set up and installed open it.
Yours may be different from mine as I have flutter plugin installed in it.
Once it is loaded and opened, click on Start a new Android Studio Project.
After this, you will be asked to select a project template for the device you want to create the app for. Keep the default selected one and click on the Next button.
Now you will be asked for some details such as name, package name, the location where you want the project to be created in your system, the language you want the project to be created in, and the minimum SDK version on which the project will be working successfully.
The name which you will be entering will be the one which will be shown to the users as the app name so you must choose it wisely. The package name is used to identify your app uniquely when you are to upload your project to the play store. Save location can be anything as far as it is on your computer. You have two options for the language you want to code in. They are Kotlin and Java and by default, Kotlin is selected. The minimum SDK specifies the min android version on which your app will be working perfectly, as far as it is built correctly. You can keep the checkbox unticked and click on the finish button.
Android studio will be taking around 10–20 seconds to create and load your project.
As soon as the project will be created Gradle sync will be starting and indexing of the files which are created is done. I will be telling you more about Gradle in the later section. For now, you can consider Gradle running now is because of some missing things which are necessary for the project. You must have a stable internet connection for this to complete quickly and without errors.
Sometimes there is a case when this stops unexpectedly. You can try making a new project or switch to a new network for better results.
This is how you can create a kotlin project in android studio.
File Structure in Kotlin Project
Once the project is created successfully you will be having these many files in the different directories.
We will be looking at the ones which we need to work with while working on our project.
In the first image given above the app is the subproject which is the one we can work upon. The app name which you have provided will be the parent folder for this subproject. We can have different sub-projects inside our parent folder. Subprojects are also known as modules.
In the manifest folder, we have an AndroidManifest.xml file. The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play. This also has details of components in our app. If we ant our app to take certain permissions from the user, we can specify that necessary permission here.
Next, we have a java folder that holds the main kotlin/java files for the project/app. In the boilerplate code(initial code) we have only one kotlin file with the name of MainActivity. We can further have n number of files/classes in this.
The next two folders which have androidText and text written in parenthesis are not much useful while app building. We can skip them for now.
Next, we have a res directory that holds the resources which will be needed in our project. It has various different directories each having multiple components in them. This directory is used to store the UI things, which are visible to the user, for our app/projects. Some of the things found in this are audio files, video files, etc.
In the res directory, we have a drawable directory that has images or vectors which can then be used to form images. If you want to store any image assets then the drawable directory is your goto place.
Net we have the mipmap directory which holds the launcher icons. For those of you who do not know what launcher icons are, they are the icons that are displayed on your phone, tapping which the app opens. We save the icons in this directory. You might be thinking that a single icon is visible to the user then why are we having these many files in here?
The reason for this is pretty simple. We cannot have a fix dimensioned file forever device. The images are there to fit specific use cases.
Next, we have the values directory which has multiple files that are mainly constant in our project/app. In the initial project, we have 3 files which are colors, strings, and styles. The colors file has all the colors which are defined or used in our app. The strings files have similar things for the strings. The styles file has the styles which are defined and are used in the app/project.
The last directory which w have in the res directory is the layout directory. This has all the layout files which are connected to our MainActivity file. We can create then with the screen builder which android studio provides or we can code them out.
In the second image, we have Gradle Scripts which has different files related to Gradle but the ones which are used by us are build.gradle which has build configurations, dependencies, plugins with versions specified and external libraries which we are using. You can explore the file to see what all things are specified in there. One important thing which is necessary after editing the build.gradle file is that you need to sync the files each time you update any of the build.gradle file.
This was a walkthrough of the file structure of the files which are available in the boilerplate code for a new kotlin project.
Hope you have got to learn something new from this. If you have any doubts you can drop them down in the comments. I would be happy to help. You can connect with me on the link given below.