android将so打到jar包中并运行

1 篇文章 0 订阅

加载so有两种方法

System.load() 和System.loadLibrary(); 前者需传入库文件的绝对路径,后者只需传入库文件名。

首先我的jar包目录如下:

Loader是加载类:

 static {
        try {
            InputStream is = null;
            if(isCPUInfo64()) {
                is = Loader.class.getResource("arm64/libhellojni.so").openStream();
            }else {
                is = Loader.class.getResource("arm32/libhellojni.so").openStream();
            }
            File tempFile = File.createTempFile("hellojni", ".so");
            FileOutputStream fos = new FileOutputStream(tempFile);
            int i;
            byte[] buf = new byte[1024];
            while ((i = is.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
            is.close();
            fos.close();
            System.load(tempFile.getAbsolutePath());
            tempFile.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

private static boolean isCPUInfo64() {
        File cpuInfo = new File("/proc/cpuinfo");
        if (cpuInfo != null && cpuInfo.exists()) {
            InputStream inputStream = null;
            BufferedReader bufferedReader = null;
            try {
                inputStream = new FileInputStream(cpuInfo);
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 512);
                String line = bufferedReader.readLine();
                if (line != null && line.length() > 0 && line.toLowerCase(Locale.US).contains("arch64")) {
                    return true;
                } else {
                    return false;
                }
            } catch (Throwable t) {
                Log.d("isCPUInfo64", " error = " + t.toString());
            } finally {
                try {
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将外部Jar包引入到Spring Boot项目并打包到Jar包,可以按照以下步骤进行操作: 1. 在pom.xml文件添加依赖项。例如,要引入一个名为example.jar的外部Jar包,可以通过以下方式添加依赖项: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/example.jar</systemPath> </dependency> ``` 其,systemPath指定了外部Jar包的路径,scope设置为system,表示使用系统路径下的Jar包。 2. 将外部Jar包复制到项目的lib目录下,例如,将example.jar复制到项目目录下的lib文件。 3. 在pom.xml文件添加Maven插件,以将外部Jar包打包到生成的Jar包。例如,可以添加以下插件: ``` <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.Application</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> ``` 其,classpathPrefix指定了Jar包lib文件夹下的依赖项路径前缀,mainClass指定了Spring Boot应用程序的主类。 4. 使用Maven命令进行打包,生成的Jar包将包含外部Jar包。例如,使用以下命令进行打包: ``` mvn clean package ``` 这样,生成的Jar包就包含了外部Jar包,并可以在运行时使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值