24

I'm following Create Hello-JNI With Android Studio.

MAC OX 10.11.5

Android Studio 2.2 stable

java version: 1.7.0_79

gradle-2.14.1

Here's my app.gradle:

apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.chenql.helloandroidjni" minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } ndk { moduleName "hello-android-jni" } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:23.3.0' compile 'com.android.support:design:23.3.0' testCompile 'junit:junit:4.12' } 

Here's the error: The Error Message

Error:(20, 0) Could not find method ndk() for arguments [build_13jh6qtzl4f08f8c1of3mvsys$_run_closure1$_closure5@5b127949] on project ':app' of type org.gradle.api.Project. 

Open File

1
  • I have also added the JNI function getMsgFromJni() and System.loadLibrary() to the end of class MainActivity. Commented Nov 9, 2016 at 7:40

4 Answers 4

74

It turns out that this code

ndk { moduleName "hello-android-jni" } 

should be placed under "defaultConfig" block:

defaultConfig { applicationId "com.chenql.helloandroidjni" minSdkVersion 22 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" ndk { moduleName "hello-android-jni" } } 

instaed of after "buildTypes" block.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Can't believe Google hasn't fixed their documentation on this: codelabs.developers.google.com/codelabs/android-studio-jni/…
Holy crap. Just that. Wew. Thanks you Rock Asian guy.
3

Comment or delete ndkVersion 'version number' at build.gradle

Comments

2

Upgrading to a newer version of the Gradle plugin solves

Comments

2

You just add the following code in android/app/build.gradle

android { ndkVersion rootProject.ext.ndkVersion //<-- add line compileSdkVersion rootProject.ext.compileSdkVersion 

And in android/build.gradle

buildscript { ext { buildToolsVersion = "29.0.3" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 30 ndkVersion = "22.1.7171670 || {{use your ndk_version}}" //<-- add line 

Thats it. Rebuild the app

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.