Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Android Project folder Structure in Kotlin

Android Studio provides a default structure for projects, which is designed to improve the organization of resources, source code, and configurations. Understanding this structure is crucial when navigating through projects or integrating with tools and libraries. Here's a basic overview of the standard Android project folder structure with a focus on a Kotlin-based project:

1. Set up a new project:

  • Open Android Studio and create a new project with the "Empty Activity" template.
  • Select Kotlin as the programming language.

2. Explore the Android Project Structure:

When you create a project, Android Studio presents you with a couple of views for your project files - 'Android' (default) and 'Project' view. We'll discuss the structure from the 'Android' perspective, which is more concise and tailored for Android development.

  • Manifests:

    • AndroidManifest.xml: Contains essential information about the app, including components (like activities) and permissions.
  • Java:

    • Contains all Kotlin and Java source files.
    • Your main application code will be inside the package that usually matches your application's domain. E.g., com.example.myapp.
    • MainActivity.kt: This is where the main activity's Kotlin code resides.
  • res:

    • This folder contains all resources for the application.
    • drawable: Graphics and XML drawable files.
    • layout: UI layouts (e.g., activity_main.xml).
    • mipmap: Launcher icon images.
    • values:
      • colors.xml: Color definitions.
      • strings.xml: String values, making localization easier.
      • styles.xml: Application styles and themes.
    • menu: XML files defining application menus.
    • raw: Raw resource files, like audio.
  • Gradle Scripts:

    • Contains all gradle related files.
    • build.gradle (Module: app): This file contains configurations and dependencies specific to the app module.
    • build.gradle (Project: ProjectName): This file contains configurations for the entire project, like the Kotlin version and classpath for plugins.
    • settings.gradle: Contains settings for the Gradle build, like which modules to include.
    • gradle.properties: Contains properties for the Gradle build, like JVM arguments.
    • gradlew & gradlew.bat: These are Gradle Wrapper scripts to ensure that you use the same version of Gradle across builds and machines.
  • .idea: This folder is generated by Android Studio and contains configuration data and settings for the IDE specific to your project.

  • app: This folder contains the main module for your app. Larger projects can have multiple modules.

  • libs: Contains private libraries or jars you might add.

  • build: Contains output files from the build process.

3. Project View Perspective:

Switching to 'Project' view (from the dropdown on the top-left) will give you a more file-system-oriented perspective. This view exposes more files:

  • .gitignore: Specifies files or folders that should be ignored by Git (if using Git as VCS).
  • gradle: Contains the full Gradle wrapper, which ensures the use of the same Gradle version across builds and machines.

Conclusion: The Android project structure is designed to keep things organized, especially as your project grows in complexity. Familiarizing yourself with this structure helps you navigate and manage resources, source code, and configurations effectively.

Creating modular Android projects in Kotlin and folder structure

Description: Creating modular Android projects involves breaking down your application into independent modules, each serving a specific purpose. Common modules include app (presentation), domain (business logic), and data (data access).

Folder Structure:

- app
  - src
    - main
      - java
      - res
- domain
  - src
    - main
      - java
- data
  - src
    - main
      - java

Recommended package structure for Android apps in Kotlin

Description: The package structure should follow a logical and hierarchical organization. Common packages include ui for user interface components, model for data models, util for utility classes, and di for dependency injection.

Package Structure:

- com.example.myapp
  - di
  - model
  - ui
    - activities
    - fragments
  - util

Separating resources and code in Android project folders with Kotlin

Description: Separate resources and code to keep the project organized. Place Java/Kotlin code in the src directory and resources (layout files, drawables) in the res directory.

Folder Structure:

- app
  - src
    - main
      - java
      - res

Common conventions for naming and structuring files in Android with Kotlin

Description: Follow common naming conventions, such as using lowercase letters for package names and camelCase for class names. Keep related files together, such as placing a layout file next to its corresponding activity or fragment.

Example:

- com.example.myapp
  - ui
    - activities
      - MainActivity.kt
      - activity_main.xml
    - fragments
      - MyFragment.kt
      - fragment_my.xml

Organizing layout files and resources in Android projects using Kotlin

Description: Organize layout files and resources based on features or components. Group related resources in the res directory, and use subdirectories for drawables, layouts, and values.

Example:

- app
  - src
    - main
      - res
        - drawable
        - layout
          - activity_main.xml
        - values
          - colors.xml

Applying clean architecture principles to Android project structure in Kotlin

Description: Apply clean architecture principles to structure your project, separating it into layers like presentation, domain, and data. This promotes a clear separation of concerns and testability.

Folder Structure:

- app
  - src
    - main
      - java
        - com.example.myapp
          - di
          - presentation
            - activities
            - fragments
          - domain
            - usecase
          - data
            - repository
            - datasource