25、Android 移植C++ 开发的第三方.so包和.a包

 基本思想:首先搭建一个Android的最基本工程,完成Android 调用c++ 的代码;

                 然后本地写一个c++的子模块,使用配置好的(arm-linux-androideabi-gcc/g++)进行编译得到.a

                最后将本地编译的.a文件移植到Android工程上,完成Android-->c++ 工程---->(pc端交叉编译的.a) 完成调用;

主要目的: 一是 可以本地开发模块,直接导入Android 使用函数功能即可,二是 可编译第三方开源库,在Android中使用

第一步、首先建立一个基本的Android 工程,然后写入Android调用c++ 的函数关系;

MainActivity 修改了默认的设置

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
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 = findViewById(R.id.sample_text);
        TextView tv = new TextView(this);
        testA aaa=new testA();
        String text=aaa.way();
        tv.setText(text);
        setContentView(tv);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */

    public native int add(int a,int b,int []c);
}

testA文件内容

package com.example.myapplication;

public class testA {

    public String  way(){

        //tv.setText(stringFromJNI());
        int c[] ={1,2,3,4,5,6,7,8,8,9,9,1};
        int sum = add(2, 3,c);
        return "2 + 3 = " + Integer.toString(sum);
    }

    public native int add(int a,int b,int []c);
}

native-lib.cpp文件

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

#include "info.h"
//#include <Eigen/Dense>
using namespace std;
extern "C" {


int total(int t,int g) {
    return t*g;
}



JNIEXPORT jint JNICALL
Java_com_example_myapplication_testA_add( JNIEnv*  env,
                                                  jobject  ,
                                                  jint a,
                                                  jint b,
                                                  jintArray arr)
{
    jint *carr;
    carr = env->GetIntArrayElements(arr, JNI_FALSE);

    if(carr == NULL) {
        return 0;
    }

    jint sum = 0;
    for(int i=0; i<10; i++) {
        sum += carr[i];
    }
    INFO *info= new INFO();
    int c=info->compute(a,b);
    delete info;
    int z=total(c,c);
    return (z+sum);
}

}

info.h头文件

#ifndef MY_APPLICATION_INFO_H
#define MY_APPLICATION_INFO_H

class INFO
{

public:
    int compute(int a,int b);

};
#endif //MY_APPLICATION_INFO_H

info.cpp文件

//
#include "info.h"

int INFO::compute(int a,int b)
{

    return a+b;
}

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).
        native-lib.cpp
        info.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}
        )

然后点击允执行效果如下(使用sevensquare 连接手机,点击Android stdio 运行,Android Stdio 环境配置自己解决)

启用Android stdio的默认工程讲述

然后在本第编译XXX.a文件;首先在ubuntu 16.04 下载https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip建立交叉编译器,


ubutnu@ubuntu:axel -n 100 https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip

ubuntu@ubutnu:~$ unzip android-ndk-r17c-linux-x86_64.zip

ubuntu@ubutnu:~$ sudo cp -r android-ndk-r17c /usr/local/



ubuntu@ubutnu:/usr/local/android-ndk-r17c$ sudo ./build/tools/make-standalone-toolchain.sh --package-dir=/usr/local --toolchain=arm-linux-androideabi-4.9
HOST_OS=linux
HOST_EXE=
HOST_ARCH=x86_64
HOST_TAG=linux-x86_64
HOST_NUM_CPUS=12
BUILD_NUM_CPUS=24
Auto-config: --arch=arm
Package installed to /usr/local.

ubuntu@ubutnu:/usr/local$ sudo tar -jxvf arm-linux-androideabi.tar.bz2 

ubuntu@ubutnu:/usr/local/arm-linux-androideabi/bin$ pwd
/usr/local/arm-linux-androideabi/bin

ubuntu@ubutnu:/usr/local/android-ndk-r17c$ pwd
/usr/local/android-ndk-r17c


填入环境变量
ubuntu@ubutnu:/usr/local$ sudo vim ~/.bashrc
ubuntu@ubutnu:/usr/local$ source ~/.bashrc

export ANDROID_NDK=/usr/local/android-ndk-r17c
export ARM_TOOL=/usr/local/arm-linux-androideabi/bin
export PATH=$ANDROID_NDK:$ARM_TOOL:$PATH

 

查看编译是否成功

