zoukankan      html  css  js  c++  java
  • Android Studio CMake 生成多个so

    生成多个so案例

    这里stringFromJNIstringFromJNI11分别是调用one-lib和two-lib两个so

    package com.test.ndkmoreso;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("one-lib");
            System.loadLibrary("two-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()+"   two"+stringFromJNI11());
        }
    
        /**
         * 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 stringFromJNI11();
    }

    cpp的目录结构:

    直接看1的CMakeLists.txt文件

    cmake_minimum_required(VERSION 3.4.1)
    
    #添加子目录,将会调用子目录中的CMakeLists.txt
    
    SET(ONE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/one)
    SET(TWO_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/two)
    
    MESSAGE(STATUS “src  dir: ${CMAKE_CURRENT_SOURCE_DIR}”)
    
    MESSAGE(STATUS “one  dir: ${ONE_PATH}”)
    MESSAGE(STATUS “two  dir: ${TWO_PATH}”)
    
    ADD_SUBDIRECTORY(${ONE_PATH})
    
    ADD_SUBDIRECTORY(${TWO_PATH})

    one的lib

    2的CMakeLists.txt文件

    cmake_minimum_required(VERSION 3.4.1)
    
    #生成so动态库
    ADD_LIBRARY(one-lib SHARED one.cpp)
    
    target_link_libraries(one-lib log)

    one.cpp

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

    two的lib

    查看3的CMakeLists.txt文件

    cmake_minimum_required(VERSION 3.4.1)
    
    #生成so动态库
    ADD_LIBRARY(two-lib SHARED two.cpp)
    
    target_link_libraries(two-lib log)

    two.cpp

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

    生成的结果如下

  • 相关阅读:
    PHP浮点数引起的四舍五入问题
    几项有用的JQUERY代码
    php5.5新特性之yield理解
    【转】循环、迭代、遍历和递归
    phpexcel导入excel文件报the filename xxx is not recognised as an OLE file错误。
    php缩放gif和png格式透明背景变成黑色的解决方法
    javascript统计输入文本的简易方法
    分享几个实用的jquery工具函数
    thinkphp中ajax技术
    正斜杠与反斜杠
  • 原文地址:https://www.cnblogs.com/mingfeng002/p/7201034.html
Copyright © 2011-2022 走看看