# 手动编译OpenCV 和OpenCV_contrib android版本
## 安装编译工具
### 基础工具安装
```sh
sudo apt install build-essential python wget git python-pip
pip install ninja
sudo apt install ant libcurl4-gnutls-dev zlib1g-dev
```
### cmake 安装
```sh
wget --no-check-certificate https://github.com/Kitware/CMake/releases/download/v3.22.1/cmake-3.22.1.tar.gz
tar -zxvf cmake-3.22.1.tar.gz
cd cmake-3.22.1
./bootstrap --system-curl
make -j8   # 8为CPU核心数
sudo make install
cmake --version  #查看是否正常安装cmake

```

### [Ccache ](https://ccache.dev/)安装

```sh
wget --no-check-certificate https://github.com/ccache/ccache/releases/download/v3.7.12/ccache-3.7.12.tar.gz
tar -zxvf ccache-3.7.12.tar.gz
cd ccache-3.7.12
./configure
make -j8 # 8为CPU核心数
sudo make install
```

### Android sdk ndk Java jdk安装 
* 下载SDK    wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
*  [NDK](https://github.com/android/ndk/wiki/Unsupported-Downloads) [JDK](https://www.oracle.com/java/technologies/downloads/)文件并解压文件,修改.bashrc文件，添加如下内容

```sh
export ANDROID_SDK=/home/ehigh/android_sdk   #sdk路径
export PATH=$ANDROID_SDK/tools:$PATH
export ANDROID_NDK=/home/ehigh/android_ndk   #ndk路径
export PATH=${PATH}:$ANDROID_NDK
export JAVA_HOME=/home/ehigh/java8        # jdk路径
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
# 保存后执行 source .bashrc 使修改生效
android -h # 验证Android sdk 版本
```

* 安装需要的组件版本

```sh
android update sdk -u --all --filter  29,62
```

## opencv 代码下载&& 配置
* 代码下载
```sh
mkdir android_opencv
cd android_opencv
git clone https://github.com/opencv/opencv_contrib.git
git clone https://github.com/opencv/opencv.git
```
* NDK配置修改
修改文件 /opencv/platforms/android/ndk-22.config.py ,修改GRADLE版本为Android项目的版本
```sh
ABIs = [
    ABI("1", "arm64-v8a",   None, cmake_vars=dict(ANDROID_GRADLE_PLUGIN_VERSION='4.2.0', GRADLE_VERSION='6.7.1', KOTLIN_PLUGIN_VERSION='1.5.0')),
]
```

* 编译配置修改

  修改文件/opencv/platforms/android/build_sdk.py,修改cmake_var 为如下内容

  ```python
      def build_library(self, abi, do_install):
          cmd = [self.cmake_path, "-GNinja"]
          cmake_vars = dict(
              CMAKE_TOOLCHAIN_FILE=self.get_toolchain_file(),
              INSTALL_CREATE_DISTRIB="ON",
              WITH_OPENCL="ON",
              BUILD_KOTLIN_EXTENSIONS="ON",
              WITH_IPP=("ON" if abi.haveIPP() else "OFF"),
              WITH_TBB="ON",
              OPENCV_ENABLE_NONFREE="ON",  # 添加这一行,添加编译opencv_contrib
              BUILD_EXAMPLES="OFF",
              BUILD_TESTS="OFF",
              BUILD_PERF_TESTS="OFF",
              BUILD_DOCS="OFF",
              BUILD_ANDROID_EXAMPLES="ON",
              INSTALL_ANDROID_EXAMPLES="OFF",
          )
  ```
## 编译代码

```sh
cd android_opencv
mkdir opencv_build
cd opencv_build
python ../opencv/platforms/android/build_sdk.py  --extra_modules_path=../opencv_contrib/modules/  --config /home/yewuya/opencv/platforms/android/ndk-22.config.py       # ndk-17要与下载的ndk版本对应
```
* 编译完成后opencv_build 文件夹下的OpenCV-android-sdk 即为编译出来的结果

## Android 项目导入opencv sdk

* 修改sdk build.gradle 文件中的compileSdkVersion 30 targetSdkVersion 30为Android项目相同的版本
* 注释 path (project.projectDir.toString() + '/libcxx_helper/CMakeLists.txt')
* 添加jnilib  在app/路径下创建lib文件夹并将sdk\native\libs\arm64-v8a  复制到lib 路径下

```java
    sourceSets {
        main {
            jni.srcDirs=[]
            jniLibs.srcDirs=['libs']  //jnilib 路径
        }
    }
```

* 在项目目录下创建文件夹custom_opencv并将sdk/java 和build.gradle复制到文件夹下
* setting.gradle 添加 include ':custom_opencv' 
* app  build.gradle   添加依赖

 ```java
  implementation project(path: ':custom_opencv')
 ```
