JNI 사용 구조는 다음 그림으로 요약할 수 있습니다.

Android에서 사용하는데 필요한 것은 5 단계에서 Cross Compiler로 컴파일 되어야 한다는 것입니다.
그리고 컴파일 된 결과가 ".DLL"이 아니라 ".so" 파일로 나온다는 거겠죠.
Cross Compiler는 http://www.codesourcery.com/gnu_toolchains/arm/download.html 에서 구할 수 있습니다.
그런데 gcc의 -shared 옵션으로 .so 파일을 만들고 실행해보면, 사용될 때 App가 죽어버립니다.
이는 precompiled-library에 관련된 문제로 보이는데, 이를 위해서 컴파일 단계의 ldscript를 수정해서 사용합니다.
http://honeypod.blogspot.com/2007/12/shared-library-hello-world-for-android.html 를 참고하면 다음과 같은 부분을 볼 수 있습니다.
$toolchains_home/arm-none-linux-gnueabi/lib/ldscripts/ 경로에 있는 armelf_linux_eabi.xsc 파일을 수정해야 한다는 이야기입니다.
수정한 파일을 사용해서 다음 명령으로 컴파일하면 실제 안드로이드에서 동작하는 라이브러리를 만들 수 있습니다.
그리고 emulator 실행 후 다음 명령으로 에뮬레이터에서 실행가능합니다.
안드로이드 Application 작성 및 빌드, javah 사용, 에뮬레이터 log 보는 법, ldscript 수정 등으로 많이 늦었습니다. 죄송해요 ㅠ
Android에서 사용하는데 필요한 것은 5 단계에서 Cross Compiler로 컴파일 되어야 한다는 것입니다.
그리고 컴파일 된 결과가 ".DLL"이 아니라 ".so" 파일로 나온다는 거겠죠.
Cross Compiler는 http://www.codesourcery.com/gnu_toolchains/arm/download.html 에서 구할 수 있습니다.
그런데 gcc의 -shared 옵션으로 .so 파일을 만들고 실행해보면, 사용될 때 App가 죽어버립니다.
이는 precompiled-library에 관련된 문제로 보이는데, 이를 위해서 컴파일 단계의 ldscript를 수정해서 사용합니다.
http://honeypod.blogspot.com/2007/12/shared-library-hello-world-for-android.html 를 참고하면 다음과 같은 부분을 볼 수 있습니다.
Now, the default linker script need to be modified. The default linker script is available at $toolchains_home/arm-none-linux-gnueabi/lib/ldscripts/armelf_linux_eabi.xsc. Copy it to the current directory. Comment out three lines and add one line replacing the first commented out line.
/* . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); */
. = .; /* this line replaces the line above */
/* . = DATA_SEGMENT_RELRO_END (0, .); */
/* . = DATA_SEGMENT_END (.); */
$toolchains_home/arm-none-linux-gnueabi/lib/ldscripts/ 경로에 있는 armelf_linux_eabi.xsc 파일을 수정해야 한다는 이야기입니다.
수정한 파일을 사용해서 다음 명령으로 컴파일하면 실제 안드로이드에서 동작하는 라이브러리를 만들 수 있습니다.
arm-none-linux-gnueabi-gcc -I/usr/lib/jvm/java-6-sun/include -I/usr/lib/jvm/java-6-sun/include/linux -fpic -c <JNI 함수 구현소스>.c
arm-none-linux-gnueabi-ld -T armelf_linux_eabi.xsc -shared -o <library명>.so <Object파일>.o
arm-none-linux-gnueabi-ld -T armelf_linux_eabi.xsc -shared -o <library명>.so <Object파일>.o
그리고 emulator 실행 후 다음 명령으로 에뮬레이터에서 실행가능합니다.
adb push <라이브러리>.so /system/lib
adb install <JNI를 사용하는 Android APP>.apk
adb install <JNI를 사용하는 Android APP>.apk

Prev

Rss Feed