get processid and threadid

 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 := user

LOCAL_SRC_FILES:= \
	main.c

LOCAL_MODULE := libelfylin
LOCAL_PRELINK_MODULE := false
LOCAL_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 OUTOFMEMORYTEST1




static 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文件的方法
有两种方法,
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");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值