user7074940
user7074940

Reputation:

Why not Reading c++ file on android studio on my app..?

I have my c++ file I putted it in jni folder with application.mk ,android.mk and putted everything in build.gradle and in the class with native stringtoJNI and static {} i guess i putted everything and still I have issues when I tried helloJNI sample it worked but on my app there is problem i searched alot and saw similar issues and tried almost everything and still i get error.

ndktest.c

jstring Java_com_example_mike_lol8_WallService(JNIEnv *env, jobject obj) {
jstring str = "http://555.555.555.555";
return (*env)->NewStringUTF(env, str); }

Application.mk

APP_ABI := all

Android.mk

LOCAL_PATH := $(call my-dir)

 include $(CLEAR_VARS)

   LOCAL_MODULE := ndktest
  LOCAL_SRC_FILES := ndktest.c

 include $(BUILD_SHARED_LIBRARY)

build.gradle

 ndk {
        moduleName "ndktest"
    }

    sourceSets.main {
        jni.srcDirs "src/main/jni"
         jniLibs.srcDir "src/main/jniLibs"
    }
}

WallService.class

public class WallService extends Service {
  public static native String stringFromJNI();

private static String BASEURL = stringFromJNI();
static {
    System.loadLibrary("ndktest");
}

So in so examples StringJNI reading the file but i got error in the logcat but this is my code StringJNI it's red can't connect with my file(can't see it) so maybe someone can help me

Upvotes: 1

Views: 1415

Answers (2)

user7074940
user7074940

Reputation:

Found the solution (maybe people benefit from it ) thanks all for the help

public class WallService extends Service {

public native String stringFromJNI();
static {
    System.loadLibrary("native-lib");
}

scr/main/cpp or(jni)/native-lib.c

#include <jni.h>
#include <string>

extern "C"
 jstring
Java_com_example_mike_lol10_WallService_stringFromJNI(
    JNIEnv *env,
    jobject /* this */) {
std::string hello = "http://130.193.127.250";
return env->NewStringUTF(hello.c_str());
   }

build.gradle

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
    applicationId "com.example.mike.lol10"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {
            cppFlags "-frtti -fexceptions"
        }
    }
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}
}

or u can check the support c++ when your creating new project under the name . It create Cmakelist too.

Upvotes: 1

NovusMobile
NovusMobile

Reputation: 1823

A) You need to add dependency into the build.Gradle file.gradle-experimental

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0'
    classpath 'com.android.tools.build:gradle-experimental:0.8.2'
}

There are two build files. 1) app.gradle 2) project.gradle. (Change into this file)

B) ndk build script

apply plugin: 'com.android.model.application'

model{
    android {
        compileSdkVersion 17
        buildToolsVersion "23.0.3"

        defaultConfig {
            applicationId "com.example.drawerlayouttest"
            minSdkVersion.apiLevel 17
            targetSdkVersion.apiLevel 17

        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file('proguard-android.txt'))
            }
        }

        ndk{
            moduleName "myjni"
            cppFlags.add("-std=c++11")
            ldLibs.addAll(["GLESv2","log"])
            stl "gnustl_shared"
        }

        /*sourceSets.main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['libs']
        }*/
    }
}

Upvotes: 0

Related Questions