If you're trying to build an Android app using Java, and Android Studio surprised you with a MainActivity.kt file instead of the good ol' MainActivity.java, you're not alone. I recently ran into this myself when setting up an OpenCV project and thought I'd share the fix.
So, let's walk through why this happens and how to make sure you're actually working in Java β not Kotlin.
π€ Why Did I Get a Kotlin File?
Starting with recent Android Studio updates, Kotlin is now the default language for new projects. Even if you choose Groovy DSL (build.gradle) during setup, that only affects the build system, not the programming language of your app.
TL;DR: Android Studio assumes Kotlin unless you explicitly tell it you want Java.
β How to Properly Create a Java-Based Android Project
Here's how to start a Java project the right way in Android Studio:
- Open Android Studio β File > New > New Project
- Choose a template (like "Empty Activity")
- In the configuration screen, set Language to Java (you can still use Groovy or Kotlin DSL for the build system β it doesn't affect your code)
- Click Finish
Now you'll get a MainActivity.java, just like you expected. π
π Already Got a Kotlin Project? Convert It to Java
If your project already started in Kotlin, you can easily switch it to Java manually.
Step 1: Delete the Kotlin Activity
Go to app > java > your.package.name and delete MainActivity.kt.
Step 2: Create a Java Version
Right-click the java folder β New > Java Class β name it MainActivity, then paste this code in:
Step 3: Update the Manifest (if needed)
Make sure it references the right class:
π§ͺ Using Java with OpenCV in Android Studio
If you're planning to use OpenCV with Java in your Android app, you're in good company β Java is still widely supported. Here's a quick preview of the setup:
- Download the OpenCV Android SDK
- Import the OpenCV module or AAR file into your project
-
Load OpenCV in your Java
MainActivity:
π Final Thoughts
It's a small detail, but choosing the right language at project creation time can save you some unnecessary cleanup later.
So if you're seeing MainActivity.kt but wanted Java, trying to use Java libraries like OpenCV, or just feel more at home with Java β make sure to set Language: Java when you create the project.
That's it! Hope this helps someone out there avoid a bit of frustration.
About the Author: gs_admin
A senior technical contributor specializing in architectural designs, software optimization, database structures, and developer education. Passionate about writing clean code and sharing engineering knowledge.