Difficulty Using JNA on Android without Copying jnidispatch.so

189 views
Skip to first unread message

k

unread,
Feb 27, 2021, 8:53:16 AM2/27/21
to Java Native Access
Hi,

I'm tentatively trying to upgrade a project to no longer need to manually copy jnidispatch.so in.  The project builds a jar with maven that uses jna, and this jar is then used in an android app.

I've changed the system to build an aar instead of a jar, and installed this in the local maven repository.  Now it automatically pulls jna in.  Unfortunately, it still doesn't get jnidispatch.so .

I checked my ~/.m2/repository tree and it looks like it is using the jna .jar instead of the jna .aar .  My understanding is that the .aar contains the android jnidispatch.so binaries, but the .jar does not.

In the library pom.xml is markup like these:

  <build><plugins>
    <plugin>
      <groupId>com.simpligility.maven.plugins</groupId>
      <artifactId>android-maven-plugin</artifactId>
      <version>4.4.3</version>
      <extensions>true</extensions>
    </plugin>
  </plugins></build>

  <dependencies>
    <dependency>
      <groupId>net.java.dev.jna</groupId>
      <artifcatId>jna-platform</artifactId>
      <version>5.7.0</version>
    </dependency>
  </dependencies>

Is this the correct setup?

Matthias Bläsing

unread,
Feb 27, 2021, 9:31:48 AM2/27/21
to jna-...@googlegroups.com
Hi k,
That is a dependency on jna-platform. This will pull in jna jar. Do you
really need jna-platform or only jna? If you need the platform and want
to use it on android, I would try this:

<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.7.0</version>
<exclusions>
<exclusion>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.7.0</version>
<type>aar</type>
</dependency>

It was googles decision not to allow loading of native libraries from
arbitrary paths. This makes the default JNA library extraction loading
impossible on android. Google invented a new format (aar) which embeds
libraries and the java source code separate from each other.

The above declares a dependency on jna-platform and excludes its
transitive default dependency jna. For android the _aar_ is then added
as an additional dependency.

HTH

Matthias

Karl

unread,
Feb 27, 2021, 10:37:15 AM2/27/21
to jna-...@googlegroups.com
>> Is this the correct setup?
>
> That is a dependency on jna-platform. This will pull in jna jar. Do you
> really need jna-platform or only jna? If you need the platform and want
> to use it on android, I would try this:
>
> <dependency>
> <groupId>net.java.dev.jna</groupId>
> <artifactId>jna-platform</artifactId>
> <version>5.7.0</version>
> <exclusions>
> <exclusion>
> <groupId>net.java.dev.jna</groupId>
> <artifactId>jna</artifactId>
> </exclusion>
> </exclusions>
> </dependency>
> <dependency>
> <groupId>net.java.dev.jna</groupId>
> <artifactId>jna</artifactId>
> <version>5.7.0</version>
> <type>aar</type>
> </dependency>

Thanks for your reply and advice.

I don't believe jna-platform is needed. But with or without it, when
I specify <type>aar</aar> for jna, `mvn packages` gives a null pointer
exception in CompilerMojo.preparePaths like in
https://issues.apache.org/jira/browse/MCOMPILER-359 . I haven't found
a way around this yet but am slowly trying different avenues.

Is there a working example anywhere?

Matthias Bläsing

unread,
Feb 27, 2021, 11:06:17 AM2/27/21
to jna-...@googlegroups.com
Hi Karl,
you could show your source and I would take a look. The referenced
issue is solved for 2 years, maybe your compiler version is just
ancient and it fails because of this?

Greetings

Matthias



Karl

unread,
Feb 27, 2021, 11:20:07 AM2/27/21
to jna-...@googlegroups.com
>> I don't believe jna-platform is needed. But with or without it, when
>> I specify <type>aar</aar> for jna, `mvn packages` gives a null
>> pointer
>> exception in CompilerMojo.preparePaths like in
>> https://issues.apache.org/jira/browse/MCOMPILER-359 . I haven't
>> found
>> a way around this yet but am slowly trying different avenues.
>>
>> Is there a working example anywhere?
>
> you could show your source and I would take a look. The referenced
> issue is solved for 2 years, maybe your compiler version is just
> ancient and it fails because of this?

