Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: How do I get .so libraries bundled in package

I have a third party library coming with .jar and .so file.

I configured pom.xml as following:

<dependency>
    <groupId>com.abc.def</groupId>
    <artifactId>sdc</artifactId>
    <version>1.1</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.abc.def</groupId>
    <artifactId>sdc</artifactId>
    <version>3</version>
    <scope>compile</scope>
    <type>so</type>
</dependency>

With this configure, I successfully tested through Intellij and apk file under target contains structure like lib/armeabi/sdc.so

However, after I do mvn clean package, the apk file generated did not contain sdc.so file, and after installing apk file on android device, lib folder is empty.

Searching through the internet, and did not find answer.

Btw, I do add <nativeLibrariesDirectory>${project.basedir}/libs</nativeLibrariesDirectory> into pluginManagement as mentioned Native libraries (.so files) are not added to an android project, but does not help.

Updates:

If someone is facing same problem as I am that SO file did not copy over, please try manually copy the so file over as following:

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
      <executions>
        <execution>
          <id>copy</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>copy</goal>
          </goals>
          <configuration>
            <artifactItems>
               <artifactItem>
                 <groupId>com.abc.def</groupId>
                 <artifactId>libdef</artifactId>
                 <version>2.1.3</version>
                 <type>so</type>
                 <destFileName>libdef.so</destFileName>
             </artifactItem>
            </artifactItems>
            <outputDirectory>${project.build.directory}/libs/armeabi</outputDirectory>
           </configuration>
         </execution>
      </executions>
    </plugin>
  </plugins>
 </build>
like image 794
jxyiliu Avatar asked Nov 12 '22 22:11

jxyiliu


1 Answers

I suggest you to use maven-android-plugin version 2.8.3 or more.

The scope of your native lib must be runtime (not sure it is required, but anyway it's a fact)

The artifactId must start with lib (i.e. it must be libsdc)

<dependency>
    <groupId>com.abc.def</groupId>
    <artifactId>libsdc</artifactId>
    <version>3</version>
    <scope>runtime</scope>
    <type>so</type>
</dependency>

The artifactId will be the library name so load it with this line of code

System.loadLibrary("sdc");

reference

Note: I don't know if sdc if the real artifactId, but if it's the case you must consider re-publishing it with the lib prefix.

like image 156
ben75 Avatar answered Nov 14 '22 23:11

ben75