4,436
社区成员
发帖
与我相关
我的任务
分享
本程序能够实现在android中利用system函数执行命令,并将执行结果输出到指定的文件中。
核心代码:
hello-jni.c
view sourceprint?01 #include <jni.h>
02 #include <string.h>
03 JNIEXPORT jstring Java_com_example_hellojni_HelloJni_system(JNIEnv* env,jobject thiz, jstring str , jstring path)
04 {
05 const jbyte* p1 = (*env)->GetStringUTFChars(env , str, NULL);
06 const jbyte* p2 = (*env)->GetStringUTFChars(env , path, NULL);
07 char* str2=(char *)p1;
08 char* path2=(char *)p2;
09 strcat(str2," > ");
10 strcat(str2,path2);
11 system(str2);
12 return(*env)->NewStringUTF(env,str2);
13 }
HelloJni.java
view sourceprint?01 package com.example.hellojni;
02 import android.app.Activity;
03 import android.widget.TextView;
04 import android.os.Bundle;
05
06 public class HelloJni extends Activity
07 {
08 /** Called when the activity is first created. */
09 @Override
10 public void onCreate(Bundle savedInstanceState)
11 {
12 super.onCreate(savedInstanceState);
13 TextView tv = new TextView(this);
14 tv.setText(system("ls","\"/mnt/sdcard/hello.txt\"") );//这里的第一个参数是要执行的命令,第二个参数是要输出的文件路径
15 setContentView(tv);
16 }
17 public native String system(String str ,String path);
18 static {
19 System.loadLibrary("hello-jni");
20 }
21 }
源代码代码:
本站下载:hello-jni
备用下载:leo108.com_614411.zip
請問
因為我想利用JNI方式使用libUSB
但是因為對so製作不是很懂,
所以想了解一下觀念,
目前手上有官方下載的libUSB-1.0.8的檔案,我想寫一隻 callLibUsb.c 的程式去使用libusb的方法
然後再用ndk-buld指令包裝成callLibUsb.so 給java使用,這樣做事可以的嗎?
這過程中我需要去make libusb嗎?
還是我也可以只將libUSB-1.0.8做ndk-build呢?然後直接給Java引用?
因為整個封裝後,要給android環境下來呼叫使用.
希望有人可以不吝指教一下.
謝謝~!
Hey Guys,
I'm writing an application that requires a custom USB driver, and i need LibUSB to be able to do it. I downloaded the following:
android.git.kernel.org Git - platform/external/libusb.git/summary
My problem is that when i try to cross compile using ./configure this lib, i get the error: unsupported operating system. I've been trying to configure like this:
Code:
./configure --host=arm-eabi CC=arm-linux-androideabi-gcc CPPFLAGS="-I$NDK_ROOT/platforms/android-3/arch-arm/usr/include/" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/" LIBS="-lc"The main aim is to be able to use the library with the Android NDK. If anybody has managed this - or could help me with this, it would be most appreciated! (Also - i am using Ubuntu)
Cheers, Tony.
1、Download Src:http://sourceforge.net/projects/libusb/files/libusb-1.0/
Now the latest release version is 1.0.8,I download version was 1.0.3
2、unpack the src:
tar xjvf libusb-1.0.3.tar.bz2
3、configure:
cd libusb-1.0.3
./configure --host=arm-linux CC=/usr/local/arm/3.4.1/bin/arm-linux-gcc CFLAGS="-Wall -fno-common -g -O2"
then copy it to android2.1 externel directory
4、Create Android.mk
Create Android.mk files in each of the libusb directories. In the top folder your Android.mk just calls your mk files in the libusb subfolders :
LOCAL_PATH := $(call my-dir)
ubdirs := $(addprefix $(LOCAL_PATH)/,$(addsuffix /Android.mk, \
libusb \
))
include $(subdirs)
in the libusb folder add the following Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
core.c \
descriptor.c \
io.c \
sync.c \
os/linux_usbfs.c�
LOCAL_C_INCLUDES += \
external/libusb-1.0.3/ \
external/libusb-1.0.3/libusb/ \
external/libusb-1.0.3/libusb/os�
LOCAL_MODULE:= libusb
include $(BUILD_SHARED_LIBRARY)
5、compile
Soon we are ready to compile the library; if we do so now, we will notice that the macro�TIMESPEC_TO_TIMEVAL is not defined by the sources inside Android. To overcome this problem we just need to define the macro in the�lubusb/io.c file
#define TIMESPEC_TO_TIMEVAL(tv, ts) \
do { \
(tv)->tv_sec = (ts)->tv_sec; \
(tv)->tv_usec = (ts)->tv_nsec / 1000; \
} while (0)
If you compile your libusb now, there is going to be the following error: build/tools/apriori/prelinkmap.c(137): library ‘libusb.so’ not in prelink map. This is because Android keeps track of all the shared libraries of the root file system (RFS). Therefore we need to add the library to the prelinked map found in ./build/core/prelink-linux-arm.map. We did so and just added libusb.so below libqcamera.so.
libqcamera.so 0xA9400000
libusb.so 0xA8000000
You are now all set to compile libusb. Execute the following commands in your Android root directory:
$ . build/envsetup.sh
$ choosecombo
mm
6、compile lsusb
Now we need to compile lsusb. The sources are located in the examples folder of libusb. Create a folder lsusb in the ./external directory of the Android sources. Copy the lsusb.c module into it and create a Android.mk file
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= lsusb.c
LOCAL_MODULE := lsusb
LOCAL_C_INCLUDES += external/libusb-1.0.3/
LOCAL_SHARED_LIBRARIES := libc libusb
include $(BUILD_EXECUTABLE
Hi all
I've run into some trouble while porting some code to android. I took
a code snippet from our linux application that communicates with our
device through libusb and began porting it. Using the android-ndk-r5b
everything compiles fine. I copied over the libusb source and included
it as a static library in my shared library, then I load the shared
library from my app.
On the android phone (Samsung Galaxy S2) I plug in our device using an
OTG cable and then mount the usbfs with devmode=0666.
The problem occurs at the point in the code where
libusb_control_transfer() is called. Keep in mind that up until this
point other libusb commands like libusb_get_device_list(),
libusb_get_device_descriptor, libusb_claim_interface() are successful
and I can find my device by checking vid pid of available devices.
I've tried to track down in the kernel source where the timeout occurs
and I've found that in the file drivers/usb/core/message.c in the
function usb_start_wait_urb(), that usb_submit_urb() returns
successfully, but then times out right after that. Seems like the urb
is queued and that's it, my app hangs waiting for its completion.
Any help or pointers on what to do from here or where to look?
我的交叉编译器是:arm-elf-gcc
./configure --build=i686-linux --host=arm-elf --prefix=/8620L/libusb-0.1.12/install
出现以下错误:
checking for what USB OS support... unknown operating system
configure: error: libusb does not support compiling for arm-unknown-elf
望高人能解答
本人是新手,最近想学习在android系统上移植USB,3G模块
之前用arm-none-linux-gnueabi进行交叉编译,可以编译成功,但移植到板子上的android系统时却显示
./usb_modeswitch command not found
怀疑是由于板子的android系统是用arm-eabi交叉编译的。
之后我便想用arm-eabi交叉编译libusb,结果出错,请大家帮帮我看这错误是怎么回事。
具体错误如下:
WSCEC@ubuntu:~$ cd 3G/123/libusb-0.1.12/
WSCEC@ubuntu:~/3G/123/libusb-0.1.12$ source ../anv
WSCEC@ubuntu:~/3G/123/libusb-0.1.12$ export ROOT=/home/WSCEC/Android/out/target/product/omap3evm/obj
WSCEC@ubuntu:~/3G/123/libusb-0.1.12$ ./configure --build=i686-linux --host=arm-eabi CPPFLAGS="-I$ROOT/include/" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$ROOT/lib/ -L$ROOT/lib/" LIBS="-lc" --prefix=/home/WSCEC/3G/123/libusb-0.1.12/install
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... no
checking for mawk... mawk
……(中间省略)
checking whether stripping libraries is possible... yes
appending configuration tag "F77" to libtool
checking for what USB OS support... unknown operating system
configure: error: libusb does not support compiling for arm-unknown-eabi
其实anv是我弄的环境变量。
后面我看了下config.log:如下:
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
It was created by libusb configure 0.1.12, which was
generated by GNU Autoconf 2.59. Invocation command line was
$ ./configure --build=i686-linux --host=arm-eabi CPPFLAGS=-I/home/WSCEC/Android/out/target/product/omap3evm/obj/include/ CFLAGS=-nostdlib LDFLAGS=-Wl,-rpath-link=/home/WSCEC/Android/out/target/product/omap3evm/obj/lib/ -L/home/WSCEC/Android/out/target/product/omap3evm/obj/lib/ LIBS=-lc --prefix=/home/WSCEC/3G/123/libusb-0.1.12/install
## --------- ##
## Platform. ##
## --------- ##
hostname = ubuntu
uname -m = i686
uname -r = 2.6.38-10-generic
uname -s = Linux
uname -v = #46-Ubuntu SMP Tue Jun 28 15:05:41 UTC 2011
……(省略)
configure:2828: $? = 0
configure:2832: test -z
|| test ! -s conftest.err
configure:2835: $? = 0
configure:2838: test -s conftest.o
configure:2841: $? = 0
configure:2852: result: yes
configure:2869: checking for arm-eabi-gcc option to accept ANSI C
configure:2939: arm-eabi-gcc -c -nostdlib -I/home/WSCEC/Android/out/target/product/omap3evm/obj/include/ conftest.c >&5
conftest.c:17:19: error: stdio.h: No such file or directory
conftest.c:18:23: error: sys/types.h: No such file or directory
conftest.c:19:22: error: sys/stat.h: No such file or directory
conftest.c:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
conftest.c:51: error: expected declaration specifiers or '...' before 'FILE'
……
#endif
#ifdef __cplusplus
void exit (int);
configure: exit 1
求各位大神们 帮帮我看看怎么回事