Android Studio NDK编译Linux可执行文件

  • 一般咱们Android开发者开发ndk项目是编译成动态库给apk使用,有时候可能须要编译成Linux下的可执行文件,经过VS链接到远程服务器也是能够作到的,这里记录一个直接使用AS和CMakeLists直接编译Linux下可执行文件的方法。
  1. 将CMakeLists里面add_library改成add_executable,例:
cmake_minimum_required(VERSION 3.4.1)
#导入当前目录下所有的.cpp文件进行编译
file(GLOB src-files
        ${CMAKE_SOURCE_DIR}/*.cpp
        )

add_executable( # Sets the name of the library.
            ShellTool

             # Sets the library as a shared library.
             #SHARED

             # Provides a relative path to your source file(s).
             ${src-files} )

复制代码
  1. 修改build.gradle里面defaultConfig节点
defaultConfig {
        applicationId "com.nani.ipashelldetec"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
            //添加-static选项
                cppFlags "-std=c++11 -static"
            }
            ndk{
            //选择须要编译的架构
                abiFilters "x86_64","arm64-v8a"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
复制代码

3.执行build就能够在对应目录找到可执行文件了,选择对应的架构,通常咱们Linux是x86_64平台的。android

相关文章
相关标签/搜索