unbekant
unbekant

Reputation: 1555

Getting an android maven library with native dependencies to work in both gradle and maven projects

I've been banging my head against the wall the entire day trying to figure this out. I'm trying to create an android library X both as an aar and apklib. The reason for this is that we have an android app that builds using android maven plugin and should use the apklib, but someone else will be using that library in project Z, using gradle, hence needing the aar.

The library I'm are trying to build has native dependencies and we are having serious issues in getting this library to work for both projects. Either one or the other fails depending on how we alter the pom file for Library X.

Let me give some more context with some pseudo build files, unfortunately I can't share the original ones.

pom.xml for library X, to be built with android maven plugin

<project ... >
...
<groupId>com.library</groupId>
<artifactId>myLibrary</artifactId>
<version>0.0.1</version>
<packaging>apklib</packaging>
...
<dependencies>
    <dependency>
        <groupId>com.native</groupId>
        <artifactId>myNativeDependency</artifactId>
        <version>2.0.0</version>
        <classifier>armeabi-v7a</classifier>
        <scope>runtime</scope>
        <type>so</type>
    </dependency>
    ...
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>4.0.0-rc.2</version>
            <configuration>
              <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
              <resourceDirectory>${project.basedir}/res</resourceDirectory>
                <mergeManifests>true</mergeManifests>
                <sdk>
                    <path>${env.ANDROID_SDK_HOME}</path>
                    <platform>${android.platform.version}</platform>
                </sdk>
            </configuration>
            <extensions>true</extensions>
        </plugin>
    </plugins>
</build>
</project>

Library X, packaged as an apklib, will be added in a maven project as a dependency as follows:

Maven Project Y, pom.xml:

...
<dependencies>
...
    <dependency>
         <groupId>com.library</groupId>
         <artifactId>myLibrary</artifactId>
         <version>0.0.1</version>
         <type>apklib</type>
    </dependency>
</dependencies>
...

Gradle project Z, referencing library X packaged as aar, build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'maven'
android {
    compileSdkVersion 20
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "com.company"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "com.library:myLibrary:0.0.1"
}

Given this configuration, the result we obtain is a running maven project Y, which seems to resolve the native dependency without issues as the .so gets included in the resulting apk. However, gradle project Z builds fine but is not able to find the .so dependency in runtime. When I explore the apklib/aar generated for library X, the .so library is not there. Somehow, maven project Y knows how to handle this transitive dependency and the native library makes it to the generated apk, but the gradle project fails to do so and the shared library does not make it into the generated apk.

What is the right way to have a library with native dependencies work both on gradle (as an aar) and maven (as an apklib) projects? Is there any way to force maven to include the native artifacts in the resulting apklib/aar generated?

Hopefully I've been clear enough, I'm open for suggestions on how to improve this question. Thanks!

Upvotes: 3

Views: 600

Answers (0)

Related Questions