android4.4系统解决“ERRORcouldn't find native method”方法
今天笔者在移植一个tv模块从android4.2到android4.4系统的设备上,同样的代码,同样的方法,就是运行不起来。大概方法就是上层写一个apk,调用一个静态的java库,java库加载调用JNI的库文件,这一套代码在4.2上都是好好的,正常能跑,移到4.4上编译也都没有问题,运行会出下面的问题:
/*****************************************************************************************************/
声明:本博内容均由http://blog.csdn.net/edsam49原创,转载请注明出处,谢谢!
/*****************************************************************************************************/
错误信息如下:
07-01 20:57:29.200 I/ActivityManager( 1613): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.softwinner.cvbs/.CVBS} from pid 213907-01 20:57:29.320 I/ActivityManager( 1613): Start proc com.softwinner.cvbs for activity com.softwinner.cvbs/.CVBS: pid=2878 uid=10022 gids={50022}07-01 20:57:29.420 I/PackageManager( 1613): Running dexopt on: com.softwinner.cvbs07-01 20:57:29.540 D/dalvikvm( 2891): DexOpt: load 14ms, verify+opt 41ms, 273740 bytes07-01 20:57:29.570 V/PhoneStatusBar( 1691): setLightsOn(true)07-01 20:57:29.800 E/dalvikvm( 2878): ERROR: couldn't find native method07-01 20:57:29.800 E/dalvikvm( 2878): Requested: Lsoftwinner/tv/TVDecoder;.connect:(IIII)I07-01 20:57:29.800 E/dalvikvm( 2878): JNI posting fatal error: RegisterNatives failed for 'softwinner/tv/TVDecoder'; aborting...07-01 20:57:29.800 I/dalvikvm( 2878): "main" prio=5 tid=1 NATIVE07-01 20:57:29.800 I/dalvikvm( 2878): | group="main" sCount=0 dsCount=0 obj=0x41533c90 self=0x4146c58007-01 20:57:29.800 I/dalvikvm( 2878): | sysTid=2878 nice=0 sched=0/0 cgrp=apps handle=107387323607-01 20:57:29.810 I/dalvikvm( 2878): | state=R schedstat=( 0 0 0 ) utm=11 stm=6 core=107-01 20:57:29.820 I/dalvikvm( 2878): #00 pc 000012f6 /system/lib/libcorkscrew.so (unwind_backtrace_thread+29)07-01 20:57:29.820 I/dalvikvm( 2878): #01 pc 000604ec /system/lib/libdvm.so (dvmDumpNativeStack(DebugOutputTarget const*, int)+31)07-01 20:57:29.820 I/dalvikvm( 2878): #02 pc 000544e8 /system/lib/libdvm.so (dvmDumpThreadEx(DebugOutputTarget const*, Thread*, bool)+395)07-01 20:57:29.820 I/dalvikvm( 2878): #03 pc 00054556 /system/lib/libdvm.so (dvmDumpThread(Thread*, bool)+25)07-01 20:57:29.820 I/dalvikvm( 2878): #04 pc 00048f90 /system/lib/libdvm.so07-01 20:57:29.820 I/dalvikvm( 2878): #05 pc 00001fd2 /system/lib/libnativehelper.so (jniRegisterNativeMethods+81)07-01 20:57:29.820 I/dalvikvm( 2878): #06 pc 00000ac6 /system/lib/libtvdecoder_jni.so (JNI_OnLoad+41)07-01 20:57:29.820 I/dalvikvm( 2878): #07 pc 0004feba /system/lib/libdvm.so (dvmLoadNativeCode(char const*, Object*, char**)+465)07-01 20:57:29.820 I/dalvikvm( 2878): #08 pc 00067590 /system/lib/libdvm.so07-01 20:57:29.820 I/dalvikvm( 2878): #09 pc 00026f60 /system/lib/libdvm.so07-01 20:57:29.820 I/dalvikvm( 2878): #10 pc 0002deec /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)07-01 20:57:29.820 I/dalvikvm( 2878): #11 pc 0002b598 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)07-01 20:57:29.820 I/dalvikvm( 2878): #12 pc 0006041e /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+337)07-01 20:57:29.820 I/dalvikvm( 2878): #13 pc 00060442 /system/lib/libdvm.so (dvmCallMethod(Thread*, Method const*, Object*, JValue*, ...)+19)07-01 20:57:29.820 I/dalvikvm( 2878): #14 pc 0006ba88 /system/lib/libdvm.so (dvmInitClass+1019)07-01 20:57:29.820 I/dalvikvm( 2878): #15 pc 00021e5c /system/lib/libdvm.so (dvmAsmSisterStart+412)07-01 20:57:29.820 I/dalvikvm( 2878): #16 pc 0002deec /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)07-01 20:57:29.820 I/dalvikvm( 2878): #17 pc 0002b598 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)07-01 20:57:29.820 I/dalvikvm( 2878): #18 pc 00060700 /system/lib/libdvm.so (dvmInvokeMethod(Object*, Method const*, ArrayObject*, ArrayObject*, ClassObject*, bool)+391)07-01 20:57:29.820 I/dalvikvm( 2878): #19 pc 0006866e /system/lib/libdvm.so07-01 20:57:29.820 I/dalvikvm( 2878): #20 pc 00026f60 /system/lib/libdvm.so07-01 20:57:29.820 I/dalvikvm( 2878): #21 pc 0002deec /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)07-01 20:57:29.820 I/dalvikvm( 2878): #22 pc 0002b598 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)07-01 20:57:29.820 I/dalvikvm( 2878): #23 pc 0006041e /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+337)07-01 20:57:29.820 I/dalvikvm( 2878): #24 pc 00049bda /system/lib/libdvm.so07-01 20:57:29.820 I/dalvikvm( 2878): #25 pc 0004ce80 /system/lib/libandroid_runtime.so07-01 20:57:29.820 I/dalvikvm( 2878): #26 pc 0004dba6 /system/lib/libandroid_runtime.so (android::AndroidRuntime::start(char const*, char const*)+353)07-01 20:57:29.830 I/dalvikvm( 2878): #27 pc 0000105a /system/bin/app_process07-01 20:57:29.830 I/dalvikvm( 2878): #28 pc 0000e3e8 /system/lib/libc.so (__libc_init+47)07-01 20:57:29.830 I/dalvikvm( 2878): at java.lang.Runtime.nativeLoad(Native Method)07-01 20:57:29.830 I/dalvikvm( 2878): at java.lang.Runtime.doLoad(Runtime.java:421)07-01 20:57:29.830 I/dalvikvm( 2878): at java.lang.Runtime.loadLibrary(Runtime.java:362)07-01 20:57:29.830 I/dalvikvm( 2878): at java.lang.System.loadLibrary(System.java:526)07-01 20:57:29.830 I/dalvikvm( 2878): at softwinner.tv.TVDecoder.<clinit>(TVDecoder.java:11)07-01 20:57:29.830 I/dalvikvm( 2878): at com.softwinner.cvbs.CVBS.onCreate(CVBS.java:-1)07-01 20:57:29.830 I/dalvikvm( 2878): at android.app.Activity.performCreate(Activity.java:5243)07-01 20:57:29.830 I/dalvikvm( 2878): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)07-01 20:57:29.830 I/dalvikvm( 2878): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)07-01 20:57:29.830 I/dalvikvm( 2878): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.j
很悲剧,老老实实查了几遍,包引用,native名称,jin名称映射,编译这些看起来都没有问题,为什么呢?笔者也Google了一遍,还好有一些同道中人啊!都是proguard惹的祸!因为它可能会优化掉这些jni native类,在编译静态java库的时候,如果不特别指定proguard的话,会出问题,下面看看build系统的 java_library.mk文件:
ifeq ($(LOCAL_PROGUARD_ENABLED),disabled) LOCAL_PROGUARD_ENABLED :=endif
ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)# No dex; all we want are the .class files with resources.$(common_javalib.jar) : $(java_resource_sources)ifdef LOCAL_PROGUARD_ENABLED$(common_javalib.jar) : $(full_classes_proguard_jar)else$(common_javalib.jar) : $(full_classes_jar)endif @echo "target Static Jar: $(PRIVATE_MODULE) ($@)" $(copy-file-to-target)ifneq ($(extra_jar_args),) $(add-java-resources-to-package)endif$(LOCAL_BUILT_MODULE): $(common_javalib.jar) $(copy-file-to-target)else # !LOCAL_IS_STATIC_JAVA_LIBRARY$(common_javalib.jar): PRIVATE_DEX_FILE := $(built_dex)$(common_javalib.jar) : $(built_dex) $(java_resource_sources) @echo "target Jar: $(PRIVATE_MODULE) ($@)" $(create-empty-package) $(add-dex-to-package) $(add-carried-java-resources)ifneq ($(extra_jar_args),) $(add-java-resources-to-package)endififdef LOCAL_DEX_PREOPTdexpreopt_boot_jar_module := $(filter $(LOCAL_MODULE),$(DEXPREOPT_BOOT_JARS_MODULES))ifneq ($(dexpreopt_boot_jar_module),)# boot jar's rules are defined in dex_preopt.mkdexpreopted_boot_jar := $(DEXPREOPT_BOOT_JAR_DIR_FULL_PATH)/$(dexpreopt_boot_jar_module)_nodex.jar$(LOCAL_BUILT_MODULE) : $(dexpreopted_boot_jar) | $(ACP) $(call copy-file-to-target)dexpreopted_boot_odex := $(DEXPREOPT_BOOT_JAR_DIR_FULL_PATH)/$(dexpreopt_boot_jar_module).odexbuilt_odex := $(basename $(LOCAL_BUILT_MODULE)).odex$(built_odex) : $(dexpreopted_boot_odex) | $(ACP) $(call copy-file-to-target)else # dexpreopt_boot_jar_modulebuilt_odex := $(basename $(LOCAL_BUILT_MODULE)).odex$(built_odex): PRIVATE_MODULE := $(LOCAL_MODULE)# Make sure the boot jars get dex-preopt-ed first$(built_odex) : $(DEXPREOPT_BOOT_ODEXS)$(built_odex) : $(common_javalib.jar) | $(DEXPREOPT) $(DEXOPT) @echo "Dexpreopt Jar: $(PRIVATE_MODULE) ($@)" $(hide) rm -f $@ @mkdir -p $(dir $@) $(call dexpreopt-one-file,$<,$@)$(LOCAL_BUILT_MODULE) : $(common_javalib.jar) | $(ACP) $(call copy-file-to-target)ifneq (nostripping,$(LOCAL_DEX_PREOPT)) $(call dexpreopt-remove-classes.dex,$@)endifendif # dexpreopt_boot_jar_moduleelse # LOCAL_DEX_PREOPT$(LOCAL_BUILT_MODULE) : $(common_javalib.jar) | $(ACP) $(call copy-file-to-target)endif # LOCAL_DEX_PREOPTendif # !LOCAL_IS_STATIC_JAVA_LIBRARYLOCAL_PROGUARD_ENABLED默认是打开的,不特别指定的话,就是起作用的,就可能做优化。这个功能让我想起来了,很早以前做很底层寄存器操作的时候,要加上volitate 来防止编译器做优化一样。
只需要再Makefile中加入一行即可解决。如下:
LOCAL_PROGUARD_ENABLED:= disabled
编译还是照旧,运行还是照旧,可是就是出来了,真的不错,没搞清楚之前,很累人啊,加油!