android studio 3.5.1 使用第三方.so 库

1.在app/src/main 路径下添加文件夹 jniLibs ,将.so 文件和文件夹 拷贝到该文件夹下
android studio 3.5.1 使用第三方.so 库_第1张图片

  1. app里的build.gradle 添加如下
    android studio 3.5.1 使用第三方.so 库_第2张图片
  2. main文件夹新建cpp,添加CMakeLists.txt 和 lalala.cpp(这个文件是在制作.so文件时用来产生.h文件)
    android studio 3.5.1 使用第三方.so 库_第3张图片
    4.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
        #lalala
        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        lalala.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
        #lalala
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

5.lalala.cpp

#include 
#include 

using namespace std;

extern "C" JNIEXPORT jstring JNICALL
Java_com_snailman06_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_snailman06_MainActivity_sayHello(JNIEnv *env, jobject thiz) {
    return env->NewStringUTF("我的天");
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_snailman06_MainActivity_add(JNIEnv *env, jobject thiz, jint num1, jint num2) {


    return num1+num2;
}

6.调用native方法

package com.snailman06;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // 引用so库,注意去掉前面的lib,和后缀.so
    static {
        System.loadLibrary("lalala");
    }

    @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);
        tv.setText("总数是:"+add(111,2222));
    }


    //编写native方法
    public native int add(int num1,int num2);
}

android studio 3.5.1 使用第三方.so 库_第4张图片

你可能感兴趣的:(android,日记)