假设你的配置信息和我一样:
android studio 版本 2.1.2
sdk\build-tools 版本 23.0.3
ndk 版本 android-ndk-r10
-------------------------------------------------------------------
以下是一个简单的android studio版本jni入门,已跳过各种坑,
(注意我没有使用v7包提供的属性和类,因为会出现各种问题)
仅供参考
===
1.新建module
2.编写native方法,与c/c++世界交流
3.make以下module或者project,生成classes文件夹,箭头指向处
4.生成头文件,假设你没有出现问题
5.可以看到.h文件,为以后复制打基础
6.编写简单jni函数,把步骤5的.h文件复制到jni目录下
注意,其中有个FixBug.cpp 可以是任意符合规则的.cpp文件
7.gradle配置
8.调用如下
9.效果如下
==========仅供参考===========
=====================Project ----->build.gradle===========================
// 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:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(
type
: Delete) {
delete
rootProject
.
buildDir
}
=====================Module--->build.gradle===========================
apply
plugin
:
'com.android.application'
android {
compileSdkVersion
23
buildToolsVersion
"23.0.3"
defaultConfig {
applicationId
"july.appn"
minSdkVersion
11
targetSdkVersion
23
versionCode
1
versionName
"1.0"
ndk{
moduleName
"hello"
// 生成hello.so文件
abiFilters
"armeabi"
,
"armeabi-v7a"
,
"x86"
//输出指定三种abi体系结构下的so库。
}
}
buildTypes {
release {
minifyEnabled
false
proguardFiles
getDefaultProguardFile
(
'proguard-android.txt'
),
'proguard-rules.pro'
}
}
sourceSets { main {
jni
.
srcDirs
= [
'src/main/jni'
,
'src/main/jni/'
] } }
}
dependencies {
compile fileTree(
dir
:
'libs'
,
include
: [
'*.jar'
])
testCompile
'junit:junit:4.12'
compile
'com.android.support:appcompat-v7:23.3.0'
compile'com.google.android.gms:play-services-appindexing:8.1.0'
}
碰见如下异常时:
10:16:09 Gradle sync failed: Error: NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration.
Consult IDE log for more details (Help | Show Log)
=============================gradle.properties配置如下=====================================
android.useDeprecatedNdk
=
true
Gradle build-info.xml not found for module jni_hello. Please make sure that you are using gradle plugin '2.0.0-alpha4' or higher.
取消掉即可.
Demo地址: https://github.com/majunm/As2Jni