首页 > 编程知识 正文

安卓替换so库,安卓系统so库

时间:2023-05-04 14:57:21 阅读:48506 作者:2854

当前AS版本:3.1.3

0、制作前

请在使用之前下载CMake、LLDB和NDK工具包。 用SDK工具下载就可以了。

1、项目编制

项目的创建方式与常规项目相同,但我记得最初选中了Include C support

15这里是文章图片024795.jpg

以下是创建后获得的项目目录。

image.png

cpp包比常规项目多。 app目录下是CMakeLists.txt

2、创建jni调用类和生成so库

在典型的新项目中,example将引用。 在cpp目录下有一个native-lib.cpp,可以通过引用来实现。

举个例子吧

(1)创建jni调用类:

package com.example.aria.jniapp;

公共类JNI测试{

publicnativevoidset (字符串;

公共本机字符串get (;

}

)2)创建相应的cpp :

在main/cpp文件下创建test.cpp文件。 名称是自由的,有关cpp的具体命名和创建规则,请参见native_lib.cpp

#包含

#包含

#ifdef __cplusplus

扩展' c ' {

#endif

jniexportjstringjnicalljava _ com _ example _ aria _ JNI app _ JNI test _ get (JNI env * env,jobject thiz ) {

printf(invokegetinc(n );

返回env-new string utf (hellofromjniintlibjni-test.so! ' );

}

jniexportvoidjnicalljava _ com _ example _ aria _ JNI app _ JNI test _ set (JNI env * env,jobject thiz,jstring string )

打印机(invokesetfromc (n ) );

char*str=(char* ) env-getstringutfchars (字符串,NULL );

printf(%s(n )、str );

env-releasestringutfchars (字符串,str );

}

#ifdef __cplusplus

}

#endif

这里必须注意的是方法的命名约定。

)3)修改CMakeLists.txt

这里主要告诉AS需要编译的文件

# formoreinformationaboutusingcmakewithandroidstudio,read the

# documentation :3359 d.Android.com/studio/projects/add-native-code.html

# setstheminimumversionofcmakerequiredtobuildthenativelibrary。

cmake _ minimum _ required (版本3.4.1 ) )。

# Creates and names a library,sets it as either STATIC

# or SHARED,andprovidestherelativepathstoitssourcecode。

# You can define multiple libraries,and CMake builds them for you

# gradleautomaticallypackagessharedlibrarieswithyourapk。

add _ library (# setsthenameofthelibrary .其实它的备注中写得很清楚。 这里是最后生成的so库的名称

本地实验室

# setsthelibraryasasharedlibrary。

共享游戏

# providesarelativepathtoyoursourcefile (s ) .在此处保留要打入so库的cpp文件

src/main/cpp/native-lib.cpp

src/main/cpp/test.cpp )

# searchesforaspecifiedprebuiltlibraryandstoresthepathasa

# variable.becausecmakeincludessystemlibrariesinthesearchpathby

#默认,youonlyneedtospecifythenameofthepublicndklibrary

# you want toadd.cmakeverifiesthatthelibraryexistsbefore

#完成its构建。

find _ library (# setsthenameofthepathvariable。

log-lib

# specifiesthenameofthendklibrarythat

# you want CMake to locate。

log )

# specifieslibrariescmakeshouldlinktoyourtargetlibrary.you

# can link多数据库,such as libraries you define in this

# build script,prebuilt third-party libraries,or系统库。

target _ link _ libraries (# specifiesthetargetlibrary。

本地实验室

# linksthetargetlibrarytotheloglibrary

# included in the NDK。

${log-lib} )

要根据公式提示修改CMakeLists文件,一般修改find_library函数即可。

修改并编译后,将得到编译后的so库文件。 的路径位于build/intermediates/cmake/debug/obj /下,每个版本都有相应的so库文件,并根据需要进行选择。

3、jni呼叫

publicclassmainactivityextendsappcompatactivity {

//usedtoloadthe ' native-lib ' libraryonapplicationstartup.JNI库

静态{

system.loadlibrary('native-lib ';

}

@Override

protectedvoidoncreate (边界维护实例state )。

super.oncreate (savedinstancestate;

setcontentview (r.layout.activity _ main;

//exampleofacalltoanativemethod

textview TV=findviewbyid (r.id.sample _ text;

JNI测试JNI test=new JNI test (;

电视. settext (newjnitest ).get );

}

}

4、总结

轻松整理了当前版本的jni编译流程,并做了记录,便于以后查看。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。