jni function missing

  • Replies:2
Gayathri Sekaran
  • Forum posts: 9

Sep 11, 2018, 11:47:04 AM via Website

Hi,

While compiling the android-studio project the .so file is not creating. Can anyone please tell me what will be the problem.

Reply
Progesterony
  • Forum posts: 27

Sep 11, 2018, 2:34:14 PM via Website

1.Create directories

Inside /app/src/main create a jni and jniLibs directory.

2.Update local.properties
Inside your local.properties file, add a line similar to:

ndk.dir=/home/user/development/bin/android-ndk-r10e

  1. Update build.gradle

something like this :
apply plugin: 'com.android.model.application'

model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.0"

defaultConfig.with {
    applicationId = "com.example.hellojni"
    minSdkVersion.apiLevel = 4
    targetSdkVersion.apiLevel = 23
}

}

compileOptions.with {
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
}

/*
* native build settings
/
android.ndk {
moduleName = "hello-jni"
/

* Other ndk flags configurable here are
* cppFlags += "-fno-rtti"
* cppFlags += "-fno-exceptions"
* ldLibs = ["android", "log"]
* stl = "system"
*/
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
}
}
android.productFlavors {
// for detailed abiFilter descriptions, refer to "Supported ABIs" @
// https://developer.android.com/ndk/guides/abis.html#sa
create("arm") {
ndk.abiFilters += "armeabi"
}
create("arm7") {
ndk.abiFilters += "armeabi-v7a"
}
create("arm8") {
ndk.abiFilters += "arm64-v8a"
}
create("x86") {
ndk.abiFilters += "x86"
}
create("x86-64") {
ndk.abiFilters += "x86_64"
}
create("mips") {
ndk.abiFilters += "mips"
}
create("mips-64") {
ndk.abiFilters += "mips64"
}
// To include all cpu architectures, leaves abiFilters empty
create("all")
}
}
4. You will need an Android.mk file inside the /app/src/main/jni directory
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

  1. test.cpp

Add some awesome C/C++ source code for your lib. These files will start in /app/src/main/jni and will be compiled and linked as specified in your Android.mk

example :

#include
#include

static const char *SOME_TAG = "MyAwesomeTag";

extern "C"
{

void
Java_com_something_something_1android_ClassName_some_fn(JNIEnv *env, jobject obj)
{
__android_log_print(ANDROID_LOG_VERBOSE, SOME_TAG, "Hello from NDK :)");
}

}

Compile and run.

this solution is compiled by several stackoverflow threads
Good luck

Helpful?
Reply
Gayathri Sekaran
  • Forum posts: 9

Sep 12, 2018, 7:21:13 AM via Website

Posting my logcat for your reference.

09-12 10:29:29.830 7401-7401/? D/ToggleAccessibilityServicePreferenceFragment: mComponentName.getPackageName : com.example.vinay.screenreader
09-12 10:29:34.806 2908-2992/? D/MARsPolicyManager: getEnabledAccessibilityPackage: add mEnabledAccessibilityPackages com.example.vinay.screenreader
09-12 10:29:34.814 2908-2908/? I/AccessibilityManagerService: isTalkbackEnable : false, accesibilityService : com.example.vinay.screenreader/com.example.vinay.screenreader.MyScreenReaderService
09-12 10:29:34.851 7401-7401/? D/ToggleAccessibilityServicePreferenceFragment: mComponentName : ComponentInfo{com.example.vinay.screenreader/com.example.vinay.screenreader.MyScreenReaderService} false

ndk path in local.properties
ndk.dir=/home/gayathri/Android/Sdk/ndk-bundle
sdk.dir=/home/gayathri/Android/Sdk

Application.mk

APP_ABI := all

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := mainfn
LOCAL_SRC_FILES := mainfn.c my_flite_hts_imp.c us_int_accent_cart.c us_int_tone_cart.c us_nums_cart.c us_phrasing_cart.c us_pos_cart.c transliterator.c
LOCAL_LDLIBS += -lm -llog

include $(BUILD_SHARED_LIBRARY)

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.example.vinay.screenreader"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    ndk
            {
                moduleName "mainfn"
            }
}


sourceSets.main
        {
            jni.srcDirs = [];
            jniLibs.srcDir 'src/main/libs'
        }
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

mainfn.c

include

JNIEXPORT void JNICALL Java_com_example_vinay_screenreader_MyScreenReaderService_mainfn
(JNIEnv *env, jobject obj, jstring Usertext_cont,jstring path,jstring wpath)
{
//jclass userClass = (*env)->GetObjectClass(env, userobject);
//jmethodID getUsertext_cont = (env)->GetMethodID(env, userClass, "getUsertext", "()I");
//char
text = (char*)(*env)->CallIntMethod(env, userobject, getUsertext_cont);
const char *aa = (*env)->GetStringUTFChars(env, Usertext_cont, 0);
const char *bb = (*env)->GetStringUTFChars(env, path, 0);
const char *cc = (*env)->GetStringUTFChars(env, wpath, 0);
char * text = strdup(aa);
char * filepath = strdup(bb);
char * wavpath = strdup(cc);
int y = main_syn(text,filepath,wavpath);
int x = num_return();
if(y==-1)
{
(*env)->NewStringUTF(env,"got -1");
}
else
{
return (*env)->NewStringUTF(env, "Hello From JNI");
(*env)->NewStringUTF(env,aa);
}

}

— modified on Sep 12, 2018, 8:37:09 AM

Helpful?
Reply