開發階段可能需要一些簡單的測試程式(執行檔) , 來進行一些小功能測試 , 所以下一個階段 , 移植一個小執行檔進入 TI BSP 吧 !!
之前我們有一個 lcd-test 的程式 , 由 console press "Enter" key , 會使用 mmap 方式填寫固定 pattern 到 fb0 內. 就先移植這個吧 !!
同樣的使用自己的 makefile 方式 .
首先開一的資料夾 ( android 的 main.mk 會自己尋找 ) , 可以有兩層 , 我的方式如下:
rowboat-android/test_apps/test/lcd-test & rowboat-android/test_apps/test/lcd-test/lcd-test-0.0.1
在 lcd-test 資料夾下放入 Android.mk file , 內容如下:
# Android.mk
#
# Copyright 2012 Jeff Hsieh <jeff@instant-jeff.instant>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
CROSS_TC_PREFIX := $(shell basename $(TARGET_TOOLS_PREFIX))
CROSS_TC_DIR := $(realpath $(shell dirname $(TARGET_TOOLS_PREFIX)))
export TARGET_CC := $(CROSS_TC_DIR)/$(CROSS_TC_PREFIX)gcc
export PRODUCT_OUT := $(PRODUCT_OUT)
PACKAGE_PATH := $(LOCAL_PATH)
LOCAL_GLOBAL_CFLAGS := $(subst -I ,-I $(shell pwd)/,$(subst -include ,-include $(shell pwd)/,$(TARGET_GLOBAL_CFLAGS))) \
$(foreach f,$(TARGET_C_INCLUDES), $(addprefix -I$(shell pwd)/,$f)) \
$(foreach f,$(TARGET_PROJECT_INCLUDES), $(addprefix -I$(shell pwd)/,$f)) \
$(foreach f,$(JNI_H_INCLUDE), $(addprefix -I$(shell pwd)/,$f))
LOCAL_GLOBAL_LDFLAGS := -L$(shell pwd)/$(TARGET_OUT_STATIC_LIBRARIES)
##=======================================
## Setting source file.
##=======================================
#~ ifeq ($(CONFIG_),y)
export VERNUM = 0.0.1
export TARGET_EXE = lcd-test
#~ endif
VERSION = lcd-test-$(VERNUM)
PACKAGE = $(PACKAGE_PATH)/$(VERSION)
##=======================================
## Setting Compiler FLAGS.
##=======================================
export EXE_CFLAGS = $(LOCAL_GLOBAL_CFLAGS)
export EXE_LDFLAGS = $(LOCAL_GLOBAL_LDFLAGS)
export EXE_EXT_FILE = $(shell pwd)/$(TARGET_CRTBEGIN_DYNAMIC_O) \
$(shell pwd)/$(TARGET_CRTEND_O)
##=======================================
.PHONY: distclean install_lcd_test distclean_lcd_test
droid: install_lcd_test
install_lcd_test: $(TARGET_CRTBEGIN_DYNAMIC_O) $(TARGET_CRTEND_O) $(TARGET_OUT_STATIC_LIBRARIES)/libm.so \
$(TARGET_OUT_STATIC_LIBRARIES)/libc.so $(TARGET_OUT_STATIC_LIBRARIES)/libdl.so \
$(TARGET_OUT_STATIC_LIBRARIES)/liblog.so
$(MAKE) -C $(PACKAGE) -f Makefile all
distclean: distclean_lcd_test
distclean_lcd_test:
@$(MAKE) -C $(PACKAGE) -f Makefile distclean
##################################################
主要將一些 ldflags , cflags export 到 下一層 Makefile 使用 , 其中 EXE_EXT_FILES 是因為執行檔 , 會需要這兩個 .o file , 並且很奇怪需要放在 同一個目錄下 , 我試過使用 --sysroot=<path> 的方式 , 也是不行 , 所以只好 copy 過去 make 完畢後 delete 掉.
接著在 ./lcd-test-0.0.1/ 下建立 Makefile , 這個 makefile 就是自己的方式.內容如下:
# ./test_apps/test/lcd-test/lcd-test-0.0.1/Makefile
#
# vi: set ts=4 :
##==========================================
TARGET_OUT := $(PWD)/$(PRODUCT_OUT)/system/bin/$(TARGET_EXE)
TARGET = $(TARGET_EXE)
CFLAGS = $(EXE_CFLAGS)
LDFLAGS = $(EXE_LDFLAGS)
##==========================================
## Setting target & source file.
##==========================================
CPP_SRCS = $(wildcard *.cpp)
C_SRCS = $(wildcard *.c)
CPP_OBJS = $(CPP_SRCS:.cpp=.opp)
C_OBJS = $(C_SRCS:.c=.o)
DEPEND_FILE = .depend
##==========================================
## Make Rules.
##==========================================
.PHONY: all distclean
all: $(DEPEND_FILE) $(TARGET_OUT)
$(TARGET_OUT): $(TARGET) Makefile
@echo " Install $(TARGET) ==> $(PRODUCT_OUT)/system/bin/"
@cp $(TARGET) $(PWD)/$(PRODUCT_OUT)/system/bin/
$(TARGET): $(C_OBJS) $(CPP_OBJS)
@echo " Building '$@' ... "
@cp $(foreach f,$(EXE_EXT_FILE), $(shell echo $f)) ./
@$(TARGET_CC) -o $@ $(C_OBJS) $(CPP_OBJS) $(LDFLAGS)
@rm $(foreach f,$(EXE_EXT_FILE), $(shell echo $f | sed 's/.*\///g'))
%.opp: %.cpp
@echo " Compiling '$<' ..."
@$(TARGET_CC) $(CFLAGS) -o $@ -c $<
%.o: %.c
@echo " Compiling '$<' ..."
@$(TARGET_CC) $(CFLAGS) -o $@ -c $<
distclean:
@echo " Distcleaning '$(TARGET)' ..."
@rm -f $(TARGET) $(C_OBJS) $(CPP_OBJS) $(DEPEND_FILE)
$(DEPEND_FILE):
@if [ -n "$(C_SRCS)" ] || [ -n "$(CPP_SRCS)" ] ; then \
echo " Generating '$@' ..." ;\
for i in $(C_SRCS) $(CPP_SRCS) ; do \
j=`echo $$i | sed 's/\.c/.o/g' ` ;\
$(TARGET_CC) $(CFLAGS) -M $$i -MT"$$j" >> $@ ;\
done ; \
fi
ifeq ($(DEPEND_FILE),$(wildcard $(DEPEND_FILE)))
include $(DEPEND_FILE)
endif
##==========================================
build 完畢後 ,利用 lrz 傳到 EVB 上的 /system/bin/ 下 . 執行看看 !!
成功....... ^^ , 不過會被原本 的畫面 re-flash 掉 , 所以建議找畫面更新率最低的頁面測試 , 或是使用 stop command 讓 java machine 暫停 , 在利用 console 執行測試程式.
Android 的 fb0 位置和一般linux 不同 , 移動到 /dev/graphics/fb0 位置.
2012年9月18日 星期二
2012年9月17日 星期一
Add Busybox into TI Android BSP
開發階段 , 很多東西都在 console 下進行一些 debug 的動作 , 有些小修改 , 卻沒有 vim 可以用 , 加上有些常用的 busybox command 都沒有 , 真的有點不方便 !!
之前有 lrz/lsz 的經驗 , 這次一樣如法泡製 , 將 busybox 移植到 TI 的 BSP 中 !!
一樣借用 linaro 這個組織的東西 網址 :
http://www.linaro.org/linux-on-arm
一樣修改一下 Android.mk file (格式和 debug message 修改 ) .
include $(CLEAR_VARS)
BUSYBOX_TOOLS := \
ar \
arp \
ash \
awk \
base64 \
basename \
beep \
blkid \
blockdev \
bunzip2 \
bzcat \
cal \
catv \
chat \
chattr \
chgrp \
chpst \
chroot \
chrt \
chvt \
cksum \
clear \
comm \
cp \
cpio \
cttyhack \
cut \
dc \
deallocvt \
depmod \
devmem \
diff \
dirname \
dnsd \
dos2unix \
dpkg \
dpkg-deb \
du \
dumpkmap \
echo \
ed \
egrep \
envdir \
envuidgid \
expand \
fakeidentd \
false \
fbset \
fbsplash \
fdflush \
fdformat \
fdisk \
fgconsole \
fgrep \
find \
findfs \
flash_lock \
flash_unlock \
flashcp \
flock \
fold \
freeramdisk \
ftpd \
ftpget \
ftpput \
fuser \
getopt \
grep \
gunzip \
halt \
hdparm \
head \
hexdump \
httpd \
ifdown \
ifup \
inotifyd \
install \
iostat \
ipaddr \
ipcalc \
iplink \
iproute \
iprule \
iptunnel \
klogd \
linuxrc \
loadkmap \
losetup \
lpd \
lpq \
lpr \
lsattr \
lspci \
lsusb \
lzcat \
lzma \
lzop \
lzopcat \
makedevs \
makemime \
man \
md5sum \
mesg \
mkfifo \
mknod \
mkswap \
mktemp \
modinfo \
modprobe \
more \
mpstat \
nbd-client \
nc \
nice \
nmeter \
nohup \
od \
openvt \
patch \
pidof \
pipe_progress \
pmap \
popmaildir \
poweroff \
printf \
pscan \
pstree \
pwd \
pwdx \
raidautorun \
rdev \
readlink \
readprofile \
realpath \
reformime \
reset \
resize \
rev \
rpm \
rpm2cpio \
rtcwake \
run-parts \
runsv \
runsvdir \
rx \
script \
scriptreplay \
sed \
sendmail \
seq \
setkeycodes \
setlogcons \
setserial \
setsid \
setuidgid \
sha1sum \
sha256sum \
sha512sum \
showkey \
smemcap \
softlimit \
sort \
split \
start-stop-daemon \
strings \
stty \
sum \
sv \
svlogd \
sysctl \
tac \
tail \
tar \
tcpsvd \
tee \
telnet \
telnetd \
test \
time \
timeout \
tr \
traceroute \
true \
ttysize \
tunctl \
tune2fs \
udhcpc \
uname \
uncompress \
unexpand \
uniq \
unix2dos \
unlzma \
unlzop \
unxz \
unzip \
uudecode \
uuencode \
vi \
volname \
watch \
wc \
wget \
which \
whoami \
whois \
xargs \
xz \
xzcat \
yes \
zcat
BB_TC_DIR := $(realpath $(shell dirname $(TARGET_TOOLS_PREFIX)))
BB_TC_PREFIX := $(shell basename $(TARGET_TOOLS_PREFIX))
BB_LDFLAGS := -nostdlib -Bdynamic -Wl,-z,muldefs$(shell if test $(PLATFORM_SDK_VERSION) -lt 16; then echo -ne ',-T../../$(BUILD_SYSTEM)/armelf.x'; fi),-dynamic-linker,/system/bin/linker,-z,nocopyreloc,--no-undefined ../../$(TARGET_CRTBEGIN_DYNAMIC_O) ../../$(TARGET_CRTEND_O) -L../../$(TARGET_OUT_STATIC_LIBRARIES)
# FIXME remove -fno-strict-aliasing once all aliasing violations are fixed
BB_COMPILER_FLAGS := $(subst -I ,-I../../,$(subst -include ,-include ../../,$(TARGET_GLOBAL_CFLAGS))) -I../../bionic/libc/include -I../../bionic/libc/kernel/common -I../../bionic/libc/arch-arm/include -I../../bionic/libc/kernel/arch-arm -I../../bionic/libm/include -fno-stack-protector -Wno-error=format-security -fno-strict-aliasing
BB_LDLIBS := dl m c gcc
ifneq ($(strip $(SHOW_COMMANDS)),)
BB_VERBOSE="V=1"
endif
.PHONY: build_busybox
FILE_BUSYBOX = $(PRODUCT_OUT)/system/bin/busybox
droid: build_busybox
systemtarball: symlinks
@echo "==================== systemtarball done ...... "
build_busybox: $(TARGET_CRTBEGIN_DYNAMIC_O) $(TARGET_CRTEND_O) $(TARGET_OUT_STATIC_LIBRARIES)/libm.so $(TARGET_OUT_STATIC_LIBRARIES)/libc.so $(TARGET_OUT_STATIC_LIBRARIES)/libdl.so
@if [ ! -e $(FILE_BUSYBOX) ];then \
echo "==================== Building Busybox ...... " ; \
cd external/busybox && \
sed -e "s|^CONFIG_CROSS_COMPILER_PREFIX=.*|CONFIG_CROSS_COMPILER_PREFIX=\"$(BB_TC_PREFIX)\"|;s|^CONFIG_EXTRA_CFLAGS=.*|CONFIG_EXTRA_CFLAGS=\"$(BB_COMPILER_FLAGS)\"|" configs/android_defconfig >.config && \
export PATH=$(BB_TC_DIR):$(PATH) && \
$(MAKE) -j1 oldconfig && \
$(MAKE) -j1 $(BB_VERBOSE) EXTRA_LDFLAGS="$(BB_LDFLAGS)" LDLIBS="$(BB_LDLIBS)" && \
mkdir -p ../../$(PRODUCT_OUT)/system/bin && \
cp busybox ../../$(PRODUCT_OUT)/system/bin/ ; \
cd - ; \
for link in $(BUSYBOX_TOOLS); do\
ln -sf busybox $(PRODUCT_OUT)/system/bin/$$link; done ; \
fi
symlinks: build_busybox
@echo "==================== Create busybox tools linking ...... " ;\
for link in $(BUSYBOX_TOOLS); do\
ln -sf busybox $(PRODUCT_OUT)/system/bin/$$link; done
最後可以在 out/target/product/am335xevm/system/bin/ 下看到 busybox 和一些 command 的 link .... 大功告成 ..
之前有 lrz/lsz 的經驗 , 這次一樣如法泡製 , 將 busybox 移植到 TI 的 BSP 中 !!
一樣借用 linaro 這個組織的東西 網址 :
http://www.linaro.org/linux-on-arm
一樣修改一下 Android.mk file (格式和 debug message 修改 ) .
include $(CLEAR_VARS)
BUSYBOX_TOOLS := \
ar \
arp \
ash \
awk \
base64 \
basename \
beep \
blkid \
blockdev \
bunzip2 \
bzcat \
cal \
catv \
chat \
chattr \
chgrp \
chpst \
chroot \
chrt \
chvt \
cksum \
clear \
comm \
cp \
cpio \
cttyhack \
cut \
dc \
deallocvt \
depmod \
devmem \
diff \
dirname \
dnsd \
dos2unix \
dpkg \
dpkg-deb \
du \
dumpkmap \
echo \
ed \
egrep \
envdir \
envuidgid \
expand \
fakeidentd \
false \
fbset \
fbsplash \
fdflush \
fdformat \
fdisk \
fgconsole \
fgrep \
find \
findfs \
flash_lock \
flash_unlock \
flashcp \
flock \
fold \
freeramdisk \
ftpd \
ftpget \
ftpput \
fuser \
getopt \
grep \
gunzip \
halt \
hdparm \
head \
hexdump \
httpd \
ifdown \
ifup \
inotifyd \
install \
iostat \
ipaddr \
ipcalc \
iplink \
iproute \
iprule \
iptunnel \
klogd \
linuxrc \
loadkmap \
losetup \
lpd \
lpq \
lpr \
lsattr \
lspci \
lsusb \
lzcat \
lzma \
lzop \
lzopcat \
makedevs \
makemime \
man \
md5sum \
mesg \
mkfifo \
mknod \
mkswap \
mktemp \
modinfo \
modprobe \
more \
mpstat \
nbd-client \
nc \
nice \
nmeter \
nohup \
od \
openvt \
patch \
pidof \
pipe_progress \
pmap \
popmaildir \
poweroff \
printf \
pscan \
pstree \
pwd \
pwdx \
raidautorun \
rdev \
readlink \
readprofile \
realpath \
reformime \
reset \
resize \
rev \
rpm \
rpm2cpio \
rtcwake \
run-parts \
runsv \
runsvdir \
rx \
script \
scriptreplay \
sed \
sendmail \
seq \
setkeycodes \
setlogcons \
setserial \
setsid \
setuidgid \
sha1sum \
sha256sum \
sha512sum \
showkey \
smemcap \
softlimit \
sort \
split \
start-stop-daemon \
strings \
stty \
sum \
sv \
svlogd \
sysctl \
tac \
tail \
tar \
tcpsvd \
tee \
telnet \
telnetd \
test \
time \
timeout \
tr \
traceroute \
true \
ttysize \
tunctl \
tune2fs \
udhcpc \
uname \
uncompress \
unexpand \
uniq \
unix2dos \
unlzma \
unlzop \
unxz \
unzip \
uudecode \
uuencode \
vi \
volname \
watch \
wc \
wget \
which \
whoami \
whois \
xargs \
xz \
xzcat \
yes \
zcat
BB_TC_DIR := $(realpath $(shell dirname $(TARGET_TOOLS_PREFIX)))
BB_TC_PREFIX := $(shell basename $(TARGET_TOOLS_PREFIX))
BB_LDFLAGS := -nostdlib -Bdynamic -Wl,-z,muldefs$(shell if test $(PLATFORM_SDK_VERSION) -lt 16; then echo -ne ',-T../../$(BUILD_SYSTEM)/armelf.x'; fi),-dynamic-linker,/system/bin/linker,-z,nocopyreloc,--no-undefined ../../$(TARGET_CRTBEGIN_DYNAMIC_O) ../../$(TARGET_CRTEND_O) -L../../$(TARGET_OUT_STATIC_LIBRARIES)
# FIXME remove -fno-strict-aliasing once all aliasing violations are fixed
BB_COMPILER_FLAGS := $(subst -I ,-I../../,$(subst -include ,-include ../../,$(TARGET_GLOBAL_CFLAGS))) -I../../bionic/libc/include -I../../bionic/libc/kernel/common -I../../bionic/libc/arch-arm/include -I../../bionic/libc/kernel/arch-arm -I../../bionic/libm/include -fno-stack-protector -Wno-error=format-security -fno-strict-aliasing
BB_LDLIBS := dl m c gcc
ifneq ($(strip $(SHOW_COMMANDS)),)
BB_VERBOSE="V=1"
endif
.PHONY: build_busybox
FILE_BUSYBOX = $(PRODUCT_OUT)/system/bin/busybox
droid: build_busybox
systemtarball: symlinks
@echo "==================== systemtarball done ...... "
build_busybox: $(TARGET_CRTBEGIN_DYNAMIC_O) $(TARGET_CRTEND_O) $(TARGET_OUT_STATIC_LIBRARIES)/libm.so $(TARGET_OUT_STATIC_LIBRARIES)/libc.so $(TARGET_OUT_STATIC_LIBRARIES)/libdl.so
@if [ ! -e $(FILE_BUSYBOX) ];then \
echo "==================== Building Busybox ...... " ; \
cd external/busybox && \
sed -e "s|^CONFIG_CROSS_COMPILER_PREFIX=.*|CONFIG_CROSS_COMPILER_PREFIX=\"$(BB_TC_PREFIX)\"|;s|^CONFIG_EXTRA_CFLAGS=.*|CONFIG_EXTRA_CFLAGS=\"$(BB_COMPILER_FLAGS)\"|" configs/android_defconfig >.config && \
export PATH=$(BB_TC_DIR):$(PATH) && \
$(MAKE) -j1 oldconfig && \
$(MAKE) -j1 $(BB_VERBOSE) EXTRA_LDFLAGS="$(BB_LDFLAGS)" LDLIBS="$(BB_LDLIBS)" && \
mkdir -p ../../$(PRODUCT_OUT)/system/bin && \
cp busybox ../../$(PRODUCT_OUT)/system/bin/ ; \
cd - ; \
for link in $(BUSYBOX_TOOLS); do\
ln -sf busybox $(PRODUCT_OUT)/system/bin/$$link; done ; \
fi
symlinks: build_busybox
@echo "==================== Create busybox tools linking ...... " ;\
for link in $(BUSYBOX_TOOLS); do\
ln -sf busybox $(PRODUCT_OUT)/system/bin/$$link; done
最後可以在 out/target/product/am335xevm/system/bin/ 下看到 busybox 和一些 command 的 link .... 大功告成 ..
2012年9月13日 星期四
Add JNI demo code ( java + C )
Pure Java 完成了 , 改換 JNI 方式吧 !! 將 a + b 的動作 用 C library 來完成 .
一樣使用之前 pure java 的 code , 如下列修改方式.
private OnClickListener mAddListener = new OnClickListener()
{
@Override
public void onClick(View view)
{
int a = Integer.parseInt(mEditText1.getText().toString());
int b = Integer.parseInt(mEditText2.getText().toString());
- int result = a+b;
+ int result = addJNI(a, b);
mButton.setText(String.valueOf("A+B=" + result));
}
};
並且在 .java 中加入 :
public native int addJNI(int a, int b);
static
{
System.loadLibrary("androint-jni");
}
## 注意 library 的名稱為 libandroint-jni.so , java 會自動將 lib prefix 補上去 , 所以JNI 產生的 so file , 要注意 , 也需要 lib 這個 perfix.
接著先 產生 .class file , 在由 .class file 產生 .h , 然後 .c file include 這個 .h 並且完成 function 的功能.
產生 .class 的方式可以用下列的command :
javac -d ./ -cp /${HOME}/android-sdk-linux/platforms/android-15/android.jar src/com/eps/william/androint/AndroIntActivity.java <source top>/out/target/common/R/com/eps/william/androint/R.java
接著用 javah 來產生 .h file , command 如下:
javah -jni -classpath ../bin com.eps.william.androint.AndroIntActivity
經過測試 ,沒有問題 , 只是..... 一切都不自動 , 需要手動產生, 並且還要先編譯 java 程式 , 然後手動產生 .h file , 然後 修正 .c file , 最後在 Android.mk 中加入 .so 的 make 部份 . 一點都不優......
經過幾天的 study , 修改了一下 Android.mk 內容 , 讓整個 make 過程中自動產生 .h 然後接著 compile .c file 產生 .so ...... , 修改後的 Android.mk 如下:
# Android.mk
#
# Copyright 2012 Jeff Hsieh <jeff@instant-jeff.instant>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
LOCAL_PATH:= $(call my-dir)
############################################
#### Jave APK
############################################
$(warning DEBUG **** BUILD_APK)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Sample_4
include $(BUILD_PACKAGE)
############################################
#### Jni Library.
############################################
#~ $(warning DEBUG **** BUILD_JNI)
#~
#~ include $(CLEAR_VARS)
#~
#~ LOCAL_MODULE_TAGS := optional
#~ LOCAL_SRC_FILES := $(call all-subdir-c-files)
#~ LOCAL_MODULE := libandroint-jni
#~
#~ LOCAL_LDLIBS := -llog
#~
#~ LOCAL_SHARED_LIBRARIES := libcutils
#~
#~ LOCAL_CHECKED_MODULE := build_jni_h
#~ LOCAL_REQUIRED_MODULES := build_jni_h
#~
#~ include $(BUILD_SHARED_LIBRARY)
.PHONY: distclean install_jni_so build_jni_hearder
droid: install_jni_so
############################################
#### create jni h file .
############################################
$(warning DEBUG **** Create JNI heard files.)
include $(CLEAR_VARS)
JNI_LOCAL_PKG_NAME := $(shell cat $(LOCAL_PATH)/AndroidManifest.xml | grep package | sed 's/.*=//g')
JNI_LOCAL_PKG_DIR := $(shell echo $(JNI_LOCAL_PKG_NAME) | sed 's/\./\//g' )
JNI_LOCAL_JAVA_SRC :=
JNI_LOCAL_JAVA_SRC += $(foreach f,$(call all-subdir-java-files), $(addprefix $(LOCAL_PATH)/,$f))
JNI_LOCAL_PATH := $(LOCAL_PATH)
JNI_JAVA_LIB := /home/jeff/Mydroid/android-sdk-linux/platforms/android-15/android.jar
JNI_R_JAVA := $(TARGET_COMMON_OUT_ROOT)/R/$(JNI_LOCAL_PKG_DIR)/R.java
build_jni_hearder: Sample_4
@if [ ! -e $(JNI_LOCAL_PATH)/bin ];then \
mkdir -p $(JNI_LOCAL_PATH)/bin ; \
fi
@if [ ! -e $(JNI_LOCAL_PATH)/bin/.jni_header ];then \
javac -d $(JNI_LOCAL_PATH)/bin -cp $(JNI_JAVA_LIB) $(JNI_R_JAVA) $(JNI_LOCAL_JAVA_SRC) ; \
javah -jni -classpath $(JNI_LOCAL_PATH)/bin -d $(JNI_LOCAL_PATH)/jni/jni_include $(JNI_LOCAL_PKG_NAME).AndroIntActivity ;\
fi
## javac -d ./ -cp /home/jeff/Mydroid/android-sdk-linux/platforms/android-15/android.jar \
## src/com/eps/william/androint/AndroIntActivity.java \
## /home/jeff/Mydroid/TI_Android/rowboat-android/out/target/common/R/com/eps/william/androint/R.java
## javah -jni -classpath ../bin com.eps.william.androint.AndroIntActivity
############################################
#### Jni Library.
############################################
include $(CLEAR_VARS)
export JNI_TC_DIR := $(realpath $(shell dirname $(TARGET_TOOLS_PREFIX)))
export JNI_TC_PREFIX := $(shell basename $(TARGET_TOOLS_PREFIX))
export TARGET_CC := $(JNI_TC_DIR)/$(JNI_TC_PREFIX)gcc
export JNI_COMPILER_FLAGS := $(subst -I ,-I $(shell pwd)/,$(subst -include ,-include $(shell pwd)/,$(TARGET_GLOBAL_CFLAGS))) \
$(foreach f,$(TARGET_C_INCLUDES), $(addprefix -I$(shell pwd)/,$f)) \
$(foreach f,$(TARGET_PROJECT_INCLUDES), $(addprefix -I$(shell pwd)/,$f)) \
$(foreach f,$(JNI_H_INCLUDE), $(addprefix -I$(shell pwd)/,$f)) \
export JNI_LDFLAGS := -L$(shell pwd)/$(TARGET_OUT_STATIC_LIBRARIES) -llog
export JNI_LOCAL_C_FILES := $(call all-subdir-c-files)
export PRODUCT_OUT := $(PRODUCT_OUT)
###################################
install_jni_so: build_jni_hearder \
$(TARGET_CRTBEGIN_DYNAMIC_O) $(TARGET_CRTEND_O) $(TARGET_OUT_STATIC_LIBRARIES)/libm.so \
$(TARGET_OUT_STATIC_LIBRARIES)/libc.so $(TARGET_OUT_STATIC_LIBRARIES)/libdl.so \
$(TARGET_OUT_STATIC_LIBRARIES)/liblog.so
@$(MAKE) -C $(JNI_LOCAL_PATH) -f Makefile.jni local_jni
############################################
#### Clean rules.
############################################
distclean:
@if [ -e $(JNI_LOCAL_PATH)/jni/jni_include ];then \
rm -rf $(JNI_LOCAL_PATH)/jni/jni_include ; \
fi
@echo "Remove temp file \"$(JNI_LOCAL_PATH)/bin/ \" .... "
@if [ -e $(JNI_LOCAL_PATH)/bin ];then \
rm -rf $(JNI_LOCAL_PATH)/bin ;\
fi
@$(MAKE) -C $(JNI_LOCAL_PATH) -f Makefile.jni $@
############################################
因為 Android 的 make rules 太複雜 ,並且支援 make -jN , 所以乾脆獨立出來 build so file . 所以會看到 "@$(MAKE) -C $(JNI_LOCAL_PATH) -f Makefile.jni local_jni " 的字串 , 其中 Makefile.jni 如下:
# Makefile.jni
#
# Copyright 2012 Jeff Hsieh <jeff@instant-jeff.instant>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
DEPEND_FILE := .jni_depend
JNI_SO_NAME := libandroint-jni.so
JNI_LOCAL_OBJ_FILES := $(JNI_LOCAL_C_FILES:.c=.o)
JNI_SO_TARGET := $(addprefix $(shell pwd)/,$(JNI_SO_NAME))
JNI_TARGET_OUT := $(PWD)/$(PRODUCT_OUT)/system/lib/$(JNI_SO_NAME)
.PHONY: local_jni distclean
local_jni: $(DEPEND_FILE) $(JNI_TARGET_OUT)
$(JNI_TARGET_OUT): $(JNI_SO_TARGET)
@echo " Install $(JNI_SO_NAME) ==> $(PRODUCT_OUT)/system/lib"
@cp $(JNI_SO_TARGET) $(PWD)/$(PRODUCT_OUT)/system/lib/
$(JNI_SO_TARGET): $(JNI_LOCAL_OBJ_FILES)
@echo " Building '$@' ... "
@$(TARGET_CC) -shared -Wl,-soname,$(JNI_SO_NAME) -o $@ $(JNI_LOCAL_OBJ_FILES) $(JNI_LDFLAGS)
%.o: %.c
@echo " Compiling '$<' ..."
@$(TARGET_CC) $(JNI_COMPILER_FLAGS) -o $@ -c $<
$(DEPEND_FILE):
@if [ -n "$(JNI_LOCAL_C_FILES)" ]; then \
echo " Generating '$@' ..." ;\
for i in $(JNI_LOCAL_C_FILES) ; do \
j=`echo $$i | sed 's/\.c/.o/g' ` ;\
$(TARGET_CC) $(JNI_COMPILER_FLAGS) -M $$i -MT"./$$j" >> $@ ;\
done ; \
fi
ifeq ($(DEPEND_FILE),$(wildcard $(DEPEND_FILE)))
include $(DEPEND_FILE)
endif
distclean:
@rm -f $(DEPEND_FILE) $(JNI_SO_TARGET) $(JNI_LOCAL_OBJ_FILES)
############################################
一樣使用之前 pure java 的 code , 如下列修改方式.
private OnClickListener mAddListener = new OnClickListener()
{
@Override
public void onClick(View view)
{
int a = Integer.parseInt(mEditText1.getText().toString());
int b = Integer.parseInt(mEditText2.getText().toString());
- int result = a+b;
+ int result = addJNI(a, b);
mButton.setText(String.valueOf("A+B=" + result));
}
};
並且在 .java 中加入 :
public native int addJNI(int a, int b);
static
{
System.loadLibrary("androint-jni");
}
## 注意 library 的名稱為 libandroint-jni.so , java 會自動將 lib prefix 補上去 , 所以JNI 產生的 so file , 要注意 , 也需要 lib 這個 perfix.
接著先 產生 .class file , 在由 .class file 產生 .h , 然後 .c file include 這個 .h 並且完成 function 的功能.
產生 .class 的方式可以用下列的command :
javac -d ./ -cp /${HOME}/android-sdk-linux/platforms/android-15/android.jar src/com/eps/william/androint/AndroIntActivity.java <source top>/out/target/common/R/com/eps/william/androint/R.java
接著用 javah 來產生 .h file , command 如下:
javah -jni -classpath ../bin com.eps.william.androint.AndroIntActivity
經過測試 ,沒有問題 , 只是..... 一切都不自動 , 需要手動產生, 並且還要先編譯 java 程式 , 然後手動產生 .h file , 然後 修正 .c file , 最後在 Android.mk 中加入 .so 的 make 部份 . 一點都不優......
經過幾天的 study , 修改了一下 Android.mk 內容 , 讓整個 make 過程中自動產生 .h 然後接著 compile .c file 產生 .so ...... , 修改後的 Android.mk 如下:
# Android.mk
#
# Copyright 2012 Jeff Hsieh <jeff@instant-jeff.instant>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
LOCAL_PATH:= $(call my-dir)
############################################
#### Jave APK
############################################
$(warning DEBUG **** BUILD_APK)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Sample_4
include $(BUILD_PACKAGE)
############################################
#### Jni Library.
############################################
#~ $(warning DEBUG **** BUILD_JNI)
#~
#~ include $(CLEAR_VARS)
#~
#~ LOCAL_MODULE_TAGS := optional
#~ LOCAL_SRC_FILES := $(call all-subdir-c-files)
#~ LOCAL_MODULE := libandroint-jni
#~
#~ LOCAL_LDLIBS := -llog
#~
#~ LOCAL_SHARED_LIBRARIES := libcutils
#~
#~ LOCAL_CHECKED_MODULE := build_jni_h
#~ LOCAL_REQUIRED_MODULES := build_jni_h
#~
#~ include $(BUILD_SHARED_LIBRARY)
.PHONY: distclean install_jni_so build_jni_hearder
droid: install_jni_so
############################################
#### create jni h file .
############################################
$(warning DEBUG **** Create JNI heard files.)
include $(CLEAR_VARS)
JNI_LOCAL_PKG_NAME := $(shell cat $(LOCAL_PATH)/AndroidManifest.xml | grep package | sed 's/.*=//g')
JNI_LOCAL_PKG_DIR := $(shell echo $(JNI_LOCAL_PKG_NAME) | sed 's/\./\//g' )
JNI_LOCAL_JAVA_SRC :=
JNI_LOCAL_JAVA_SRC += $(foreach f,$(call all-subdir-java-files), $(addprefix $(LOCAL_PATH)/,$f))
JNI_LOCAL_PATH := $(LOCAL_PATH)
JNI_JAVA_LIB := /home/jeff/Mydroid/android-sdk-linux/platforms/android-15/android.jar
JNI_R_JAVA := $(TARGET_COMMON_OUT_ROOT)/R/$(JNI_LOCAL_PKG_DIR)/R.java
build_jni_hearder: Sample_4
@if [ ! -e $(JNI_LOCAL_PATH)/bin ];then \
mkdir -p $(JNI_LOCAL_PATH)/bin ; \
fi
@if [ ! -e $(JNI_LOCAL_PATH)/bin/.jni_header ];then \
javac -d $(JNI_LOCAL_PATH)/bin -cp $(JNI_JAVA_LIB) $(JNI_R_JAVA) $(JNI_LOCAL_JAVA_SRC) ; \
javah -jni -classpath $(JNI_LOCAL_PATH)/bin -d $(JNI_LOCAL_PATH)/jni/jni_include $(JNI_LOCAL_PKG_NAME).AndroIntActivity ;\
fi
## javac -d ./ -cp /home/jeff/Mydroid/android-sdk-linux/platforms/android-15/android.jar \
## src/com/eps/william/androint/AndroIntActivity.java \
## /home/jeff/Mydroid/TI_Android/rowboat-android/out/target/common/R/com/eps/william/androint/R.java
## javah -jni -classpath ../bin com.eps.william.androint.AndroIntActivity
############################################
#### Jni Library.
############################################
include $(CLEAR_VARS)
export JNI_TC_DIR := $(realpath $(shell dirname $(TARGET_TOOLS_PREFIX)))
export JNI_TC_PREFIX := $(shell basename $(TARGET_TOOLS_PREFIX))
export TARGET_CC := $(JNI_TC_DIR)/$(JNI_TC_PREFIX)gcc
export JNI_COMPILER_FLAGS := $(subst -I ,-I $(shell pwd)/,$(subst -include ,-include $(shell pwd)/,$(TARGET_GLOBAL_CFLAGS))) \
$(foreach f,$(TARGET_C_INCLUDES), $(addprefix -I$(shell pwd)/,$f)) \
$(foreach f,$(TARGET_PROJECT_INCLUDES), $(addprefix -I$(shell pwd)/,$f)) \
$(foreach f,$(JNI_H_INCLUDE), $(addprefix -I$(shell pwd)/,$f)) \
export JNI_LDFLAGS := -L$(shell pwd)/$(TARGET_OUT_STATIC_LIBRARIES) -llog
export JNI_LOCAL_C_FILES := $(call all-subdir-c-files)
export PRODUCT_OUT := $(PRODUCT_OUT)
###################################
install_jni_so: build_jni_hearder \
$(TARGET_CRTBEGIN_DYNAMIC_O) $(TARGET_CRTEND_O) $(TARGET_OUT_STATIC_LIBRARIES)/libm.so \
$(TARGET_OUT_STATIC_LIBRARIES)/libc.so $(TARGET_OUT_STATIC_LIBRARIES)/libdl.so \
$(TARGET_OUT_STATIC_LIBRARIES)/liblog.so
@$(MAKE) -C $(JNI_LOCAL_PATH) -f Makefile.jni local_jni
############################################
#### Clean rules.
############################################
distclean:
@if [ -e $(JNI_LOCAL_PATH)/jni/jni_include ];then \
rm -rf $(JNI_LOCAL_PATH)/jni/jni_include ; \
fi
@echo "Remove temp file \"$(JNI_LOCAL_PATH)/bin/ \" .... "
@if [ -e $(JNI_LOCAL_PATH)/bin ];then \
rm -rf $(JNI_LOCAL_PATH)/bin ;\
fi
@$(MAKE) -C $(JNI_LOCAL_PATH) -f Makefile.jni $@
############################################
因為 Android 的 make rules 太複雜 ,並且支援 make -jN , 所以乾脆獨立出來 build so file . 所以會看到 "@$(MAKE) -C $(JNI_LOCAL_PATH) -f Makefile.jni local_jni " 的字串 , 其中 Makefile.jni 如下:
# Makefile.jni
#
# Copyright 2012 Jeff Hsieh <jeff@instant-jeff.instant>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
DEPEND_FILE := .jni_depend
JNI_SO_NAME := libandroint-jni.so
JNI_LOCAL_OBJ_FILES := $(JNI_LOCAL_C_FILES:.c=.o)
JNI_SO_TARGET := $(addprefix $(shell pwd)/,$(JNI_SO_NAME))
JNI_TARGET_OUT := $(PWD)/$(PRODUCT_OUT)/system/lib/$(JNI_SO_NAME)
.PHONY: local_jni distclean
local_jni: $(DEPEND_FILE) $(JNI_TARGET_OUT)
$(JNI_TARGET_OUT): $(JNI_SO_TARGET)
@echo " Install $(JNI_SO_NAME) ==> $(PRODUCT_OUT)/system/lib"
@cp $(JNI_SO_TARGET) $(PWD)/$(PRODUCT_OUT)/system/lib/
$(JNI_SO_TARGET): $(JNI_LOCAL_OBJ_FILES)
@echo " Building '$@' ... "
@$(TARGET_CC) -shared -Wl,-soname,$(JNI_SO_NAME) -o $@ $(JNI_LOCAL_OBJ_FILES) $(JNI_LDFLAGS)
%.o: %.c
@echo " Compiling '$<' ..."
@$(TARGET_CC) $(JNI_COMPILER_FLAGS) -o $@ -c $<
$(DEPEND_FILE):
@if [ -n "$(JNI_LOCAL_C_FILES)" ]; then \
echo " Generating '$@' ..." ;\
for i in $(JNI_LOCAL_C_FILES) ; do \
j=`echo $$i | sed 's/\.c/.o/g' ` ;\
$(TARGET_CC) $(JNI_COMPILER_FLAGS) -M $$i -MT"./$$j" >> $@ ;\
done ; \
fi
ifeq ($(DEPEND_FILE),$(wildcard $(DEPEND_FILE)))
include $(DEPEND_FILE)
endif
distclean:
@rm -f $(DEPEND_FILE) $(JNI_SO_TARGET) $(JNI_LOCAL_OBJ_FILES)
############################################
訂閱:
意見 (Atom)