ubuntu@ubutnu:/usr/local$ arm + TAB 键 
ubuntu@ubutnu:/usr/local$ arm
arm                                 arm-none-linux-gnueabi-addr2line
arm2hpdl                            arm-none-linux-gnueabi-ar
arm-linux-addr2line                 arm-none-linux-gnueabi-as
arm-linux-androideabi-addr2line     arm-none-linux-gnueabi-c++
arm-linux-androideabi-ar            arm-none-linux-gnueabi-c++filt
arm-linux-androideabi-as            arm-none-linux-gnueabi-cpp
arm-linux-androideabi-c++filt       arm-none-linux-gnueabi-g++
arm-linux-androideabi-clang         arm-none-linux-gnueabi-gcc
arm-linux-androideab
....

在本地写一个function.cpp文件

 

 
#include<stdio.h>
 
int fun(int a,int b)
{
return a+b;
}
  
ubutnu@ubuntu: arm-linux-androideabi-g++ -shared -fPIC function.cpp -o libfunction.so 

上述要注意,一定要生成libxxx.so 否则找不到这个动态库文件,然后 新建一个Android 的工程

ubuntu@ubuntu:MyApplication2$ tree -l
.
├── app
│   ├── app.iml
│   ├── build
│   │   ├── generated
│   │   │   └── source
│   │   │       └── buildConfig
│   │   │           ├── androidTest
│   │   │           │   └── debug
│   │   │           │       └── com
│   │   │           │           └── example
│   │   │           │               └── myapplication
│   │   │           │                   └── test
│   │   │           │                       └── BuildConfig.java
│   │   │           └── debug
│   │   │               └── com
│   │   │                   └── example
│   │   │                       └── myapplication
│   │   │                           └── BuildConfig.java
│   │   ├── intermediates
│   │   │   ├── apk_list
│   │   │   │   └── debug
│   │   │   │       └── mainApkListPersistenceDebug
│   │   │   │           └── apk-list.gson
│   │   │   ├── bundle_manifest
│   │   │   │   └── debug
│   │   │   │       └── processDebugManifest
│   │   │   │           └── bundle-manifest
│   │   │   │               ├── AndroidManifest.xml
│   │   │   │               └── output.json
│   │   │   ├── check_manifest_result
│   │   │   │   └── debug
│   │   │   │       └── checkDebugManifest
│   │   │   │           └── out
│   │   │   ├── cmake
│   │   │   │   └── debug
│   │   │   │       └── obj
│   │   │   │           └── x86
│   │   │   ├── compatible_screen_manifest
│   │   │   │   └── debug
│   │   │   │       └── createDebugCompatibleScreenManifests
│   │   │   │           └── out
│   │   │   │               └── output.json
│   │   │   ├── instant_app_manifest
│   │   │   │   └── debug
│   │   │   │       ├── AndroidManifest.xml
│   │   │   │       └── output.json
│   │   │   ├── lint_jar
│   │   │   │   └── global
│   │   │   │       └── prepareLintJar
│   │   │   ├── lint_publish_jar
│   │   │   │   └── global
│   │   │   │       └── prepareLintJarForPublish
│   │   │   ├── manifest_merge_blame_file
│   │   │   │   ├── debug
│   │   │   │   │   └── manifest-merger-blame-debug-report.txt
│   │   │   │   └── debugAndroidTest
│   │   │   │       └── manifest-merger-blame-debug-androidTest-report.txt
│   │   │   ├── merged_manifests
│   │   │   │   ├── debug
│   │   │   │   │   ├── AndroidManifest.xml
│   │   │   │   │   └── output.json
│   │   │   │   └── debugAndroidTest
│   │   │   │       ├── AndroidManifest.xml
│   │   │   │       └── output.json
│   │   │   ├── metadata_feature_manifest
│   │   │   │   └── debug
│   │   │   │       └── processDebugManifest
│   │   │   │           └── metadata-feature
│   │   │   │               └── output.json
│   │   │   └── tmp
│   │   │       └── manifest
│   │   │           └── androidTest
│   │   │               └── debug
│   │   └── outputs
│   │       └── logs
│   │           └── manifest-merger-debug-report.txt
│   ├── build.gradle
│   ├── libs
│   ├── proguard-rules.pro
│   └── src
│       ├── androidTest
│       │   └── java
│       │       └── com
│       │           └── example
│       │               └── myapplication
│       │                   └── ExampleInstrumentedTest.java
│       ├── main
│       │   ├── AndroidManifest.xml
│       │   ├── cpp
│       │   │   ├── CMakeLists.txt
│       │   │   └── native-lib.cpp
│       │   ├── java
│       │   │   └── com
│       │   │       └── example
│       │   │           └── myapplication
│       │   │               └── MainActivity.java
│       │   └── res
│       │       ├── drawable
│       │       │   └── ic_launcher_background.xml
│       │       ├── drawable-v24
│       │       │   └── ic_launcher_foreground.xml
│       │       ├── layout
│       │       │   └── activity_main.xml
│       │       ├── mipmap-anydpi-v26
│       │       │   ├── ic_launcher_round.xml
│       │       │   └── ic_launcher.xml
│       │       ├── mipmap-hdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-mdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-xhdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-xxhdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       ├── mipmap-xxxhdpi
│       │       │   ├── ic_launcher.png
│       │       │   └── ic_launcher_round.png
│       │       └── values
│       │           ├── colors.xml
│       │           ├── strings.xml
│       │           └── styles.xml
│       └── test
│           └── java
│               └── com
│                   └── example
│                       └── myapplication
│                           └── ExampleUnitTest.java
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── local.properties
├── My Application.iml
└── settings.gradle

