Domanda

I am writing an android library project and would like to add the support for maven. My library project pom file looks like this:

   <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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>my_group_id</groupId>
  <artifactId>my_artifactId</artifactId>
  <version>1.0.1</version>
  <packaging>apklib</packaging>
  <name>My Library Project</name>

  <dependencies>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <version>4.1.1.4</version>
      <scope>provided</scope>
    </dependency>   
  </dependencies>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
          <version>3.8.2</version>
          <extensions>true</extensions>   
        <configuration>
          <sdk>
            <platform>19</platform>
          </sdk>
        </configuration>     
      </plugin>
    </plugins>
  </build>
</project>

My main project pom file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.simpligility.android</groupId>
  <artifactId>helloflashlight</artifactId>
  <version>1.0.0</version>
  <packaging>apk</packaging>

  <name>HelloFlashlight</name>

  <dependencies> 
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <version>4.1.1.4</version>
      <scope>provided</scope>
    </dependency>        
    <dependency>
  <groupId>my_group_id</groupId>
  <artifactId>my_artifactId</artifactId>
  <version>1.0.1</version>
      <type>apklib</type>
    </dependency>               
  </dependencies>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
          <version>3.8.2</version>
          <extensions>true</extensions>   
        <configuration>
          <sdk>
            <platform>19</platform>
          </sdk>
        </configuration>     
      </plugin>
    </plugins>
  </build>

</project>

I am installing the library project to my local repository using:

mvn clean install

then I am compiling and installing the main project to my local repository using:

mvn clean install

and generating the apk using:

mvn android:deploy

Now the problem occurs when I compile the project with maven. The project compiles successfully, however when I am running the apk on the device I receive NullPointerException when trying to perform findById for resources that are inside the library project. Please note that part of the time findById does return value, but not always the correct type (e.g, expected layout but got a button). It is important to say that when running in eclipse without maven everything works perfectly.

Please advise

È stato utile?

Soluzione

Finally I found the solution, so I'll update my answer for future readers. The solution to this problem was to create aar file instead of apklib file using maven.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top