Android Studio3.0开发JNI流程------在Android原程序添加自己类的native多个so的方法

本文介绍了在Android Studio 3.0中使用JNI调用C++代码改变TextView内容的流程。通过创建源程序、添加C++代码,以及配置不同的库,实现了多个SO库的运行。感谢@螃蟹变异了提供的解决方案,程序源代码可在优快云下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一个关联C/C++的Android程序,创建流程http://blog.youkuaiyun.com/cloverjf/article/details/78652245

源程序是调用C++代码来改变TextView的文本内容。

博主删除了在这之前的办法,因为不好用。
感谢@螃蟹变异了 大佬提供的多个so库同时运行的解决办法。下面博主给出解决方案,再次感谢@螃蟹变异了。

以下只提供代码,应该很容易懂…
MainActivity.java文件代码:

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    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 = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());

        // 在 MainActivity类中调用MyTest类的native方法
        TextView tv1 = (TextView) findViewById(R.id.sample_text1);  //
        tv1.setText(new MyTest().stringFromJNI1());   // "Hello from C++  ===  native-test-lib库";

    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();   // 在app\src\main\cpp\native-lib.cpp
}

MyTest.java类:

public class MyTest {

    static {
        System.loadLibrary("native-test-lib");
    }

    public native String stringFromJNI1();  // 在app\src\main\cpp\main.cpp
}

在app\src\main\cpp目录下创建两个.app文件:
native-lib.cpp文件内容:

#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring

JNICALL
Java_wvs_mm_tencent_com_testapplication_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++  === 来自native-lib库";
    return env->NewStringUTF(hello.c_str());
}

main.cpp文件内容:

#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring

JNICALL
Java_wvs_mm_tencent_com_testapplication_MyTest_stringFromJNI1(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++  ===  来自native-test-lib库";
    return env->NewStringUTF(hello.c_str());
}

主要的处理不同so的实在配置文件中:

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

add_library( # Sets the name of the library.

            native-test-lib

            # Sets the library as a shared library.
            SHARED

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

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

主要是使用两个add_library来添加不同的库。

运行结果:
这里写图片描述

在此,再次感谢@螃蟹变异了,好了,留下代码,赠人玫瑰,手留余香……

程序源代码:http://download.youkuaiyun.com/download/cloverjf/10262095

下一章:研究怎么在Android程序中添加JNI开发流程。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值