简体   繁体   English

从.dll创建.jar

[英]Create a .jar from a .dll

I'm currently trying to create a .jar from a .dll, in order to use it on Android applications. 我目前正在尝试从.dll创建.jar,以便在Android应用程序上使用它。 I'm quite new with this technology, so I'll tried to be as clear as possible. 我对这项技术还很陌生,所以我会尽量保持清晰。

I have 3 distinct parts: 我有3个不同的部分:

  • A first .dll which is an API, in C++ and developed with Visual studio 2013. 第一个.dll是使用C ++并使用Visual Studio 2013开发的API。
  • A second .dll that makes the link between my API and Java code (using JNI, so this is my native library), also developed with Visual studio 2013. 第二个.dll使我的API和Java代码之间建立链接(使用JNI,因此这是我的本机库),该文件也是由Visual Studio 2013开发的。
  • And my Java code that loads my native library and that implements native functions from API. 还有我的Java代码,该代码加载了本机库并通过API实现了本机功能。 I use eclipse IDE for that stuff, because I read somewhere that was the easiest way to create a .jar. 我将eclipse IDE用于这些东西,因为我读过某个地方,这是创建.jar的最简单方法。

This part works pretty well. 这部分效果很好。 I created a main test and I get all the information from the API in Java. 我创建了一个主测试,并从Java中的API获取了所有信息。 But now, I would like to create a .jar file that I can use on Android and here comes the crows... 但是现在,我想创建一个可以在Android上使用的.jar文件,乌鸦来了...

I tried many ways, from the simple .jar export, to the One-Jar method, but nothing worked. 我尝试了很多方法,从简单的.jar导出到One-Jar方法,但没有任何效果。 I always have a link error on my android platform (functions are recognized on the .jar, but their implementations are not). 我的android平台上总是存在链接错误(.jar上可以识别功能,但其实现无法识别)。

So here is my question, how to create a kind of 'static library' in a .jar that I can use on Android? 所以这是我的问题,如何在可以在Android上使用的.jar中创建一种“静态库”?

I hope I was clear enough, don't hesitate to ask me more details. 我希望我足够清楚,不要犹豫,问我更多细节。

Thank you for your help! 谢谢您的帮助!

EDIT: 编辑:
I tried with an Android Library project and my problem is always coming from my loadLibrary() function: 我尝试了一个Android Library项目,而我的问题总是来自我的loadLibrary()函数:

public class MyWrapper {
    static {
        System.loadLibrary("MyWrapper");    
    }

    public static native int getNegative(int p_number);
    public static native int getPositive(int p_number);
}

Every things works fine on my library, but when I use it in android application, I always get this error: 一切都在我的库中正常运行,但是当我在android应用程序中使用它时,我总是会收到此错误:

01-27 11:31:29.565: E/AndroidRuntime(7089): FATAL EXCEPTION: main
01-27 11:31:29.565: E/AndroidRuntime(7089): Process: com.example.wrappertest, PID: 7089
01-27 11:31:29.565: E/AndroidRuntime(7089): java.lang.UnsatisfiedLinkError: Couldn't load MyWrapper from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.wrappertest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.wrappertest-2, /vendor/lib, /system/lib]]]: findLibrary returned null

My library cound't be created as static library, to keep the link on the .dll? 我的库无法创建为静态库,以将链接保留在.dll上吗?

A .dll file is usually windows specific. .dll文件通常是Windows特定的。 If you're wanting to load native code on Android you'll have to look into using the Native Development Kit. 如果要在Android上加载本机代码,则必须考虑使用本机开发套件。 The specifics of using the NDK is a bit beyond what can be explained here, but the Android Developer website provides a decent starting point at http://developer.android.com/tools/sdk/ndk/index.html 使用NDK的细节超出了此处的解释范围,但是Android Developer网站在http://developer.android.com/tools/sdk/ndk/index.html提供了不错的起点

As for the jar itself, you may want to look into building an Android Library Project instead. 至于jar本身,您可能需要考虑构建一个Android Library Project。 This is essentially a means of packaging a jar with some assets (including native libraries) which can be used by an Android Application project. 从本质上讲,这是一种将带有一些资产(包括本机库)的jar打包的方法,供Android应用程序项目使用。

  1. Compile your librarie with ndk-build 使用ndk-build编译您的librarie
  2. Create an Android Library module 创建一个Android库模块
  3. Create a Java class and JNI wrapper 创建一个Java类和JNI包装器
  4. Disable gradle for ndk-build and create Android.mk and Application.mk in the jni folder 禁用ndk-build gradle并在jni文件夹中创建Android.mkApplication.mk
  5. Import the .so library and pre build it on the Android.mk 导入.so库并在Android.mk上预先构建
  6. Invoke ndk-build manually and its done 手动调用ndk-build并完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM