Android Studio3.0开发JNI流程------在Android原程序添加自己类的native多个so的方法
16lz
2021-01-23
创建一个关联C/C++的Android程序,创建流程http://blog.csdn.net/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 #include extern "C"JNIEXPORT jstringJNICALLJava_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 #include extern "C"JNIEXPORT jstringJNICALLJava_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.csdn.net/download/cloverjf/10262095
下一章:研究怎么在Android程序中添加JNI开发流程。
更多相关文章
- Android 的 那些 秘密代码
- Android res/raw文件以及raw与res/assets异同
- Windows环境下编译Assimp库生成Android可用的.so文件
- Android超炫图片浏览器代码
- [eclipse]android开发如何查看源代码文件(android source)
- Android之A面试题④应用程序内部启动Activity过程(startActivity)
- 第101讲:Android源代码下载指南(图解)
- android DDMS 连接真机(己ROOT),用file explore看不到data/data文
- Cocos2d-x在win32,android和IOS下的文件读写问题