最小的Android CMake构建 [英] Minimal CMake building for android
问题描述
我正在尝试将本机C ++ DLL移植为Android上的共享库。对于构建,我使用的是VSCode,而不是Visual Studio。下载了Android NDK r18b。我的项目基于CMake,当我尝试使用 NMake Mmakefiles生成器生成它时,总是会收到此错误:
I'm trying to port a native C++ DLL to be a shared library on Android. For building, I am using VSCode, not using Visual Studio. Downloaded the Android NDK r18b. My project is CMake based, and when I try to generate it to be with the "NMake Mmakefiles" generator, I always get this error:
CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio Edition' is not installed.
我创建了一个小项目对此进行测试。
I have created a small project to test this.
main.cpp
int foo( int a, int b ) { return a + b; }
CMakeLists.txt
CMakeLists.txt
cmake_minimum_required( VERSION 3.11.0 )
add_library( Engine SHARED main.cpp )
我使用以下命令行(CMake 3.11.4)运行它:
I run it with this command line (CMake 3.11.4):
cmake -g "NMake Makefiles" .. -DCMAKE_TOOLCHAIN_FILE=%NDK_ROOT%\build\cmake\android.toolchain.cmake -DANDROID_NDK=%NDK_ROOT%
使用此命令,我仍然会收到错误:
With this one, I still get the error:
CMake Error in CMakeLists.txt:
CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio Edition' is not installed.
有人可以帮我把这个小的main.cpp变成Android .so吗?我不想使用Android Studio或其他IDE。寻找创建make文件并将其添加到我的VSCode版本中。
Can someone help me to make this small main.cpp into an Android .so? I'd not like to use Android Studio nor an other IDE. Looking for creating a make file and add it to my VSCode build.
谢谢。
推荐答案
如果您愿意使用 Gradle 来配置cmake ...
In case you were willing to use Gradle to configure your cmake...
我使用Gradle Wrapper,在安装gradle之后,您可以在目录中调用 gradle wrapper
,它将安装一个本地副本。可以将副本设置为特定版本,也可以升级。
I use the Gradle Wrapper, after you install gradle you can call gradle wrapper
in a directory and it will install a local copy. This copy can be set at a specific version or can be upgraded.
Android Gradle插件使用忍者构建系统来配置cmake和目标ABI。以下脚本将为所有受支持的平台类型生成ABI,但是很容易删除您不希望创建的任何平台。
The Android Gradle plugin uses the ninja build system to configure cmake and target ABIs. The following scripts will generate ABIs for all supported platform types, but it is easy to remove any that you don't wish to create.
VSCode的 gradle插件以便从IDE生成,也可以创建仅调用gradlew的构建类型
VSCode has a gradle plugin for generating from the IDE, or you can create a build type that simply calls the gradlew command line.
首先,我使用 gradle wrapper $ c $初始化了gradle项目。 c>,然后单击
gradlew init
。
First, I inited a gradle project using gradle wrapper
and then gradlew init
.
然后,我添加了用于构建Android库的构建脚本。
Then I added the build scripts for building an Android Library.
build.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* This is a general purpose Gradle build.
* Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
*/
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 28 // Change this to the SDK version you have installed
buildToolsVersion '28.0.3' // Change this to the SDK build tools you have installed
defaultConfig {
minSdkVersion 16 // Cannot be less than 16
targetSdkVersion 28 // Same as CompileSdkVersion
versionCode 1
versionName "1.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
}
}
ndk {
abiFilters = []
abiFilters.addAll(ABI_FILTERS.split(';').collect { it as String })
}
externalNativeBuild {
cmake {
arguments '-DANDROID_PLATFORM=android-16',
'-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static',
'-DANDROID_CPP_FEATURES=rtti exceptions'
}
}
}
externalNativeBuild {
cmake {
path './CMakeLists.txt'
}
}
buildTypes {
release {
minifyEnabled false
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
}
}
}
此版本文件包含一个虚拟 AndroidManifest.xml 文件:
This build file includes a dummy AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="16" />
<application/>
</manifest>
我需要较旧版本的CMake,我使用的是3.4而不是3.11
I needed an older version of CMake, I went with 3.4 instead of 3.11
CMakeLists.txt
cmake_minimum_required( VERSION 3.4.0 )
add_library( Engine SHARED main.cpp )
本地。属性
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
ndk.dir=d\:\\android\\ndk
sdk.dir=d\:\\android\\sdk
gradle.properties
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# EDIT THIS LINE to change the Target ABIs
ABI_FILTERS=x86;x86_64;armeabi-v7a;arm64-v8a
org.gradle.java.home=D:\\Program Files\\Java\\jdk8
distributionUrl=\
https\://services.gradle.org/distributions/gradle-4.1-all.zip
# When set to true the Gradle daemon is used to run the build. For local developer builds this is our favorite property.
# The developer environment is optimized for speed and feedback so we nearly always run Gradle jobs with the daemon.
org.gradle.daemon=true
最后,要构建,我只需运行:
Finally, to build I simply run:
.\gradlew :externalNativeBuildDebug
或
.\gradlew :externalNativeBuildRelease
它会在 build\intermediates\中生成库makecmake
目录。
这篇关于最小的Android CMake构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!