能否在Android项目中弃用Gradle改用Maven?如何配置相关参数?
Hey there, great question! The short answer: yes, you absolutely can ditch Gradle for Maven in your Android project. Even though Google pushes Gradle as the default build tool, Maven has full support for Android builds via the android-maven-plugin (maintained as part of the Apache Maven ecosystem). Let’s walk through exactly how to make this switch, including mapping all those familiar Gradle settings to Maven’s configuration.
First: Prep Your Project Structure
Start by cleaning out all Gradle-related files: delete build.gradle (both root and module-level), settings.gradle, gradle.properties, and the .gradle folder.
Good news: most of your existing project structure will carry over since Maven’s standard layout aligns closely with Gradle’s default Android structure:
- Main source code stays in
src/main/java - Resources remain in
src/main/res AndroidManifest.xmlstays put atsrc/main/AndroidManifest.xml- For instrumented tests, keep them in
src/androidTest/java; unit tests go insrc/test/java
Core: Configure Your pom.xml
The heart of your Maven build is the pom.xml file (create one at the root; add module-specific pom.xmls if you’re working with a multi-module project). Below’s how to map key Gradle parameters to Maven:
1. Set Up the Android Maven Plugin
First, you need to include the plugin that handles Android-specific builds:
<build> <plugins> <plugin> <groupId>com.simpligility.maven.plugins</groupId> <artifactId>android-maven-plugin</artifactId> <version>4.6.0</version> <!-- Grab the latest stable version --> <extensions>true</extensions> <configuration> <!-- We’ll fill this with your Gradle equivalents next --> </configuration> </plugin> </plugins> </build>
2. Map compileSdkVersion
In Gradle, you’d set compileSdkVersion 34; in Maven, this goes in the plugin’s SDK config:
<configuration> <sdk> <platform>34</platform> <!-- Matches compileSdkVersion 34 --> <path>${env.ANDROID_HOME}</path> <!-- Uses your system’s Android SDK path --> </sdk> </configuration>
3. Enable MultiDex (multiDexEnabled true)
To replicate Gradle’s multiDex setup, do two things:
- Add the multidex dependency to your
dependenciesblock:<dependencies> <dependency> <groupId>com.android.support</groupId> <artifactId>multidex</artifactId> <version>1.0.3</version> <scope>compile</scope> </dependency> </dependencies> - Turn on multiDex in the plugin config:
<configuration> <!-- Other configs here --> <multidex>true</multidex> </configuration>
Don’t forget to update your AndroidManifest.xml: either set your application class to MultiDexApplication, or override attachBaseContext() in your custom app class to call MultiDex.install(this).
4. Replicate buildTypes (Debug/Release)
Gradle’s buildTypes translate to Maven Profiles—these let you switch between build configurations easily. Here’s how to set up debug and release profiles:
<profiles> <!-- Default Debug Profile --> <profile> <id>debug</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>com.simpligility.maven.plugins</groupId> <artifactId>android-maven-plugin</artifactId> <configuration> <debug>true</debug> <sign>false</sign> <!-- No signing for debug builds --> </configuration> </plugin> </plugins> </build> </profile> <!-- Release Profile --> <profile> <id>release</id> <build> <plugins> <plugin> <groupId>com.simpligility.maven.plugins</groupId> <artifactId>android-maven-plugin</artifactId> <configuration> <debug>false</debug> <sign>true</sign> <!-- Configure your release keystore here --> <signing> <keystore>${user.home}/.android/release.keystore</keystore> <storepass>your-store-password</storepass> <keypass>your-key-password</keypass> <alias>your-key-alias</alias> </signing> <!-- Enable ProGuard (matches minifyEnabled true in Gradle) --> <proguard>true</proguard> <proguardConfig>proguard-rules.pro</proguardConfig> </configuration> </plugin> </plugins> </build> </profile> </profiles>
To build a release APK, run mvn clean install -Prelease—the default build will use the debug profile.
5. Other Common Gradle Settings
minSdkVersion: Add<minSdkVersion>21</minSdkVersion>inside the<sdk>blocktargetSdkVersion: Add<targetSdkVersion>34</targetSdkVersion>inside the<sdk>block- Dependencies: Gradle’s
implementation= Maven’s<scope>compile</scope>;androidTestImplementation=<scope>androidTest</scope>;testImplementation=<scope>test</scope>
Build Your Project
Once your pom.xml is set up, use these Maven commands:
- Build debug APK:
mvn clean install - Build release APK:
mvn clean install -Prelease - Run instrumented tests:
mvn android:android-test
Quick Notes
- Make sure you have Maven 3.6+ installed and your
ANDROID_HOMEenvironment variable configured. - Complex Gradle build variants might need more elaborate profile setups, but all core features (multiDex, signing, obfuscation) work seamlessly.
- For multi-module projects, create a
pom.xmlfor each module and link them via the root pom’s<modules>block.
内容的提问来源于stack exchange,提问作者Adrián García Campos

