android so文件的作用,Android so库的使用

当前AS版本:3.1.3

0、创建前

在使用前应先下载 CMake、LLDB、NDK工具包。在SDK Tools里下载即可。

1、项目创建

项目创建同普通项目,只是在一开始记得勾选 Include C++ support

ce86b5721872

1533352024795.jpg

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

ce86b5721872

image.png

比普通的项目会多出cpp包,app目录下还有一个CMakeLists.txt

2、编写jni调用类及生成so库

一般新建的项目中它会提供一个example给你参考,在cpp目录下会有个native-lib.cpp,可以参照它来实现。

下面举个例子

(1)编写jni调用类:

package com.example.aria.jniapp;

public class JniTest {

public native void set(String str);

public native String get();

}

(2)创建对应的cpp:

在main/cpp文件下创建test.cpp文件,名字随意,cpp的具体命名规则以及编写规则可以参考native_lib.cpp

#include

#include

#ifdef __cplusplus

extern "C" {

#endif

JNIEXPORT jstring JNICALL Java_com_example_aria_jniapp_JniTest_get(JNIEnv *env, jobject thiz) {

printf("invoke get in c++\n");

return env->NewStringUTF("Hello from JNI int libjni-test.so!");

}

JNIEXPORT void JNICALL Java_com_example_aria_jniapp_JniTest_set(JNIEnv *env, jobject thiz, jstring string) {

printf("invoke set from c++\n");

char* str = (char*)env->GetStringUTFChars(string, NULL);

printf("%s\n", str);

env->ReleaseStringUTFChars(string, str);

}

#ifdef __cplusplus

}

#endif

这里要注意的是方法的命名规则。

(3)修改CMakeLists.txt

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

# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

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

# or SHARED, and provides the relative paths to its source code.

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

# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.其实它备注里都写的听清楚了,这里是最后要生成的so库的名字

native-lib

# Sets the library as a shared library.

SHARED

# Provides a relative path to your source file(s).这里留下你要打入so库的cpp文件

src/main/cpp/native-lib.cpp

src/main/cpp/test.cpp)

# Searches for a specified prebuilt library and stores the path as a

# variable. Because CMake includes system libraries in the search path by

# default, you only need to specify the name of the public NDK library

# you want to add. CMake verifies that the library exists before

# completing its build.

find_library( # Sets the name of the path variable.

log-lib

# Specifies the name of the NDK library that

# you want CMake to locate.

log )

# Specifies libraries CMake should link to your target library. You

# can link multiple libraries, such as libraries you define in this

# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.

native-lib

# Links the target library to the log library

# included in the NDK.

${log-lib} )

根据官方的提示修改CMakeLists文件,一般只要修改find_library函数就行了。

修改后编译一下,就可以得到编译后的so库文件了。文件的路径在build/intermediates/cmake/debug/obj/下,有各个版本下对应的so库文件,按需选择。

3、jni调用

public class MainActivity extends AppCompatActivity {

// Used to load the 'native-lib' library on application startup.加载jni库

static {

System.loadLibrary("native-lib");

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Example of a call to a native method

TextView tv = findViewById(R.id.sample_text);

JniTest jniTest = new JniTest();

tv.setText(new JniTest().get());

}

}

4、总结

简单整理了当前版本jni编译的流程,做个记录方便以后看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值