Search code examples
javaandroidjava-native-interfaceshared-librariesartoolkit

How to include shared libraries in android studio project?


I am trying to use the ARToolKit library in an android studio project. This library uses an NDK shared library, so i have followed this guide to include it in my project. I have also tried solutions from this question.

I don't think this issue is specific to the ARToolKit library but rather that I misunderstand the NDK shared library usage.

My Android.mk file for the shared library module looks like this (following the question referenced above):

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := artoolkit
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libc++_shared.so
include $(PREBUILT_SHARED_LIBRARY)

Or like this, respectively (following the guide mentioned above):

LOCAL_PATH := /Users/balance/SDKs/ARToolKit5

include $(CLEAR_VARS)
LOCAL_MODULE := artoolkit
LOCAL_SRC_FILES := android/libs/$(TARGET_ARCH_ABI)/libc++_shared.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

And for the .aar module:

include $(CLEAR_VARS)
LOCAL_MODULE := aRBaseLib-release
LOCAL_SHARED_LIBRARIES := artoolkit
include $(BUILD_SHARED_LIBRARY)

I have added the shared library module as a dependency to the .aar module, resulting in this code in aRBaseLib-release:

configurations.maybeCreate("default")
artifacts.add("default", file('aRBaseLib-release.aar'))
dependencies {
    implementation project(':artoolkit')
}

But when trying to build the project i get the following error:

Gradle sync failed: Could not find method implementation() for arguments [project ':artoolkit'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I realise these snippets may not be all that helpful, so I am of course ready to start over with your guidance and to post new error messages from there.

Any ideas will be appreciated and tried out.


Solution

  • I finally found a solution here. The .so file must be located in <project>/app/src/main/jniLibs/ to be included in the .apk. Before, I had put them into the libs folder instead.