90 directories, 52 files

工程界面

在这个目录下建立一个文件夹,将so文件拖入进去,参考(https://www.cnblogs.com/leipDao/p/10685302.html) 动态库只能建立在该文件夹下 jniLibs,静态库无所谓~~

 

然后将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.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a")

target_link_libraries( # Specifies the target library.
        native-lib
        function

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

因为移动了CMakeLIsts.txt的位置,需要修改一下这个文件build.gradle

然后在代码中写入调用的函数即可

native-lib.cpp 文件

 

然后点击运行就可以调用了,这里没有打印显示

 

静态库的编译

 

ubuntu@ubuntu:~/project/src$ arm-linux-androideabi-g++ -shared -fPIC function.cpp -o libfunction.so 
ubuntu@ubuntu:~/project/src$ ls
function.cpp  libfunction.so  Untitled Document
ubuntu@ubuntu:~/project/src$ arm-linux-androideabi-ar r staticfunction.a libfunction.so 
arm-linux-androideabi-ar: creating staticfunction.a

填入工程目录

 

然后修改对应的文件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)
add_library(function STATIC IMPORTED)

set_target_properties(function PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/staticlibs/staticfunction.a)

# 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
        function
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

可以运行了,结果正常~~

感谢博客及其博主的参考~~ 致谢

https://www.cnblogs.com/leipDao/p/10685302.html

https://www.jianshu.com/p/fbedb32e242b

https://www.jianshu.com/p/30e040468721?utm_campaign=hugo

 

海思平台上USB WIFI的移植与局域网无线调试和视频流预览-第5/11季视频课程

06-13
本季课程在开发板上移植USB WIFI模块(套装附带的MT7601模块),工作在ap模式下,并且用电脑通过WIFI去连接开发板实现主机和开发板的无线网络局域网通信,这样就可以用WIFI网络取代板载ethernet有线网络方式来进行调试和局域网视频预览。现在很多的视频摄像头产品都要求支持无线网络连接功能,本季课程主要是针对这方面开发技术讲解的。 随着通信行业发展,网速越来越快,网络也从文本时代发展越过语音时代到了现在视频时代,“优酷、爱奇艺”、“微视频”、“直播”等的出现也是视频逐步成为主流媒体的证据和表现。航拍、视频监控、刷脸过关检测、汽车ADAS系统等应用也是视频在主流行业中的应用,因此视频的采集、编解码、传输、识别等技术变得越来越有价值,涌现出了“海康威视”、“大华股份”、“深圳英飞拓”等一批明星企业,名扬海内外,动辄市值几百亿。同时在芯片级,国产华为海思HI35XX系列视频编解码方案也逐步取代进口芯片(如TI、安霸)成为主流方案。 视频行业技术含量高、难度大、学习周期长、发展变化快而资料少,因此行业比较缺人、工资较高。如海康威视,普通高校硕士研究生应届进入年薪普遍高于15-20万,在嵌入式linux领域也算高工资技术方向了。 本项目是朱老师及其团队推出的一个嵌入式linux领域重量级企业级实战项目,选用华为海思HI3518E方案,结合OV9712/AR0130 Sensor实现图像采集和H.264编码压缩,并通过ethernet和WIFI,以socket实时流和RTSP等方式,实现720P/960P高清视频传输。本项目共分11季,从海思SDK开发环境搭建,到sample程序讲解、SDK中API函数解析,到H.264编解码、RTSP流媒体传输、MP4文件打,到图像IQ调试、图像识别等视频领域高阶内容,可以说从零基础入手,对图像采集、编解码、网络传输、图像存储和识别做了全方位的详细讲解和代码分析,是目前市面上**一套系统讲解图像和视频技术的视频课程。
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值