Here's where I'm at with the pom file:
https://github.com/xloem/brainflow/blob/android-3-test/java-package/brainflow/pom.xml

Specifying <type>aar</type> is a temporary kludge because a jar for
non-android is also built, but I could simply make two pom files.

Unfortunately although the referenced issue with maven-compiler-plugin
has been fixed, there doesn't appear to have been a release yet that
includes the fix.

Matthias Bläsing

unread,
Feb 27, 2021, 11:54:31 AM2/27/21
to jna-...@googlegroups.com
Hi Karl,

Am Samstag, den 27.02.2021, 11:20 -0500 schrieb Karl:
> > > I don't believe jna-platform is needed. But with or without it,
> > > when
> > > I specify <type>aar</aar> for jna, `mvn packages` gives a null
> > > pointer
> > > exception in CompilerMojo.preparePaths like in
> > > https://issues.apache.org/jira/browse/MCOMPILER-359 . I haven't
> > > found
> > > a way around this yet but am slowly trying different avenues.
> > >
> > > Is there a working example anywhere?
> >
> > you could show your source and I would take a look. The referenced
> > issue is solved for 2 years, maybe your compiler version is just
> > ancient and it fails because of this?
>
> Here's where I'm at with the pom file:
> https://github.com/xloem/brainflow/blob/android-3-test/java-package/brainflow/pom.xml
>
> Specifying <type>aar</type> is a temporary kludge because a jar for
> non-android is also built, but I could simply make two pom files.

as already written, please direct this at google. They decided that
Java was not good enough and had a serious NIH syndrom.

> Unfortunately although the referenced issue with maven-compiler-
> plugin
> has been fixed, there doesn't appear to have been a release yet that
> includes the fix.
>

At least after I got it to compile, I got this trace:

Caused by: java.lang.NullPointerException
at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init> (SchemaModule.java:157)
at com.android.repository.api.SchemaModule.<init> (SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.<clinit> (AndroidSdkHandler.java:81)
at com.simpligility.maven.plugins.android.AndroidSdk.<init> (AndroidSdk.java:91)
at com.simpligility.maven.plugins.android.AbstractAndroidMojo.getAndroidSdk (AbstractAndroidMojo.java:1172)
at com.simpligility.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR (GenerateSourcesMojo.java:789)
at com.simpligility.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.execute (GenerateSourcesMojo.java:240)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)


To my knowledge, google decided to abandon the original eclise based
IDE, the maven tooling and switched to Android Studio + gradle.

Given, that there is only one elephant in the room (google) I strongly
suggest to follow their best practices or be prepared to debug all
steps yourself.

Greetings

Matthias

Karl

unread,
Feb 27, 2021, 12:53:42 PM2/27/21
to jna-...@googlegroups.com
>> > > Is there a working example anywhere?
>> >
>> > you could show your source and I would take a look. The referenced
>> > issue is solved for 2 years, maybe your compiler version is just
>> > ancient and it fails because of this?
>>
>> Here's where I'm at with the pom file:
>> https://github.com/xloem/brainflow/blob/android-3-test/java-package/brainflow/pom.xml
>>
>> Specifying <type>aar</type> is a temporary kludge because a jar for
>> non-android is also built, but I could simply make two pom files.
>
> as already written, please direct this at google. They decided that
> Java was not good enough and had a serious NIH syndrom.

Thanks .. I don't really understand what you are saying here, but I
infer that maven and android is not the way to go with regard to JNA.
It sounds like you recommend using gradle if I want jna on android to
work as described, and that this is due to choices made by google.
Reply all
Reply to author
Forward
0 new messages