java – What is Maven in Android Studio?-ThrowExceptions

Exception or error:

There are lot of Answers on how to use Maven in Android and work with it.

However, As Gradle being already available, which manages the build process of Android projects and support complex scenarios in creating Android applications, such as Multi-distribution and Multi-apk,

What exactly is the role of Maven in Android Studio?

And how is it different from Gradle?

How to solve:

The short answer is: The tools do the same thing but in different ways. The difference is how you can use them to build your project. Apache Maven goes for “follow our convention” way and Gradle gives you flexibility. Some devs don’t want a new tool or don’t have time to learn how to use it properly.

Now the long answer…

To understand properly why some devs are attached to Apache Maven, we have to look at some years in the past.

Apache Maven is a build automation tool just like Gradle.

Maven got released on 2004. Gradle first version got out on 2007 but it did not become as popular as Maven in the early days.
Some devs don’t like having to learn anything new and most companies don’t want to risk the exchange of an already running and mastered tool (Maven) for the new kid on the block.

Gradle became popular when the Android development raised. Android’s project has a different project structure from Java EE/Web projects. Trying to use Maven on Android’s project just feels unnatural, the tool was not prepared to provide flexibility.

Until now, Apache Maven can be used on simple Java EE projects without being a pain in the ass if you already know the forced conventions. So devs that aren’t aware of Gradle/did not get in touch with Android apps don’t have motivation to change to another tool.

In my opinion, Gradle is better than Apache Maven in every way that you could imagine. It gives you flexibility and tries not to get in your way, a “feature” that you cannot find in Apache Maven. If you don’t follow Apache Maven life cycle, your build will fail, that’s it.

For example, your code can be spread across many directories in any kind of layout if you are using Gradle. If you are on Maven and you don’t follow the ‘convention’, you’ll lose some hours changing your pom (Maven‘s build file) to be able to understand and handle your folder structure.

E.g.: Java source code must be on src/main/java folder. If you got an old project and the structure is src/java, sorry, You have no guarantee that all Maven plugins will run as expected.

Gradle does us a favor and puts more features for comparison in a very organized chart. Take a look. https://gradle.org/maven_vs_gradle/

###

Maven is just a tool that manages and simplifies how you build your project. Among many other things (running tests, managing conflicts, documentation, modularization), its most useful purpose is that of automatically fetching dependencies that your project needs and dependencies of those dependencies (transitive dependencies) if any. Dependencies are usually just JAR files that contain re-usable code.

So what does this mean. Suppose you want to use a library such as OpenCSV to generate a CSV file in your application. Non-Maven way: Google search for the library, check if it needs other libraries and if so download them, put them in your project’s classpath then build. When you move your project to another PC, copy all the JAR’s or it won’t work. Maven way: insert something like this in a file named pom.xml:

 <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.3</version>
    </dependency>

Done. Maven will do the rest for you when you build (download to local cache if not already existing, fetch dependencies, javadoc etc). Even if you were to copy your project to another PC without the JAR’s, delete the JAR's etc, if Maven is present it will re-download them automatically when you build.

For more: Link

Leave a Reply

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