⑴ 如何在android源碼里查找java中native方法對應的C++實現
知道方法名就知道C++里的函數名了,native方法的函數名是
Java_包名_類名_函數名
開頭的,包名要把.換成_。比如com.xxx.yyy.ClassA.methodB在C++那邊找Java_com_xxx_yyy_ClassA_methodB就行了
⑵ 如何在Android源碼里查找Java中native方法對應的C++實現
android源碼只包含android app java的客戶端代碼。
native部分可能有可能沒有,先全局搜索.c或者.cpp文件,有這些文件才可能有。
如果是標準的android native support 工程,那麼eclipse裡面打開之後,在工程目錄/jni下面就是 native的C++代碼。
Android源碼中很多關鍵代碼都是C++實現的,java通過jni來調用,經常會看到java中這樣的代碼:
static native Thread currentThread();
如何根據方法名找到其對應的C++實現,有兩個方法。
先來個java代碼的示例VMThread.java:
package java.lang;
class VMThread {
Thread thread;
int vmData;
VMThread(Thread t) {
thread = t;
}
native static void create(Thread t, long stackSize);
static native Thread currentThread();
static native boolean interrupted();
static native void sleep (long msec, int nsec) throws InterruptedException;
static native void yield();
native void interrupt();
native boolean isInterrupted();
......
}
我們要查找currentThread方法的實現。
方法一:
由於Android源碼中對每個native實現都會寫一個java方法名和C++方法名映射的列表,所以我們直接搜索這個列表內容即可。
zkw@zkw $ grep -rns '"currentThread"' ./*
./art/compiler/dex/quick/dex_file_method_inliner.cc:108: "currentThread", // kNameCacheCurrentThread
匹配到二進制文件 ./dalvik/vm/native/.java_lang_VMThread.cpp.swp
./dalvik/vm/native/java_lang_VMThread.cpp:241: { "currentThread", "()Ljava/lang/Thread;",
./external/android-mock/tests/com/google/android/testing/mocking/AndroidMockGeneratorTest.java:249: Method method = Thread.class.getMethod("currentThread");
./external/android-mock/tests/com/google/android/testing/mocking/AndroidMockGeneratorTest.java:407: Method method = Thread.class.getMethod("currentThread");
可以看到,在文件./dalvik/vm/native/java_lang_VMThread.cpp中找到currentThread方法相關的信息,後面()Ljava/lang/Thread代表這個方法的返回值。
進入java_lang_VMThread.cpp這個文件可以看到:
17 /*
18 * java.lang.VMThread
19 */
20 #include "Dalvik.h"
21 #include "native/InternalNativePriv.h"
22
23
24 /*
25 * static void create(Thread t, long stacksize)
26 *
27 * This is eventually called as a result of Thread.start().
28 *
29 * Throws an exception on failure.
30 */
31 static void Dalvik_java_lang_VMThread_create(const u4* args, JValue* pResult)
32 {
33 Object* threadObj = (Object*) args[0];
34 s8 stackSize = GET_ARG_LONG(args, 1);
35
36 /* ing collector will pin threadObj for us since it was an argument */
37 dvmCreateInterpThread(threadObj, (int) stackSize);
38 RETURN_VOID();
39 }
40
41 /*
42 * static Thread currentThread()
43 */
44 static void Dalvik_java_lang_VMThread_currentThread(const u4* args,
45 JValue* pResult)
46 {
47 UNUSED_PARAMETER(args);
48
49 RETURN_PTR(dvmThreadSelf()->threadObj);
50 }
51
......
237
238 const DalvikNativeMethod dvm_java_lang_VMThread[] = {
239 { "create", "(Ljava/lang/Thread;J)V",
240 Dalvik_java_lang_VMThread_create },
241 { "currentThread", "()Ljava/lang/Thread;",
242 Dalvik_java_lang_VMThread_currentThread },
243 { "getStatus", "()I",
244 Dalvik_java_lang_VMThread_getStatus },
245 { "holdsLock", "(Ljava/lang/Object;)Z",
246 Dalvik_java_lang_VMThread_holdsLock },
247 { "interrupt", "()V",
248 Dalvik_java_lang_VMThread_interrupt },
249 { "interrupted", "()Z",
250 Dalvik_java_lang_VMThread_interrupted },
251 { "isInterrupted", "()Z",
252 Dalvik_java_lang_VMThread_isInterrupted },
253 { "nameChanged", "(Ljava/lang/String;)V",
254 Dalvik_java_lang_VMThread_nameChanged },
255 { "setPriority", "(I)V",
256 Dalvik_java_lang_VMThread_setPriority },
257 { "sleep", "(JI)V",
258 Dalvik_java_lang_VMThread_sleep },
259 { "yield", "()V",
260 Dalvik_java_lang_VMThread_yield },
261 { NULL, NULL, NULL },
262 };
源碼中第242行找到對應的名字,用紅色標出,其實現就在第44行。
這個方法不是很准確,要靠經驗來判斷搜出來的代碼是否是自己要找的,下一個方法可以較准確的查找。
方法二:
還是找VMThread.java的currentThread函數,找多了會發現,C++的名字一般都是包名+類名+方法名,比如currentThread的C++名字就肯定包含「java_lang_VMThread_currentThread」,所以直接搜索即可。
如何在Android源碼里查找Java中native方法對應的C++參考:http://e.51cto.com/course/course_id-4377.html
⑶ 如何編譯Android動態鏈編的native c/c++code
編譯環境要求:下載Android的源碼,並執行完一次完整的編譯。以下的所有命令均是在編譯後的源碼根目錄下執行。
1. 編譯C code
同樣以hello.c為例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("hello, world!\n");
return 0;
}
執行以下步驟生成動態鏈編的binary文件:
生成目標文件:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -include system/core/include/arch/linux-arm/AndroidConfig.h -c -o hello.o hello.c
生成可執行程序:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm- eabi-gcc -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,noreloc -o hello -Lout/target/proct/generic/obj/lib -Wl,-rpath-link=out/target/proct/generic/obj/lib -lc -lstdc++ out/target/proct/generic/obj/lib/crtbegin_dynamic.o hello.o -Wl,--no-undefined ./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/proct/generic/obj/lib/crtend_android.o
用命令file查看生成的hello文件屬性:
hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
可以證明此時的hello是一個動態鏈編的文件。
2. 編譯native c++ 代碼
以hello_cpp為例:
hello_cpp.h
#ifndef HELLO_CPP_H
#define HELLO_CPP_H
class Hello
{
public:
Hello();
~Hello();
void printMessage(char* msg);
};
#endif
hello_cpp.cpp
#include <stdio.h>
#include "hello_cpp.h"
Hello::Hello()
{
}
Hello::~Hello()
{
}
void Hello::printMessage(char* msg)
{
printf("C++ example printing message: %s", msg);
}
int main(void)
{
Hello hello_obj;
hello_obj.printMessage("Hello world!\n");
return 0;
}
執行以下命令完成:
編譯目標文件:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++ -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -include system/core/include/arch/linux-arm/AndroidConfig.h -fno-exceptions -fno-rtti -c -o hello_cpp.o hello_cpp.cpp
編譯可執行程序:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm- eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,noreloc -o hello_cpp -Lout/target/proct/generic/obj/lib -Wl,-rpath-link=out/target/proct/generic/obj/lib -lc -lstdc++ out/target/proct/generic/obj/lib/crtbegin_dynamic.o hello_cpp.o -Wl,--no-undefined ./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/proct/generic/obj/lib/crtend_android.o
同樣用file查看hello_cpp的文件屬性:
hello_cpp: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
但是很不幸的是,android自帶的toolchain不支持C++標准庫的開發,即所有的std namespace下的類均無法使用,包括基本的string。