Jetpack Compose Android Tutorial: Project and Dependency Setup

Learn how to set up a Jetpack Compose project in Android Studio, including dependencies and configurations for efficient development.

Setting up a new Jetpack Compose project in Android Studio is an essential skill for Android developers looking to leverage the modern UI toolkit. In this article, we will guide you through the steps to create a new Jetpack Compose project, configure the necessary dependencies, and ensure everything is ready for development. Creating a New Project To start, open Android Studio and create a new project: 1. Select "New Project": On the Android Studio welcome screen, click on the "New Project" option. 2. Choose a Template: Select the "Empty Compose Activity" template. This template is tailored for Jetpack Compose and provides a good foundation for your app. 3. Configure Project Settings: - Name your project (e.g., "JetpackComposeApp"). - Choose a package name (e.g., "com.example.jetpackcomposeapp"). - Select your preferred save location. - Choose the language (Kotlin is recommended) and the minimum API level (API 21 or higher is typical for Compose). Once you’ve filled out the necessary fields, click "Finish" to create your project. Setting Up Gradle Dependencies After creating the project, you need to ensure that the correct dependencies for Jetpack Compose are included in your Gradle files. This is crucial for utilizing Compose features effectively. 1. Open build.gradle (Project): Ensure that your project-level build.gradle file contains the Google Maven repository. It should look something like this: groovy buildscript { ext { composeversion = '1.3.0' // Make sure to check for the latest version } repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.2.0' // Update if necessary // Other dependencies } } 2. Open build.gradle (Module): Next, navigate to your module-level build.gradle file (usually located in app/build.gradle). Here, you will need to add the Compose dependencies. Add the following dependencies under the dependencies block: groovy dependencies { implementation "androidx.compose.ui:ui:$composeversion" implementation "androidx.compose.material:material:$composeversion" implementation "androidx.compose.ui:ui-tooling-preview:$composeversion" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.0" implementation "androidx.activity:activity-compose:1.6.0" } Ensure that you also enable Kotlin's extension features by adding the following lines: groovy android { ... buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion composeversion } } 3. Sync Gradle: Once you have added these dependencies, click on the "Sync Now" link that appears in the top right corner of the editor. This process will download the necessary libraries for your project. Configuring the AndroidManifest.xml Next, you need to configure your AndroidManifest.xml file to ensure it aligns with Jetpack Compose's requirements. Open the AndroidManifest.xml file located in the src/main directory. 1. Set the Theme: Make sure that your app's theme supports Jetpack Compose. If you're using the Material theme, it should look like this: xml <application android:theme="@style/Theme.Material3.DayNight" ... </application 2. Set the Main Activity: Ensure that the activity declared in the manifest matches your main activity's name. It should look like this: xml <activity android:name=".MainActivity" android:exported="true" <intent-filter <action android:name="android.intent.action.MAIN" / <category android:name="android.intent.category.LAUNCHER" / </intent-filter </activity Running Your Application Now that the project is set up, it's time to run your application: 1. Build the Project: Click on the "Build" menu and select "Rebuild Project" to ensure everything is functioning correctly. 2. Run the Application: Click on the green "Run" button in Android Studio or press Shift + F10 to build and launch your application on an emulator or connected device. Once the app launches, you should see a blank screen, which indicates that your Jetpack Compose setup is correct. Conclusion Setting up a Jetpack Compose project in Android Studio involves creating a new project, configuring Gradle dependencies, and adjusting your manifest file. With these steps completed, you are now ready to start building user interfaces using Jetpack Compose. For a more detailed walkthrough, you can refer to the video tutorial. Watch the full tutorial on YouTube(https://www.youtube.com/watch?v=OHjFc73rno0).