How to Create a Java Project properly

🚀 How to Create a Java Android Project in Android Studio (and Not Get Kotlin by Accident)

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:

  1. Open Android StudioFile > New > New Project

  2. Choose a template (like “Empty Activity”)

  3. In the configuration screen:

    • Set Language to Java

    • You can still use Groovy or Kotlin DSL for the build system—doesn’t affect your code

  4. 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

  • Delete MainActivity.kt

Step 2: Create a Java Version
  • Right-click the java folder → New > Java Class → name it MainActivity

Paste this code in:

package your.package.name;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Step 3: Update the Manifest (if needed)

Make sure it references the right class:

<activity android:name=”.MainActivity” />

🧪 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:

  1. Download the OpenCV Android SDK

  2. Import the OpenCV module or AAR file into your project

  3. Load OpenCV in your Java MainActivity:

static {
if (!OpenCVLoader.initDebug()) {
Log.d(“OpenCV”, “Initialization failed”);
} else {
Log.d(“OpenCV”, “Initialization successful”);
}
}

🏁 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.


 

💬 Got questions?

Feel free to leave a comment or message me—happy to help!

Leave a Comment

Your email address will not be published. Required fields are marked *