使用的AndroidStudio版本是1.3.2,可以支持自动构建NDK,需要做一些配置:
1.工程的local.properties里添加ndk路径:
## This file is automatically generated by Android Studio. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # 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. #Tue Sep 15 17:59:20 CST 2015 ndk.dir=/Users/songjia/software/android-ndk-r10e sdk.dir=/Users/songjia/software/android-sdk
2.在需要使用本地代码的模块中添加jni目录和需要的源文件,头文件;
不需要再手动编写Android.mk编译文件,会自动生成的。
3.在模块的build.gradle中添加ndk配置项:
apply plugin: 'com.android.library' android { compileSdkVersion 22 buildToolsVersion "23.0.0" defaultConfig { minSdkVersion 14 targetSdkVersion 22 versionCode 1 versionName "1.0" ndk{ //模块名 moduleName 'ndkdemo' //目标平台 abiFilters 'armeabi','armeabi-v7a' //链接库文件 ldLibs 'log' } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }
4.新版AndroidStudio要在项目的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. # Default value: -Xmx10248m -XX:MaxPermSize=256m # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # 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 # 使用废弃的NDK android.useDeprecatedNdk=true
没有报错的话就可以编译成功了