Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 756
Filip V Avatar asked Nov 08 '22 23:11

Filip V


1 Answers

To make the sample work you may try the following steps:

  1. The signature of the jni method Java_filsoft_photo_HomeScreen_testMethod(...) does not match to your package name which is lala.lala. For the method to be called make its signature consistent with package name.
  2. Try editing hello-jni.c as follows:

    #include <jni.h>
    #include "hello-jni.h"
    
    jint Java_lala_lala_HomeScreen_testMethod( JNIEnv* env, jclass type )
    {
    }
    
  3. Regarding the red highlightings, as of Android Studio 2.0 Preview 3b the NDK feature is not yet officially supported and, to my best guess, this is the reason for it. Disable Android NDK support plugin (if installed)

    Settings > Plugins > uncheck Android NDK support > restart Android Studio

like image 169
Onik Avatar answered Nov 14 '22 23:11

Onik