Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by legyndir · Jun 06, 2014 at 12:19 PM · androidpluginapkc++

How can I include a C++ dll in the .apk file

Hi, I am using a C++ dll file compiled with Visual Studio. When I try to run the game in editor, it works fine and I can use the functions imported from the DLL. I can also build it for windows and run the game without any errors. But when I build the apk for android and run it on the device I get these errors:

 E/Unity   (23601): Unable to find sflib.dll
 D/Unity   (23601): Unable to lookup library path for 'sflib.dll', native renderplugin support disabled.
 E/Unity   (23601): Unable to find sflib.dll
 I/Unity   (23601): DllNotFoundException: sflib.dll
 I/Unity   (23601):   at (wrapper managed-to-native) sfish_script:sf_init ()
 I/Unity   (23601):
 I/Unity   (23601): (Filename: C Line: 0)

Also when I open the apk as an archive, I can not see sflib.dll anywhere. Though not sure if I should be able to see that, since it is not managed code.

Any suggestions? Thanks.

Comment
Add comment · Show 1
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 legyndir · Jun 09, 2014 at 08:35 AM 0
Share

Thanks for the copied answer from the link, but I don't see how it is related to the DLLNotFoundException problem.

The problem is not about calling functions from an external DLL, things work just fine on the desktop or the editor. But its the apk file running on the android that can not find the DLL code.

1) Is it normal that I don't see the DLL file itself in the apk? Or does it get converted to some other format when publishing the package to apk?

2) I place the DLL file in the plugins folder and also a copy in the main folder. It still doesn't solve the problem. What else can I do for the apk file to be able to use the DLL

$$anonymous$$artin, I will try that. I guess Unity will add the .so file from the plugins folder into the apk

Edit: for those looking for a clear description, here is a link from unity: http://docs.unity3d.com/$$anonymous$$anual/PluginsForAndroid.html

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Bilelmnasser · Jun 06, 2014 at 10:49 PM

Unity has extensive support for Plugins, which are libraries of native code written in C, C++, Objective-C, etc. Plugins allow your game code (written in Javascript, C# or Boo) to call functions from these libraries. This feature allows Unity to integrate with middleware libraries or existing C/C++ game code.

Note: On the desktop platforms, plugins are a pro-only feature. For security reasons, plugins are not usable with webplayers.

In order to use a plugin you need to do two things:-

Write functions in a C-based language and compile them into a library. Create a C# script which calls functions in the library. The plugin should provide a simple C interface which the C# script then exposes to other user scripts. It is also possible for Unity to call functions exported by the plugin when certain low-level rendering events happen (for example, when a graphics device is created), see the Native Plugin Interface page for details.

Here is a very simple example: C/C++ File of a Minimal Plugin:

  float FooPluginFunction () { return 5.0F; } 



C# Script that Uses the Plugin:

 using UnityEngine;
 using System.Runtime.InteropServices;
 
 class SomeScript : MonoBehaviour {
 
    #if UNITY_IPHONE || UNITY_XBOX360
    
    // On iOS and Xbox 360 plugins are statically linked into
    // the executable, so we have to use __Internal as the
    // library name.
    [DllImport ("__Internal")]
 
    #else
 
    // Other platforms load plugins dynamically, so pass the name
    // of the plugin's dynamic library.
    [DllImport ("PluginName")]
     
    #endif
 
    private static extern float FooPluginFunction ();
 
    void Awake () {
       // Calls the FooPluginFunction inside the plugin
       // And prints 5 to the console
       print (FooPluginFunction ());
    }
 }

Note that when using Javascript you will need to use the following syntax, where DLLName is the name of the plugin you have written, or “__Internal” if you are writing statically linked native code:

 @DllImport (DLLName)
 static private function FooPluginFunction () : float {};


Creating a Plugin

In general, plugins are built with native code compilers on the target platform. Since plugin functions use a C-based call interface, you must avoid name mangling issues when using C++ or Objective-C.

For further details and examples, see the following pages:

http://docs.unity3d.com/Manual/PluginsForAndroid.html

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
avatar image
3

Answer by MartinCA · Jun 09, 2014 at 11:24 AM

DLL is a windows specific type of shared library. For Androif you'll have to compile your native code into an .so yourself - Unity looks for the appropriate type of library based on the runtime platform. I cannot load windows specific libraries on any other system.

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
avatar image
1

Answer by meat5000 · Jun 23, 2014 at 10:38 AM

Aside from the answers already posted, you can export your project as a 'Google Project' and import it to eclipse (or equivalent)

Android has the NDK for download which is for that very purpose, of creating and using native code plugins.

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.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Choosing which files to move to Android apk expansion file 0 Answers

How to include assets in an android plugin (aar)? 3 Answers

Android Native Plugin (*.SO) 2 Answers

Unity apk installing 2 icons 2 Answers

Android native plugin question 1 Answer


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