Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and maven: problem with maven dependencies in apk

I'm currently working on an Android project using Maven as the build tool. However I'm having a problem with including dependencies, to be more specific: none of the dependencies listed in my pom file are packed in the apk. This results in ClassDefNotFound errors when I'm trying to run the app on my emulator (the underlying ClassNotFound exception is thrown in the Dalvik loader).

I'm using Springsource Tool Suite ( 2.5.2.SR1), with the following plugin configuration:

  • Android Development Tools (10.0.1.v201103111512-110841)
  • Maven integration for Android Development Tools (0.2.5)
  • Maven integration for Eclipse (0.12.1.20110112-1712)
  • Maven (2.2.1)

This is my pom file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>android-test</groupId>
  <artifactId>android-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>apk</packaging>
  <name>android-test</name>

    <properties> 
        <android-platform>10</android-platform>
        <android-emulator>Android-2.3.3</android-emulator>
        <android-sdk-path>C:\Program Files\Android\android-sdk</android-sdk-path>
        <maven-android-plugin-version>2.8.4</maven-android-plugin-version>
        <maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
        <android-version>2.3.1</android-version>
        <spring-android-version>1.0.0.M2</spring-android-version>
        <jackson-version>1.7.2</jackson-version>
    </properties>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>maven-android-plugin</artifactId>
                <version>${maven-android-plugin-version}</version>
                <configuration>
                    <sdk>
                        <platform>${android-platform}</platform>
                        <path>${android-sdk-path}</path>
                    </sdk>
                    <emulator>
                        <avd>${android-emulator}</avd>
                    </emulator>
                    <deleteConflictingFiles>false</deleteConflictingFiles>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin-version}</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${android-version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-rest-template</artifactId>
            <version>${spring-android-version}</version>
        </dependency>
        <dependency>
            <!-- Using Jackson for JSON marshaling -->
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <!-- For testing against latest Spring snapshots -->
        <repository>
            <id>org.springframework.maven.snapshot</id>
            <name>Spring Maven Snapshot Repository</name>
            <url>http://maven.springframework.org/snapshot</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <!-- For developing against latest Spring milestones -->
        <repository>
            <id>org.springframework.maven.milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>

</project>

And my android manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ae.test.android"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

As far as I can tell, this should be a correct setup. The problem now is, when I try to build this project, I get:

Cannot add source folder Duplicate files at the same path inside the APK

I found this on SO and while the solution (changing the default output folder of eclipse to bin instead of target) did solve the error, it didn't solve the dependency problem. My generated android files aren't included in the target folder so the solution is of no use to me.

Has anyone else got any experience with setting up Android and Maven and if so, could you please help me out?

Many thanks in advance!

like image 642
thomaux Avatar asked Jan 20 '23 03:01

thomaux


2 Answers

I skipped using the Maven integration for Android Development Tools (0.2.5) because the eclipse automatically build process took to long. I could leave my default output folder to <project>/target/classes in this case.

Building my project from the command line works fine. The dependencies are included in the apk file.

Running the project from eclipse only works if I add the dependencies explicit to the eclipse project with

Project Properties -> Java Build Path -> Libraries -> Add External JARs...

That was the bitter pill I had to swallow (I am not native english and hope this sentence makes sence).

like image 102
FrVaBe Avatar answered Jan 28 '23 11:01

FrVaBe


Use <scope>compile</scope> for your dependencies wich are not provided by the target system (e.g. the android libs are provided on android systems) otherwise maven will not put the .class files from your dependencies into the apk.

like image 43
Sneek Avatar answered Jan 28 '23 10:01

Sneek