CPS251 Android Development by Scott Shaper

Common Problems and Solutions

If you're working with Jetpack Compose and Material Design, you will run into some confusing errors or unexpected behavior. Don't worry - it will happen alot! This guide will help you understand and fix the most common problems you might encounter.

Import-Related Issues

"Cannot resolve symbol 'Material3'"

This is probably the most common error you'll see when starting with Material Design. Here's what it looks like:


@Composable
fun MyScreen() {
    Button(onClick = { }) {  // Error: Cannot resolve symbol 'Button'
        Text("Click Me")     // Error: Cannot resolve symbol 'Text'
    }
}
        

Why This Happens: Your project is missing either the Material3 dependency or the import. It's like trying to cook with ingredients you haven't bought yet!

How to Fix It:

  1. First, check your build.gradle file (app level) and make sure you have:
  2. 
    dependencies {
        implementation "androidx.compose.material3:material3:1.1.2"
    }
                
  3. Then, add these imports to your file:
  4. 
    import androidx.compose.material3.Button
    import androidx.compose.material3.Text
                

Pro Tip: When you see a red squiggly line under a component, try pressing Alt+Enter (or Option+Enter on Mac). Android Studio will usually suggest the correct import!

"Ambiguous import" Error

Sometimes you'll see an error like this:

Ambiguous import. Both 'androidx.compose.material.Text' and 'androidx.compose.material3.Text' match.

Why This Happens: You're trying to use Text, but Android Studio doesn't know if you want the Material 2 version or the Material 3 version. It's like having two friends named "John" and not specifying which one you're talking about!

How to Fix It:

Common Mistake: Don't mix Material 2 and Material 3 imports in the same file! Pick one version and stick with it. Material 3 is the newer, recommended version.

Material Design Theme Problems

Theme Not Applying Correctly

You've set up your theme, but your components aren't using the colors and styles you defined:


@Composable
fun MyApp() {
    MaterialTheme(
        colorScheme = lightColorScheme(
            primary = Color(0xFF6200EE),
            secondary = Color(0xFF03DAC6)
        )
    ) {
        // Your components here
    }
}
        

Why This Happens: There are a few common reasons:

How to Fix It:

  1. Always use theme colors instead of hardcoded ones:
  2. 
    // Do this:
    Text(
        text = "Hello",
        color = MaterialTheme.colorScheme.primary
    )
    
    // Not this:
    Text(
        text = "Hello",
        color = Color(0xFF6200EE)
    )
                
  3. Make sure your theme wraps everything:
  4. 
    @Composable
    fun MyApp() {
        MaterialTheme {
            // All your app content should be here
            Navigation()
        }
    }
                

Gradle and Dependencies

Gradle Sync Fails

You're trying to add Material Design, but Gradle sync fails with errors like:

Could not resolve androidx.compose.material3:material3:1.1.2

Why This Happens: Usually because of version mismatches or missing repositories.

How to Fix It:

  1. Make sure you have the Google Maven repository:
  2. 
    repositories {
        google()
        mavenCentral()
    }
                
  3. Check your Compose version compatibility:
  4. 
    // In your build.gradle (project level)
    buildscript {
        ext {
            compose_version = '1.5.4'
        }
    }
    
    // In your build.gradle (app level)
    dependencies {
        implementation "androidx.compose.material3:material3:$compose_version"
    }
                

Component-Specific Issues

Material Components Not Displaying

Your Material components are invisible or not showing up correctly:


@Composable
fun MyButton() {
    Button(onClick = { }) {
        Text("Click Me")
    }
}
        

Common Causes:

How to Fix It:


@Composable
fun MyButton() {
    Button(
        onClick = { },
        modifier = Modifier
            .fillMaxWidth()
            .height(48.dp)
    ) {
        Text("Click Me")
    }
}
        

Migration and Compatibility

Moving from Material 2 to Material 3

You're trying to update your app to use Material 3, but things look different or broken:

Key Changes to Watch For:

Migration Steps:

  1. Update your dependencies to Material 3
  2. Replace Material 2 imports with Material 3
  3. Update your theme to use Material 3 color scheme
  4. Test each screen for visual changes

Performance Issues

Slow Compilation

Your app takes forever to build, especially after adding Material Design:

How to Speed Things Up:

Real-World Examples

Fixing a Common Layout Problem

Here's a before and after of a common layout issue:

Before (Problematic):

@Composable
fun MyScreen() {
    Column {
        Text("Title")
        Button(onClick = { }) {
            Text("Click Me")
        }
    }
}
        
After (Fixed):

@Composable
fun MyScreen() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Title",
            style = MaterialTheme.typography.headlineMedium,
            modifier = Modifier.padding(bottom = 16.dp)
        )
        Button(
            onClick = { },
            modifier = Modifier.fillMaxWidth()
        ) {
            Text("Click Me")
        }
    }
}
        

Remember These Key Points: