Android studio Jni调用第三方库(CMakelist)

时间:2024-04-03 16:01:41



Jni项目生成的库文件需要调用硬件给的c++编写的so库,拿到so库跟头文件,jni通过CMakelist方式怎么调用c++写的so文件。。。

 

首先,创建项目,勾选support c/c++选项


Android studio Jni调用第三方库(CMakelist)


Android studio Jni调用第三方库(CMakelist)Android studio Jni调用第三方库(CMakelist)


Android studio Jni调用第三方库(CMakelist)Android studio Jni调用第三方库(CMakelist)

Android studio Jni调用第三方库(CMakelist)


接下来,就是配置CMakeList.text文件:

Android studio Jni调用第三方库(CMakelist)


下面附上CMakeList的详细代码:

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

#引用已经有的库
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 )

#资源文件夹的位置libs
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)

#导入类库,只是作为引用,不编译
add_library( first
             SHARED
             IMPORTED )

#引用目标类库是本地类库位置在libs/armeabi-v7a/xxx.so
set_target_properties( first
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libfirst.so )

#添加类库位置在src/main/cpp/xxx.cpp需要编译
add_library(native-lib
             SHARED
             src/main/cpp/native-lib.cpp )

#引入头文件目录位置
include_directories(libs/jpeg)

#将预构建库与你本地库相关联
target_link_libraries( # Specifies the target library.
                       native-lib first

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

然后点击MakeProject:

Android studio Jni调用第三方库(CMakelist)Android studio Jni调用第三方库(CMakelist)


这时,如果没出什么意外的话,我们可以在 t\app\build\intermediates\cmake\debug\obj 下查看到生成的so文件

Android studio Jni调用第三方库(CMakelist)Android studio Jni调用第三方库(CMakelist)


我们的Android项目即可调用此库做相应的操作了

但是要注意一点,因为我们的libnative-lib.so是依赖libfirst.so生成的,所以我们调用的时候需要将两个库都加载进去,不然会报错

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("first");
        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 = (TextView) findViewById(R.id.sample_text);
        tv.setText(helloString("hi   kathy"));
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    public native String helloString(String hello);

}


效果如图:

Android studio Jni调用第三方库(CMakelist)

Android studio Jni调用第三方库(CMakelist)

ok,that is all

不懂的可以下载相应的demo自行查看

有关其他的jni问题可以提出来相互讨论,刚刚接触,共同学习,共同进步

thanks~


DEMO下载:http://download.csdn.net/download/qq_35532751/10138080