0

I am currently working on an android library in android studio 3.0.1 with cpp support. Therefor I started with a little test to check the functionality and get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no native-lib in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at com.pmdtec.testproject.TestClass.<clinit>(TestClass.java:6)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:107)

My Source Files are:

TestProject/app/src/java/com/example/TestClass.java

package com.example;

public class TestClass
{
    static {
        System.loadLibrary("native-lib");
    }

    public static void main(String[] args)
    {
        new TestClass().helloNative();
    }

    native void helloNative();
}

TestProject/app/src/cpp/native-lib.cpp

#include <jni.h>

extern "C"
JNIEXPORT void JNICALL
Java_com_example_TestClass_helloNative(JNIEnv *env, jobject instance)
{
    // implement later
}

And I also added "android.enableAapt2=false" to the TestProject/gradle.properties because the gradle build got this error (This error does not occure anymore. I just wanted to document that, and why I added the property to gradle.properties)

Information:Gradle: Executing tasks: [:app:assembleDebug]
Information:Gradle: BUILD FAILED in 4s
Information:15.01.2018 11:46 - Compilation completed with 5 errors and 0 warnings in 6s 736ms
Error:Gradle: failed to create directory 'D:\workspace\TestProject\app\build\generated\source\r\debug\com\example'.
Error:Gradle: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Gradle: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Gradle: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Gradle: Execution failed for task ':app:processDebugResources'.
> Failed to execute aapt

The files that are not shown here are the generated files. So my question is, does anyone know why I get the error mentiont at the top of this post?


Some more details:

The resulting .apk contains the following files

/lib/arm64-v8a/libnative-lib.so
/lib/armeabi-v7a/libnative-lib.so
/lib/x86/libnative-lib.so
/lib/x86_64/libnative-lib.so
/META-INF/...
/res/...
AndroidManifest.xml
classes.dex
resources.arsc

Is there anything I have to do, so the native-lib is linked?

Many thanks in advance for your support!

Kind regards Robert A. Fritsch

  • _"Compilation completed with 5 errors ..."_ you need to find where these are logged. – Richard Critten Jan 15 '18 at 11:25
  • Your `TestClass` has `package com.example;` but your JNI call declares it as `com.pmdtec.testproject.TestClass` - these need to match. So either refactor your project or fix your JNI export name. – Shark Jan 15 '18 at 11:32
  • @Shark right, that was by editing. sorry. –  Jan 15 '18 at 12:05
  • @richard-critten I do not think aapt is needed. I just pointed out that I disabled it to avoid this error. –  Jan 15 '18 at 12:07
  • @thomas-mary I dont think this is a duplicate. The resulting libnative-lib.so in the .apk is placed in lib and therefor be linked automatically. Or am I wrong? –  Jan 15 '18 at 12:14
  • _" I do not think aapt is needed..."_: no, I meant it looks like your build has already failed at this point and error are logged elsewhere. If the build has failed then running and getting loader errors are secondary. – Richard Critten Jan 15 '18 at 12:18
  • @richard-critten after adding "android.enableAapt2=false" to the gradel.properties, the mentioned error does not occure anymore. I will add this comment to the question. –  Jan 15 '18 at 12:22

1 Answers1

0

thanks everyone for reading my question. It turned out, that I have to run my code on a device. Its not possible to test navtive code via a main-method if you are using android to build it.

Kind regards Robert A. Fritsch