coderain blog

How to Fix AWS Lambda ClassNotFoundException When Uploading Android Studio JAR File

AWS Lambda is a serverless computing service that lets you run code without provisioning or managing servers. It’s popular for building event-driven applications, but integrating it with Android Studio—typically used for Android app development—can sometimes lead to unexpected errors. One common issue developers face is the ClassNotFoundException when uploading a JAR file built in Android Studio to AWS Lambda.

This error occurs when Lambda cannot locate the specified handler class in your JAR file. While Android Studio excels at building Android apps, its default configurations are optimized for mobile development, not serverless Java functions. In this blog, we’ll break down why this error happens and provide a step-by-step guide to fix it, ensuring your Lambda function runs smoothly.

2026-01

Table of Contents#

  1. Understanding ClassNotFoundException in AWS Lambda
  2. Common Causes of the Error
  3. Step-by-Step Solutions
  4. Preventive Measures
  5. Conclusion
  6. References

Understanding ClassNotFoundException in AWS Lambda#

AWS Lambda requires a handler class to execute your code. This class must be specified in the Lambda configuration (e.g., com.example.MyHandler::handleRequest). The ClassNotFoundException error indicates that Lambda cannot find this class in the JAR file you uploaded.

Example error message:

{  
  "errorMessage": "Class not found: com.example.MyHandler",  
  "errorType": "java.lang.ClassNotFoundException"  
}  

This error is not specific to Lambda itself but rather a mismatch between how Android Studio builds JAR files and what Lambda expects. Let’s explore why this happens.

Common Causes of the Error#

Before diving into fixes, let’s identify the root causes:

  1. Incorrect Handler Configuration: The handler string in Lambda (e.g., package.Class::method) may have typos or mismatched package/class names.
  2. Invalid JAR Structure: Android Studio may build a JAR with extra layers (e.g., Android-specific directories) or missing the handler class.
  3. Android-Specific Dependencies: JARs built from Android modules include mobile dependencies (e.g., androidx libraries) that Lambda doesn’t support, leading to conflicts.
  4. ProGuard/Minification: Android Studio’s code-shrinking tools (like ProGuard) may accidentally remove the handler class if not configured properly.
  5. Using an Android Module Instead of a Java Library: Android Studio’s default "App" module is optimized for APKs, not plain Java JARs.

Step-by-Step Solutions#

Let’s resolve the error with these actionable steps:

1. Verify Lambda Handler Configuration#

The first check is to ensure your Lambda handler string matches the actual package and class name in your JAR.

How to Fix:

  • In the AWS Lambda console, navigate to your function → ConfigurationGeneral configurationHandler.
  • Confirm the handler format: [PackageName].[ClassName]::[MethodName] (e.g., com.myapp.LambdaHandler::handleRequest).
  • Double-check for typos (e.g., uppercase/lowercase letters, missing dots).

2. Inspect the JAR File Structure#

Lambda expects a flat JAR structure where classes are organized by package (e.g., com/myapp/LambdaHandler.class). Android Studio may build JARs with extra layers or missing classes.

How to Inspect the JAR:

  • Locate your JAR file (typically in YourProject/module-name/build/libs/).
  • Use the jar command-line tool to list contents:
    jar tf your-lambda.jar  
  • Look for your handler class (e.g., com/myapp/LambdaHandler.class). If it’s missing or nested under unexpected directories (e.g., BOOT-INF/classes/), the structure is invalid.

3. Fix Android Studio Build Configuration#

Android Studio is designed for Android apps, so using an "Android App" module to build a Lambda JAR is problematic. Instead, create a Java Library module, which produces plain Java JARs.

Step 3.1: Create a Java Library Module#

  1. Open your Android Studio project.
  2. Go to File → New → New Module.
  3. Select Java or Kotlin Library (under "Java") → Click Next.
  4. Name the module (e.g., lambda-function) and set the Java package name (e.g., com.myapp).
  5. Click Finish.

Step 3.2: Write Your Lambda Handler#

