androidcandroid-studioandroid-ndkjava-native-interface

Hello-jni sample doesn't work in Android Studio 2.0 Preview


I'm trying to implement hello-jni sample into my project. I have Gradle 2.8 and 'com.android.tools.build:gradle-experimental:0.4.0' and using Android Studio 2.0 Preview 3b.

This is my build.gradle:

apply plugin: 'com.android.model.application'
model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"
        defaultConfig.with {
            applicationId = "lala.lala"
            minSdkVersion.apiLevel = 16
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }
    /*
    * native build settings
    */
    android.ndk {
        moduleName = "hello-jni"


        //  cppFlags.add("-fno-rtti")
     //    cppFlags.add("-fno-exceptions")
     //   ldLibs.addAll(["android", "log"])
      //  stl       = "system"

    }

    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
        }
        create("x86") {
            ndk.abiFilters.add("x86")
        }
        create("x86-64") {
            ndk.abiFilters.add("x86_64")
        }
        create("mips") {
            ndk.abiFilters.add("mips")
        }
        create("mips-64") {
            ndk.abiFilters.add("mips64")
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            //proguardFiles.add(file('proguard-rules.txt'))
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.google.android.gms:play-services-ads:8.3.0'
    compile 'com.google.android.gms:play-services-analytics:8.3.0'
    compile 'com.google.android.gms:play-services-appindexing:8.3.0'
}

I have created in JNI folder hello-jni.h:

enter image description here

In some part of my code I import this:

static {
    System.loadLibrary("hello-jni");
}
public static native int testMethod();

And by auto completion created hello-jni.c:

#include "hello-jni.h"

JNIEXPORT jintJNICALL
Java_lala_lala_HomeScreen_testMethod(JNIEnv
*env,
jclass type
)
{
// TODO
}

Header file hello-jni.h:

//
// Created by Filip on 15.12.2015..
//

#ifndef PHOTO_HELLO_JNI_H
#define PHOTO_HELLO_JNI_H

#endif //PHOTO_HELLO_JNI_H

But it doesn't work. It is full red:

enter image description here

What is problem? How to solve it?


Solution

  • It's problem in Android studio, but just on Windows: Issue 195483
    You can downgrade to Android studio 1.5.1, gradle 2.8, experimental wrapper 0.4.0.
    I tried Android Studio 2.0 beta 6, with wrapper 0.6.0-beta5 and 0.6.0-beta6, but still doesn't work.