• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Arshia001 · Mar 13, 2015 at 07:40 PM · androidnativedllnotfoundexceptionopencv

Why does Unity fail to load android native libraries when linked against OpenCV?

The title says it all. I'm using NDK to build a native library for use with Unity (the game engine, not the Ubuntu shell). I already have much of the code in place and it works on my Xperia Z Ultra w$$anonymous$$ch runs Android 4.4.4. However, recently I sent the app to some other people to test on their phones, and it worked on none of their phones. They were using Android 4.0 and 4.1, so I tried running the app on my own Android 4.0.4 device (an old Xperia Mini Pro) and had the same problem. After much narrowing down, I've found out that the root of the problem is including OpenCV in the build, even if it's not referenced at all.

Here's the code I have now. First, the simplest CPP file you've seen:

 //Test.cpp:
 extern "C"
 {
     int Test(int a, int b)
     {
         return a + b;
     }
 }

Note how it doesn't even include anyt$$anonymous$$ng from OpenCV. The makefile (or whatever it's called in the context of NDK, I'm mostly a Windows/Visual Studio person) is:

 #Android.mk:
 LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
 
 #---------------------------
 #note: if I comment these lines out, the library works just fine.
 #if I don't, it won't load at all.
 #---------------------------
 OPENCV_PACKAGE_DIR := D:\Eclipse\OpenCVAndroid\OpenCV-2.4.9-android-sdk
 include $(OPENCV_PACKAGE_DIR)/sdk/native/jni/OpenCV.mk
 
 LOCAL_MODULE    := Test
 LOCAL_SRC_FILES := Test.cpp
 
 include $(BUILD_SHARED_LIBRARY)

Building t$$anonymous$$s project gives me a "libTest.so" file. I put t$$anonymous$$s into my Unity project, at Assets/Plugins/Android/ (I've also tried putting it in Plugins/Android/libs/armeabi-v7a/, no luck). I also have a script inside unity w$$anonymous$$ch invokes the library:

 //TestNative.cs:
 using UnityEngine;
 using System.Collections;
 using System.Runtime.InteropServices;
 
 public class TestNative : MonoBehaviour
 {
     [DllImport("Test")]
     public static extern int Test(int a, int b);
 
     void OnGUI()
     {
         try
         {
             GUI.Label(new Rect(0, 0, 100, 100), "2 + 3 = " + Test(2, 3));
         }
         catch (System.Exception e)
         {
             GUI.Label(new Rect(0, 0, 600, 400), e.GetType().ToString() + " " + e.Message + "\n" + e.StackTrace);
         }
     }
 }

When I run t$$anonymous$$s on my Z Ultra, it works just fine. When I run it on the Mini Pro, it fails with the exception "DllNotFoundException: Test". I've checked logcat for errors, and t$$anonymous$$s is what it says:

 01-06 06:46:27.660: D/dalvikvm(11135): Trying to load lib /mnt/asec/com.cet.sna2-2/lib/libTest.so 0x2bb86638
 01-06 06:46:27.660: D/dalvikvm(11135): Trying to load lib /mnt/asec/com.cet.sna2-2/lib/libTest.so 0x2bb86638
 01-06 06:46:27.660: E/Unity(11135): Unable to find Test

It doesn't say anyt$$anonymous$$ng else, it just fails. The fact that it works on 4.4.4 makes me t$$anonymous$$nk it might have somet$$anonymous$$ng to do with build configurations or somet$$anonymous$$ng like that, but I can't figure out what it is. Any ideas? Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Arshia001 · Dec 02, 2015 at 04:23 AM

Sorry for never updating t$$anonymous$$s question until now. The problem was that Android was not loading the libraries in the correct order. What I did to solve it was to manually load the libraries before they were referenced using Unity's AndroidJavaObject to make loadLibrary calls and load them in correct order.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rockin_Zombie · Oct 17, 2017 at 04:23 PM 0
Share

Hi, can you share how you solved this issue? We are running into the same problem while trying to link OpenCV natively

avatar image tmendez · Jul 19, 2019 at 08:15 PM 1
Share

For people having trouble with this, you'll need to add a public function to your plugin like so:

Java:

 public static void loadLibc() {
     System.loadLibrary("c");
 }

And then back in Unity you'll need to call it:

 private void importLibc() {
     AndroidJavaClass plugin = new AndroidJavaClass("PluginClass");
     plugin.CallStatic("loadLibc");
 }

In my example, I was getting the error that it couldn't find the library "libc". So I had to call loadLibrary on "c".

Edit: This does not work.

avatar image kyle-misner-kriel tmendez · Aug 21, 2020 at 08:04 PM 0
Share

You just saved my life <3

avatar image
1

Answer by CAMOBAP · Dec 02, 2015 at 03:17 AM

You may inspect your library dependencies first, like described below http://stackoverflow.com/questions/6242761/how-do-i-find-the-direct-shared-object-dependencies-of-a-linux-elf-binary

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arshia001 · Dec 02, 2015 at 04:20 AM 0
Share

Thanks man, I'll keep that in mind.

avatar image
0

Answer by heyGlo · Jun 17, 2015 at 03:28 PM

try to add libopencv_java.so into the your unity project: ../Assets/Plugins/Android/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dll not found exception in Editor only 0 Answers

How to pass ArrayList from Java to Unity C# 1 Answer

OpenCV - Mobile Devices Query 0 Answers

Sqlite3.dll not found on specific Android devices 2 Answers

How to use android Renderscript v8 with Unity? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges