※ [本文轉錄自 AndroidDev 看板 #1Lts9J1F ]
作者: Nt1 (用功點吧!) 看板: AndroidDev
標題: [問題] 用mono c# 無法載入 android 的 library
時間: Fri Aug 28 03:17:36 2015
想請教一下版上的大大關於 mono 的問題,問題如下:
我有一個需求,是在arm的平台上,使用 mono 寫c#,並且load library,這隻.so是
包在apk裡面的,已經unzip拉出來。
但是不管我用什麼方法,mono總是跟我說找不到這個 so 檔,但是他明明就在orz
我試過:
1. 把 .so和.exe放一起
2. 把 .so放到 /usr/lib, 並執行ldconfig更新library cache,
確定 ldconfig -p 有看到該so檔
3. export LD_LIBRARY_PATH ="./"
4. 用不同的表示方法去load…像是
[DllImport("libChronicleNUtils.so")],
[DllImport(@"/usr/lib/libChronicleNUtils.so")]
[DllImport("ChronicleNUtils")]
都會失敗,失敗訊息如下:
Mono: DllImport attempting to load: 'ChronicleNUtils'.
Mono: DllImport error loading library '/home/pi/libChronicleNUtils':
'/home/pi/libChronicleNUtils: cannot open shared object file: No such file or
directory'.
Mono: DllImport error loading library '/home/pi/libChronicleNUtils.so':
'/home/pi/libChronicleNUtils.so: cannot open shared object file: No such file
or directory'.
(略...)
Unhandled Exception:
System.DllNotFoundException: ChronicleNUtils
at (wrapper managed-to-native) NativePlugin:breakData_ (intptr,int,int)
at NativePlugin.breakData (System.Byte[] data, Boolean isData) [0x00000] in
<filename unknown>:0
at NativePlugin.Main (System.String[] args) [0x00000] in <filename
unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.DllNotFoundException:
ChronicleNUtils
at (wrapper managed-to-native) NativePlugin:breakData_ (intptr,int,int)
at NativePlugin.breakData (System.Byte[] data, Boolean isData) [0x00000] in
<filename unknown>:0
at NativePlugin.Main (System.String[] args) [0x00000] in <filename
unknown>:0
我100%確定 /home/pi/libChronicleNUtils.so 是存在的,
但是在debug訊息卻說找不到..
請大家幫幫忙,找了很久找不到原因,附上簡單的source code
===============================================
using System.Runtime.InteropServices;
using System;
using System.Text;
public static class NativePlugin
{
[DllImport("ChronicleNUtils")]
private static extern byte breakData_(IntPtr data, int data_size, int
bd);
public static void breakData(byte[] data, bool isData)
{
GCHandle gCHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
IntPtr data2 = gCHandle.AddrOfPinnedObject();
NativePlugin.breakData_(data2, data.Length, (!isData) ? 60 :
221);
gCHandle.Free();
}
static int Main(string[] args)
{
bool isData = false;
byte[] data =System.Text.Encoding.Default.GetBytes("test
data");
NativePlugin.breakData(data, isData);
return 0;
}
}
==============================================================
其他資訊:
file /home/pi/libChronicleNUtils.so
/home/pi/libChronicleNUtils.so: ELF 32-bit LSB shared object, ARM, version 1
(SYSV), dynamically linked, stripped
uname -a
Linux raspberrypi 3.10.26+ #2 Fri Jan 17 22:13:59 EST 2014 armv6l GNU/Linux