0
votes

This is my first time doing JNI and not much knowledge with Android Studio either and I am using Windows. I just follow a helloworld jni tutorial, but I run into problem. Maybe it's related to NDK. Also my native function called hello() in MainActivity.java file is in red color so that is error i think.

MainActivity.java:

package com.example.user.ndkdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = new TextView(this);
        textView.setText(hello());
        setContentView(textView);
    }

    static{
        System.loadLibrary("hello");
    }

    public native String hello();
}

com_example_user_ndkdemo_MainActivity.h:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>


#ifndef _Included_com_example_user_ndkdemo_MainActivity
#define _Included_com_example_user_ndkdemo_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_example_user_ndkdemo_MainActivity_BIND_ABOVE_CLIENT
#define com_example_user_ndkdemo_MainActivity_BIND_ABOVE_CLIENT 8L
#undef com_example_user_ndkdemo_MainActivity_BIND_ADJUST_WITH_ACTIVITY
#define com_example_user_ndkdemo_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef com_example_user_ndkdemo_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
#define com_example_user_ndkdemo_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef com_example_user_ndkdemo_MainActivity_BIND_AUTO_CREATE
#define com_example_user_ndkdemo_MainActivity_BIND_AUTO_CREATE 1L
#undef com_example_user_ndkdemo_MainActivity_BIND_DEBUG_UNBIND
#define com_example_user_ndkdemo_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_example_user_ndkdemo_MainActivity_BIND_IMPORTANT
#define com_example_user_ndkdemo_MainActivity_BIND_IMPORTANT 64L
#undef com_example_user_ndkdemo_MainActivity_BIND_NOT_FOREGROUND
#define com_example_user_ndkdemo_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_example_user_ndkdemo_MainActivity_BIND_WAIVE_PRIORITY
#define com_example_user_ndkdemo_MainActivity_BIND_WAIVE_PRIORITY 32L
#undef com_example_user_ndkdemo_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_example_user_ndkdemo_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_example_user_ndkdemo_MainActivity_CONTEXT_INCLUDE_CODE
#define com_example_user_ndkdemo_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_example_user_ndkdemo_MainActivity_CONTEXT_RESTRICTED
#define com_example_user_ndkdemo_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_example_user_ndkdemo_MainActivity_MODE_APPEND
#define com_example_user_ndkdemo_MainActivity_MODE_APPEND 32768L
#undef com_example_user_ndkdemo_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
#define com_example_user_ndkdemo_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
#undef com_example_user_ndkdemo_MainActivity_MODE_MULTI_PROCESS
#define com_example_user_ndkdemo_MainActivity_MODE_MULTI_PROCESS 4L
#undef com_example_user_ndkdemo_MainActivity_MODE_PRIVATE
#define com_example_user_ndkdemo_MainActivity_MODE_PRIVATE 0L
#undef com_example_user_ndkdemo_MainActivity_MODE_WORLD_READABLE
#define com_example_user_ndkdemo_MainActivity_MODE_WORLD_READABLE 1L
#undef com_example_user_ndkdemo_MainActivity_MODE_WORLD_WRITEABLE
#define com_example_user_ndkdemo_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DIALER
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DISABLE
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_example_user_ndkdemo_MainActivity_RESULT_CANCELED
#define com_example_user_ndkdemo_MainActivity_RESULT_CANCELED 0L
#undef com_example_user_ndkdemo_MainActivity_RESULT_FIRST_USER
#define com_example_user_ndkdemo_MainActivity_RESULT_FIRST_USER 1L
#undef com_example_user_ndkdemo_MainActivity_RESULT_OK
#define com_example_user_ndkdemo_MainActivity_RESULT_OK -1L
#undef com_example_user_ndkdemo_MainActivity_HONEYCOMB
#define com_example_user_ndkdemo_MainActivity_HONEYCOMB 11L
#undef com_example_user_ndkdemo_MainActivity_MSG_REALLY_STOPPED
#define com_example_user_ndkdemo_MainActivity_MSG_REALLY_STOPPED 1L
#undef com_example_user_ndkdemo_MainActivity_MSG_RESUME_PENDING
#define com_example_user_ndkdemo_MainActivity_MSG_RESUME_PENDING 2L
/*
 * Class:     com_example_user_ndkdemo_MainActivity
 * Method:    hello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_user_ndkdemo_MainActivity_hello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

main.cpp:

#include <com_example_user_ndkdemo_MainActivity.h>    

/*
 * Class:     com_example_user_ndkdemo_MainActivity
 * Method:    hello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_user_ndkdemo_MainActivity_hello
  (JNIEnv *env, jobject obj){

  return (*env)->NewStringUTF(env, "hello from JNI");
}

Android.mk:

LOCAL_PATH := $(call my-dir)

include #(CLEAR_VARS)

LOCAL_MODULE := hello
LOCAL_LDLIBS += -llog
LOCAL_SRC_FILES := main.cpp

include $(BUILD_SHARED_LIBRARY)

Application.mk:

APP_ABI:=all

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.example.user.ndkdemo"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        ndk{
            moduleName ="hello"
        }
    }

    sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
        jniLibs.srcDir 'src/main/libs'
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
}

local.properties:

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Thu Nov 19 12:40:02 PST 2015
sdk.dir=C\:\\Users\\user\\AppData\\Local\\Android\\sdk
ndk.dir=C\:\\Users\\user\\AppData\\Local\\Android\\android-ndk-r10e

grandle.properties:

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

android.useDeprecatedNdk=true

So when I run the ndk build by typing: "C:\Users\user\AppData\Local\Android\android-ndk-r10e\ndk-build.cmd" I dont see any text afterward in the Android Studio Terminal window message. So I thought everything working, but when I run the project on my android phone, the message pops up on my phone saying "unfortunately, NDKDemo has stopped." I check the Android Studio Android windows message: it says "couldn't find "libshello.so" but I don't have any .so file as i create this project from scratch.

Please let me know what I do wrong. thank you.

1

1 Answers

0
votes

3 things I can mention you check with those if it is useful for you.

1. Following syntax I am having in my JNI .cpp file , and its working for me.

env-> NewStringUTF ( "Hellofrom JNI!");

2. I don't think

ndk{
  moduleName ="hello"
        }

is needed in build.gradle file, as you already declared in Android.mk

  1. Your module name and JNI method name is same "hello" you should avoid such kind of discrepancies.