如何在 Android Studio 1.3 中配置 NDK 项目
Posted
技术标签:
【中文标题】如何在 Android Studio 1.3 中配置 NDK 项目【英文标题】:How to configure NDK project in Android Studio 1.3 【发布时间】:2015-10-31 14:27:39 【问题描述】:我一直在尝试按照this 文章和this 文章为 NDk 配置 android Studio。以下是我的 gradle-wrapper.properties 的内容
#Sat Aug 08 09:36:53 IST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
以下是build.gradle(project)的内容
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript
repositories
jcenter()
dependencies
classpath 'com.android.tools.build:gradle-experimental:0.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
allprojects
repositories
jcenter()
最后是 build.gradle(module)
apply plugin: 'com.android.model.application'
model
android
compileSdkVersion = 22
buildToolsVersion = "23.0.0 rc3"
defaultConfig.with
applicationId = "com.opaxlabs.nativetest"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = 22
versionCode = 1
versionName = "1.0"
android.buildTypes
release
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
android.ndk
moduleName = "native"
dependencies
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
当我尝试同步 gradle 文件时,出现以下错误:
Error:No such property: android for class: com.android.build.gradle.managed.AndroidConfig
local.properties 中定义了以下路径
ndk.dir=/opt/adt-bundle-linux-x86_64-20140702/sdk/ndk-bundle
sdk.dir=/opt/adt-bundle-linux-x86_64-20140702/sdk
所以看起来我错过了一些东西,只是我找不到它。 任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:NDK Build option
ndk
moduleName "SeePlusPlus" // Name of C++ module (i.e. libSeePlusPlus)
cFlags "-std=c++11 -fexceptions" // Add provisions to allow C++11 functionality
stl "gnustl_shared" // Which STL library to use: gnustl or stlport
【讨论】:
很抱歉投反对票,但您的 gradle 脚本不遵守 Gradle 2.5 语法。请在此处查看原因:tools.android.com/tech-docs/new-build-system/…【参考方案2】:在您的 build.gradle(module) 中,android.buildTypes 块需要在 android 块之外。所以它应该是这样的:
model
android
compileSdkVersion = 22
buildToolsVersion = "23.0.0 rc3"
defaultConfig.with
applicationId = "com.opaxlabs.nativetest"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = 22
versionCode = 1
versionName = "1.0"
android.buildTypes
release
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
android.ndk
moduleName = "native"
【讨论】:
对于从 0.2.0 迁移到 0.4.0 的用户注意:+= 不再适用于 0.4.0,请参阅 tools.android.com/tech-docs/new-build-system/…【参考方案3】:您是否在 Local.property 文件中声明了您的 NDK 路径??
看起来环境路径和 local.properties 文件指向不同的位置:
PATH: C:\Program Files (x86)\Android\android-ndk-r9d
local.properties:C:\Program Files (x86)\Android\android-studio\android-ndk-r9d
确定哪个是正确的。您可以保留 PATH 并删除 local.properties 声明,然后通过控制台尝试此命令:ndk-build -?看看它是否在 PATH 中找到
【讨论】:
从 Android Studio 1.3 起无需声明NDK Path
。现在设置在Project Structure > SDK Location
切勿对 NDK 使用带空格的路径。如果它的路径也有空格,Android SDK 可能会不高兴。以上是关于如何在 Android Studio 1.3 中配置 NDK 项目的主要内容,如果未能解决你的问题,请参考以下文章