In the new Java Library module:

  • Navigate to src/main/java/[package-name] (e.g., com/myapp).
  • Create a new class (e.g., LambdaHandler) with a handleRequest method. Example:
    package com.myapp;  
     
    import com.amazonaws.services.lambda.runtime.Context;  
    import com.amazonaws.services.lambda.runtime.RequestHandler;  
     
    public class LambdaHandler implements RequestHandler<Object, String> {  
        @Override  
        public String handleRequest(Object input, Context context) {  
            return "Hello from Lambda!";  
        }  
    }  

Step 3.3: Build the JAR Correctly#

Configure Gradle to build a plain Java JAR:

  1. Open the build.gradle file of your Java Library module (not the app module).
  2. Ensure the plugins block uses java-library (not com.android.application):
    plugins {  
        id 'java-library'  
    }  
  3. Add the AWS Lambda Java Core dependency (required for the RequestHandler interface):
    dependencies {  
        implementation 'com.amazonaws:aws-lambda-java-core:1.2.2'  
        // Add other Java dependencies (e.g., AWS SDK), but NO Android libraries!  
    }  
  4. Build the JAR:
    • Go to Gradle → Your Project → lambda-function → Tasks → build → jar.
    • Click Run (green play button). The JAR will generate in lambda-function/build/libs/lambda-function.jar.

4. Resolve Dependency Conflicts#

Android-specific dependencies (e.g., androidx.appcompat, com.google.android.material) in your JAR can cause conflicts or bloat, leading to class-loading issues.

How to Fix:

  • In your Java Library module’s build.gradle, remove all Android dependencies. Use only Java-standard libraries or AWS SDKs (e.g., aws-lambda-java-events for event handling).
  • To audit dependencies, run:
    ./gradlew :lambda-function:dependencies  
  • Exclude transitive Android dependencies (if any) using exclude:
    implementation('some.library') {  
        exclude group: 'androidx.core' // Example: Exclude AndroidX  
    }  

5. Disable ProGuard/Minification#

Android Studio uses ProGuard to shrink code, but it may remove your handler class if not told to keep it.

How to Fix:

  • In your Java Library module’s build.gradle, ensure minification is disabled:
    android { // Remove this block if using a Java Library (it’s for Android modules!)  
        buildTypes {  
            release {  
                minifyEnabled false // Disable ProGuard  
            }  
        }  
    }  
  • If you must use minification (not recommended for Lambda), add a ProGuard rule to keep the handler class:
    Create proguard-rules.pro in your module and add:
    -keep class com.myapp.LambdaHandler { *; } // Replace with your handler class  

6. Test Locally Before Uploading#

Test your JAR locally to catch ClassNotFoundException before uploading to AWS. Use tools like AWS SAM CLI or Lambda Local.

Example with AWS SAM CLI:

  1. Install the AWS SAM CLI.
  2. Create a template.yaml in your project root:
    AWSTemplateFormatVersion: '2010-09-09'  
    Transform: AWS::Serverless-2016-10-31  
    Resources:  
      MyLambdaFunction:  
        Type: AWS::Serverless::Function  
        Properties:  
          Handler: com.myapp.LambdaHandler::handleRequest  
          Runtime: java11  
          CodeUri: lambda-function/build/libs/lambda-function.jar  
  3. Test locally:
    sam local invoke MyLambdaFunction  

If you see ClassNotFoundException here, fix the JAR before uploading to AWS.

Preventive Measures#

To avoid this error in the future:

  • Use Java Library Modules: Always build Lambda JARs in a Java Library module (not an Android "App" module).
  • Keep Dependencies Minimal: Only include Java-standard libraries or AWS SDKs; avoid Android dependencies.
  • Test Locally: Use AWS SAM CLI or Lambda Local to validate JARs before uploading.
  • Document Handler Paths: Maintain a README with the exact handler string (package.Class::method) for your team.

Conclusion#

The ClassNotFoundException when uploading an Android Studio JAR to AWS Lambda is typically caused by mismatched configurations, invalid JAR structures, or Android-specific dependencies. By using a Java Library module, verifying the handler, inspecting the JAR, and testing locally, you can resolve this error and ensure your serverless function runs smoothly.

With these steps, you’ll bridge the gap between Android Studio’s mobile-focused tools and AWS Lambda’s serverless environment.

References#