Disable jni logging on Android production version - android

I created a logger.h file and I include and use it on my c++ code:
#ifndef LOG_TAG
#define LOG_TAG "jni"
#include <android/log.h>
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#endif // LOG_TAG
In my java application file I use the following code to detect debug mode:
private boolean inDebugMode() {
boolean inDebugMode = false;
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int flags = packageInfo.applicationInfo.flags;
inDebugMode = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
// GlobalData.DEBUG_MODE = inDebugMode;
return inDebugMode;
}
How can I do it on the native code?
Code examples would be appreciated...

Related

Android NDK: dlsym() returns "undefined symbol: JNI_OnLoad" error

I don't know if I'm wrong but I found this solution to bypass Android restriction on private shared library like OpenCL. It copies required libs from /system/vendor/lib64 to /data/data/package.name/ and then loads them by calling dlopen.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>
#define LOG_TAG "JNI"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
void copy_file(const char* in_path, const char* out_path) {
FILE* fin = fopen(in_path, "rb");
check(fin, "fopen()");
FILE* fout = fopen(out_path, "wb");
check(fout, "fopen()");
unsigned char buffer[255];
size_t ret;
while ((ret = fread(buffer, sizeof(*buffer), ARRAY_SIZE(buffer), fin)) == ARRAY_SIZE(buffer)) {
fwrite(buffer, sizeof(*buffer), ret, fout);
}
if (ferror(fin) != 0) {
LOGE("Error on read");
exit(1);
}
fwrite(buffer, sizeof(*buffer), ret, fout);
fclose(fin);
fclose(fout);
}
void* sdlopen(const char* path){
void *handle;
handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
LOGE("#1 %s", dlerror());
exit(1);
}
return handle;
}
void load(){
void *handle, *handle1, *handle2, *handle3, *handle4, *handle5, *handle6;
cl_int (*_clGetPlatformIDs)(cl_uint, cl_platform_id*, cl_uint*);
char *error;
copy_file("/system/vendor/lib64/libOpenCL.so", "/data/data/com.app.opencl/libOpenCL.so");
copy_file("/system/lib64/libcutils.so", "/data/data/com.app.opencl/libcutils.so");
copy_file("/system/lib64/libbase.so", "/data/data/com.app.opencl/libbase.so");
copy_file("/system/lib64/libc++.so", "/data/data/com.app.opencl/libc++.so");
copy_file("/system/lib64/libvndksupport.so", "/data/data/com.app.opencl/libvndksupport.so");
copy_file("/system/lib64/libdl_android.so", "/data/data/com.app.opencl/libdl_android.so");
copy_file("/system/lib64/ld-android.so", "/data/data/com.app.opencl/ld-android.so");
handle1 = sdlopen("/data/data/com.app.opencl/libc++.so");
handle2 = sdlopen("/data/data/com.app.opencl/ld-android.so");
handle3 = sdlopen("/data/data/com.app.opencl/libdl_android.so");
handle4 = sdlopen("/data/data/com.app.opencl/libvndksupport.so");
handle5 = sdlopen("/data/data/com.app.opencl/libbase.so");
handle6 = sdlopen("/data/data/com.app.opencl/libcutils.so");
handle = sdlopen("/data/data/com.app.opencl/libOpenCL.so");
_clGetPlatformIDs = dlsym(handle, "clGetPlatformIDs");
if ((error = dlerror()) != NULL) {
LOGE("#2 %s", error);
exit(1);
}
dlclose(handle);
dlclose(handle1);
dlclose(handle2);
dlclose(handle3);
dlclose(handle4);
dlclose(handle5);
dlclose(handle6);
}
With the code above I can dlopen libOpenCL.so but
dlsym(handle, "clGetPlatformIDs");
returns undefined symbol: JNI_OnLoad. I think it shouldn't look for JNI_OnLoad function. What is wrong?

How to read a file in c using NDK in android

I am trying to read a file in c using NDK. When i debug the code, i see that i get a NULL pointer in f(file reading pointer). Also, my file which i need to read is in the same folder under cpp including all other header files. I have the read and write permissions for external storage in my app. Any help is appreciated.
C Code:
#ifndef client_h
#define client_h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "utility.h"
#define SENDBUFSIZE 1399
#define RECVBUFSIZE 1399
void pingServer(char *hostname, int portno)
{
//Some other functionality which works correctly
// File Read operations
FILE *f = fopen("somefile.csv", "r"); //Get the null pointer here
if (f == NULL)
perror("Could not open input file");
char *currentRow[3];
char *nextRow[3];
int rows = 3;
for (int i=0; i<rows; i++)
{
currentRow[i] = (char*)malloc(20 * sizeof(char));
nextRow[i] = (char*)malloc(20 * sizeof(char));
}
double current, next;
double elapsed;
int isNextRow, isCurentRow;
readCsvLine(f,currentRow,rows); //Get the headers
int counter = 2;
isCurentRow = readCsvLine(f,currentRow,rows);
//sends the data
//Free the memory
for (int i=0; i<rows; i++)
{
free(currentRow[i]);
free(nextRow[i]);
}
free(buf);
return;
}
char* printName(){
char* ptr = "hello From C";
return ptr;
}
#endif
Java code for calling the c methods:
#include <jni.h>
#include <string>
#include "client.h"
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_habbas_mobiledifferentiation_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
char *hostname = "127.0.0.1";
int portno = 1254;
pingServer(hostname, portno);
std::string hello = printName();
return env->NewStringUTF(hello.c_str());
}

