您现在的位置是:网站首页> Android

简单理理AndroidStudio 生成SO并调用

  • Android
  • 2023-08-25
  • 473人已阅读
摘要

工程结构如下

1.png

文件结构如下:

1.png


在调用的地方定义在 MainActivity类里



public class MainActivity extends AppCompatActivity {


    // Used to load the 'native-lib' library on application startup.

    static {

        System.loadLibrary("native-lib");

    }

public native String stringFromJNI();

...

}


在native-lib.cpp中实现


#include <jni.h>

#include <string>


extern "C" JNIEXPORT jstring JNICALL

Java_com_xn_helloso_MainActivity_stringFromJNI(

        JNIEnv* env,

        jobject /* this */) {

    std::string hello = "Hello from C++";

    return env->NewStringUTF(hello.c_str());

}


函数名称就是Java+包地址+类+函数名

编译需要在module下的build.gradle文件里加

externalNativeBuild {

        cmake {

            path "CMakeLists.txt"

        }

    }

生成的so动态库在:app\build\intermediates\cmake\debug\obj

CMakeLists.txt文件内容


# 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.

             native-lib


             # Sets the library as a shared library.

             SHARED


             # Provides a relative path to your source file(s).

             src/main/cpp/native-lib.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} )




AndroidStudio引用第三方so库的正确姿势

1、把so文件复制到 \app1\app\libs\ 文件夹下,但是要注意,so文件是放在对应的平台文件夹之下(如arm64-v8a,armeabi-v7a, x86,x86_64),这点非常重要,否则不能成功引用,每个平台文件夹下都放上该so文件,如下图:

1.png


2.png


 


2、AndroidStudio打开项目,并切换到 Android 栏,并打开Gradle Scripts\build.gradle(Module:app1.app) ,加入 节点


sourceSets{

    main{

        jniLibs.srcDirs "libs"

    }

}

如下图:

3.png 


 


3、加完之后,有一个刷新(同步)的操作 ,之后在app下就可以看到jniLibs文件夹,如下:

4.png


通过JNI定义实现调用第三方库so如上面的native-lib.cpp文件


Top