【问题标题】:Do I need seperate library with native function in order to load library with main function?我是否需要带有本机功能的单独库才能加载带有主功能的库?
【发布时间】:2019-05-09 13:10:37
【问题描述】:

就像在主题中一样,我是否需要带有本机功能的单独库才能加载带有主要功能的库?还是建议这样做?

此时,我使用 main.so 编译了那些本机函数,但我不确定是否应该这样做。

native 是在 main 之前加载的 lib

protected String[] getLibraries() {
        return new String[] {
            "SDL2",
            "hidapi",
            "native",
            "main"
        };
    }

在里面我得到了一堆原生函数,其中一个就是例子

JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeRunMain(jstring library, jstring function, jobject array)
{
  return Java_org_libsdl_app_SDLActivity_nativeRunMain("libmain.so", "SDL_main", NULL);
}

到目前为止,应用程序正在运行加载库,并没有尝试查看 log_android 是否来自主函数,但它没有。

【问题讨论】:

  • 我不完全确定您所说的 “main function” 是什么意思。加载原生库后,VM会调用的函数是JNI_OnLoad
  • 从表面上看,你在这个 nativeRunMain 原生方法中有一个无限递归。您想在这里实现什么目标?
  • @Michael 我的意思是 int main() 之一,我确实在某处读到 JNI_OnLoad 不是必需的,logcat 也没有说没有找到 JNI_OnLoad
  • @AlexCohn 不介意这个函数里面有什么。我想运行 libmain.so,但是一旦加载它就会立即完成并关闭应用程序。 pastebin.com/X8AAtuaS
  • 我不确定我是否理解您粘贴的日志行来自何处​​。能否展示相关源代码?

标签: android java-native-interface shared-libraries sdl-2


【解决方案1】:

好的,我的问题的解决方案有点奇怪。我不得不使用 SDL2 提供的构建脚本之一从头开始生成整个包。然后我从旧文件中复制了一些文件,然后它现在可以访问 int main() 函数了。

这是来自 Logcat 的日志。

05-11 12:42:28.829 10315 10315 V SDL     : Device: santoni
05-11 12:42:28.829 10315 10315 V SDL     : Model: Redmi 4X
05-11 12:42:28.829 10315 10315 V SDL     : onCreate()
05-11 12:42:28.890 10315 10315 V SDL     : nativeSetupJNI()
05-11 12:42:28.890 10315 10315 V SDL     : AUDIO nativeSetupJNI()
05-11 12:42:28.890 10315 10315 V SDL     : CONTROLLER nativeSetupJNI()
05-11 12:42:28.900 10315 10315 D hidapi  : Initializing Bluetooth
05-11 12:42:28.929 10315 10315 D AccessibilityManager: current package=org.libsdl.app, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=false, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
05-11 12:42:28.952 10315 10315 V SDL     : onResume()
05-11 12:42:28.986 10315 10336 I Adreno  : QUALCOMM build                   : dd15ef5, Ic280a69317
05-11 12:42:28.986 10315 10336 I Adreno  : Build Date                       : 05/09/17
05-11 12:42:28.986 10315 10336 I Adreno  : OpenGL ES Shader Compiler Version: XE031.09.00.04
05-11 12:42:28.986 10315 10336 I Adreno  : Local Branch                     : 
05-11 12:42:28.986 10315 10336 I Adreno  : Remote Branch                    : quic/gfx-adreno.lnx.1.0.r5-rel
05-11 12:42:28.986 10315 10336 I Adreno  : Remote Branch                    : NONE
05-11 12:42:28.986 10315 10336 I Adreno  : Reconstruct Branch               : NOTHING
05-11 12:42:28.994 10315 10336 I OpenGLRenderer: Initialized EGL, version 1.4
05-11 12:42:28.994 10315 10336 D OpenGLRenderer: Swap behavior 1
05-11 12:42:28.995 10315 10315 V SDL     : surfaceCreated()
05-11 12:42:28.995 10315 10315 V SDL     : surfaceChanged()
05-11 12:42:28.995 10315 10315 V SDL     : pixel format RGB_565
05-11 12:42:28.996 10315 10315 V SDL     : Window size: 720x1230
05-11 12:42:28.996 10315 10315 V SDL     : Device size: 720x1280
05-11 12:42:29.001 10315 10315 V SDL     : nativeResume()
05-11 12:42:29.002 10315 10338 V SDL     : Running main function SDL_main from library /data/app/org.libsdl.app-1/lib/arm64/libmain.so
05-11 12:42:29.002 10315 10338 V SDL     : nativeRunMain()

也转到下一个等待我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2017-10-01
    • 1970-01-01
    • 2017-02-25
    • 2015-08-28
    相关资源
    最近更新 更多