NDK-build GLuint does not name a type when trying to compile

So apparently im not too sure how to include Open GL in my SDL application
I have:
if _ANDROID_
#include <GLES2/gl2.h>
#include <GLES2/glext.h>
but when I use GLUint I get
GLuint does not name a type
My android.mk includes
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
and i am building for android platform 10 (** **)
APP_PLATFORM := android-10
APP_ABI := armeabi-v7a armeabi x86
So i am not too sure what i am doing wrong
(Added the rest of the file that is having problems:)
#pragma once
#include <stdlib.h>
#include <iostream>
#include <random>
#include <cassert>
#include "vec2.h"
#include "vec3.h"
#include "vec4.h"
#include "mat4.h"
#include "transformations.h"
#if defined(_MSC_VER)
#include <windows.h>
#include <glew.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_opengl.h>
#include <stdio.h>
#include <string>
#endif
#if __APPLE__
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
#include <SDL2_ttf/SDL_ttf.h>
#include <SDL2_mixer/SDL_mixer.h>
#endif
#if __ANDROID__
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#endif
// vec2 *screen;
/*const*/ // int sw;
/*const*/ // int sh;
#if __IPHONEOS__ || __ANDROID__
#define MOBILE 1
#endif
#if __MACOSX__ || __WINDOWS__ || __LINUX__
#define PC 1
#endif
#if __IPHONEOS__
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
inline void glBindVertexArray(GLuint id1) {
glBindVertexArrayOES(id1);
}
inline void glGenVertexArrays(GLsizei n, GLuint *ids) {
glGenVertexArraysOES(n, ids);
}
inline void glDeleteVertexArrays(GLsizei n, const GLuint *ids) {
glDeleteVertexArraysOES(n, ids);
}
#elif TARGET_OS_MAC
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
inline void glBindVertexArray(GLuint id1) {
glBindVertexArrayAPPLE(id1);
}
inline void glGenVertexArrays(GLsizei n, GLuint *ids) {
glGenVertexArraysAPPLE(n, ids);
}
inline void glDeleteVertexArrays(GLsizei n, const GLuint *ids) {
glDeleteVertexArraysAPPLE(n, ids);
}
#elif __ANDROID__
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
inline void glBindVertexArray(GLuint id1) {
glBindVertexArrayOES(id1);
}
inline void glGenVertexArrays(GLsizei n, GLuint *ids) {
glGenVertexArraysOES(n, ids);
}
inline void glDeleteVertexArrays(GLsizei n, const GLuint *ids) {
glDeleteVertexArraysOES(n, ids);
}
#endif // ANDROID
inline string get_path(string filename) {
char *base = SDL_GetBasePath();
string path(base + filename);
SDL_free(base);
//cout << "getting path " << path << endl;
return path;
}
using namespace std;
I solved it by using
APP_STL := c++_static
instead of
#APP_STL := gnustl_static

How to Debug JNI C File in NDK android

I want to check the value of the variable in JNI C NDK android, Can anybody help? I need similar to
System.out.println(""+values);
in android.
Define this at the top of the class and try to use like this
#define LOG_TAG "ProjectName"
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOG_ASSERT(_cond, ...) if (!_cond) __android_log_assert("conditional", LOG_TAG, __VA_ARGS__)
Use like this it will helps you :)
LOGV("Hello World Sample...!");

How to turn on/off native log cat in ndk at runtime

I use this snippet code to turn log on or off
#define DEBUG 1
#if DEBUG
#include <android/log.h>
#define LOG_TAG "native_log"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
# define LOGD(...) do {} while (0) // do nothing
#endif
// use it
LOGD("%s : %d","value", val);
It worked fine by turn DEBUG flag on/off. The problem is I want to do it at run time in java side. What I want like this:
// java
private native void nativeSetDebug(boolean flag);
// jni
JNIEXPORT void JNICALL Java_com_my_package_Native_nativeSetDebug(JNIEnv *env, jobject thiz, jboolean flag){
// what should I do in this method?
}
Since the macros in c++ are replaced by the preprocessor by their value before source file even compiles, so I'm looking for another approach. Is there any ideas?
macro file
extern bool useDebug;
#include <android/log.h>
#define LOG_TAG "native_log"
#define LOGD(...) if(useDebug){__android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)}
C file
bool useDebug = true;
JNIEXPORT void JNICALL Java_com_my_package_Native_nativeSetDebug(JNIEnv *env, jobject thiz, jboolean flag){
useDebug = flag;
}
The extern is important, otherwise each file including the header will define its own variable and they won't be set correctly.

Categories

Resources