找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2396|回复: 1

Groovy脚本动态加载 so

[复制链接]

1

主题

1

回帖

7

积分

新手上路

积分
7
发表于 2025-3-18 17:51:16 | 显示全部楼层 |阅读模式
/**
* 动态加载指定路径的 .so 库文件
* @param soPath 完整的 .so 文件路径(包括文件名)
* @throws UnsatisfiedLinkError 如果加载失败
* @throws SecurityException 无文件访问权限
*/
def loadNativeLibrary(String soPath) {
    def soFile = new File(soPath)
   
    if (!soFile.exists()) {
        throw new FileNotFoundException("SO 文件未找到: ${soPath}")
    }

    if (!soFile.canRead()) {
        throw new SecurityException("无权限读取 SO 文件: ${soPath}")
    }

    try {
        System.load(soPath)
        println("成功加载库: ${soPath}")
    } catch (UnsatisfiedLinkError e) {
        def errorMessage = "加载失败: ${e.message}"
        if (e.message.contains("already loaded")) {
            errorMessage += " (库已加载)"
        } else if (e.message.contains("wrong ELF class")) {
            errorMessage += " (架构不匹配)"
        }
        throw new UnsatisfiedLinkError(errorMessage)
    }
}

1

主题

1

回帖

7

积分

新手上路

积分
7
 楼主| 发表于 2025-3-18 17:52:35 | 显示全部楼层
def targetSoPath = "~/video.so"
try {
    loadNativeLibrary(targetSoPath)   
} catch (Exception e) {
    System.exit(1)
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Android开发工具,脚本 ( 粤ICP备2024275843号 )

GMT+8, 2026-4-16 02:52 , Processed in 0.019875 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表