get processid and threadid

来源:互联网 发布:淘宝买东西怎么发国外 编辑:程序博客网 时间:2024/05/16 05:23

 We want to get the linux  process id and the thread id from java.

Actually there will be no directly way, because we don't know the mechanism that dalvik, at least now.

but i'v got a method to get it now.

the way is to use jni.

1. add a lib project dir under external, here is my makefile and c file

Android.mk

 

LOCAL_PATH := $(my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := userLOCAL_SRC_FILES:= \main.cLOCAL_MODULE := libelfylinLOCAL_PRELINK_MODULE := falseLOCAL_SHARED_LIBRARIES := \libnativehelper \libcutils \libutils include $(BUILD_SHARED_LIBRARY)


main.c

#include <nativehelper/JNIHelp.h>#include <nativehelper/jni.h>#include <assert.h>#include <dlfcn.h>#include <stdio.h>#include <string.h>#include <sys/stat.h>#include <utils/Log.h>#ifndef NELEM#define NELEM(x) ((int)(sizeof(x) / sizeof((x)[0])))#endif// Define the line below to turn on poor man's debugging output#undef SUPERDEBUG// Various tests#undef REALLOCTEST#undef OUTOFMEMORYTEST1static int getThreadId() {    //return 200;    return gettid();}static int getProcessId(){    return getpid();}static const char *classPathName = "com/android/browser/Controller";static JNINativeMethod methods[] = {  {"getThreadIdNative", "()I", (void*)getThreadId },  {"getProcessIdNative", "()I", (void*)getProcessId },};/* * Register several native methods for one class. */static int registerNativeMethods(JNIEnv* env, const char* className,    JNINativeMethod* gMethods, int numMethods){    jclass clazz;    clazz = (*env)->FindClass(env, className);    if (clazz == NULL) {        fprintf(stderr,            "Native registration unable to find class '%s'\n", className);        return JNI_FALSE;    }    if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {        fprintf(stderr, "RegisterNatives failed for '%s'\n", className);        return JNI_FALSE;    }    return JNI_TRUE;}/* * Register native methods for all classes we know about. */static int registerNatives(JNIEnv* env){    return jniRegisterNativeMethods(env, classPathName,                                    methods, NELEM(methods));}/* * Set some test stuff up. * * Returns the JNI version on success, -1 on failure. */__attribute__ ((visibility("default"))) jint JNI_OnLoad(JavaVM* vm, void* reserved){    JNIEnv* env = NULL;    jint result = -1;    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {        fprintf(stderr, "ERROR: GetEnv failed\n");        goto bail;    }    assert(env != NULL);    printf("In mgmain JNI_OnLoad\n");    if (registerNatives(env) < 0) {        fprintf(stderr, "ERROR: Exif native registration failed\n");        goto bail;    }    /* success -- return valid version number */    result = JNI_VERSION_1_4;bail:    return result;}


java declaration

must be the same file as the class used in jni, my class is "com/android/browser/Controller"

    static private native int getThreadIdNative();    static private native int getProcessIdNative();


useage:

                System.loadLibrary("elfylin");                Log.d(TAG," tid="+getThreadIdNative()+",pid="+getProcessIdNative());


 

OK,done!

complile the lib and compile the apk, run it.

result:

12-18 22:23:43.273: DEBUG/Controller(31905):  tid=31905,pid=31905

 

 

below is the article how to pack so into apk, i havn't try today.

原文地址:在apk里打包进.so文件的方法作者:navycn
在apk里打包进.so文件的方法
有两种方法,
1 是在Android.mk文件里增加
LOCAL_JNI_SHARED_LIBRARIES := libxxx
这样在编译的时候,NDK自动会把这个libxxx打包进apk;
放在youapk/lib/目录下。

2 是在应用的目录下手工建
libs/armeabi
目录,然后把libxxx.so拷贝到这个目录下,
这样NDK就会自动把这个libxxx.so打包进apk,位置还是在
放在youapk/lib/目录下。

在代码里,使用
System.loadLibrary("xxx");
就可以加载这个动态库了。
这里要注意,参数只写xxx就可以了,不需要写libxxx,也不需要写libxxx.so。

还有一点要说明,System.loadLibrary这个函数会在如下路径搜索libxxx.so文件:
/system/lib
/data/data/you apk package/lib

但,如果libxxx.so还依赖其它.so文件,比如libyyy.so,则System.loadLibrary只会
在/system/lib目录下去找,如果没找到,它不会自动到/data/data/you apk package/lib
下去找,这个时候就会报动态库没找到的错;
解决方法是在load libxxx.so之前,先load libyyy.so,如下:
System.loadLibrary("yyy");
System.loadLibrary("xxx");

 

原创粉丝点击