有时候需要把代码制作成jar包放入到项目中,如果只是一个普通的java文件导入系统都是没有问题的,但是很有可能需要将service导出jar包到,由系统启动。
1.通常只需要修改SystemService.java,比如jar包为AutoService_v0.1.jar,把该文件拷贝到framework/base/services/java/目录下。
2.修改framework/base/services/java目录下的Android.mk文件,这样才会编译到系统,通常都是编译到services.java里面,这不是绝对的。
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := AutoService:AutoService_v0.1.jar
include $(BUILD_MULTI_PREBUILT)
# the library
# ============================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
$(call all-subdir-java-files) \
com/android/server/EventLogTags.logtags \
com/android/server/am/EventLogTags.logtags
LOCAL_SRC_FILES += $(call all-Iaidl-files-under,com)
LOCAL_MODULE:= services
LOCAL_JAVA_LIBRARIES := android.policy conscrypt telephony-common
AutoService
include $(BUILD_JAVA_LIBRARY)
include $(BUILD_DROIDDOC)
3.启动autoservice,这时需要修改SystemService.java,修改如下。
4.最后是编译,我们可以用mmm方式进行编译,这样会节省编译时间。
mmm framework/base/services/java
mmm编译生成services.jar文件,把该文件push到机器的/system/framework目录下,重启机器。也许你会有很大的惊喜,也许会让你失望。不过没关系,办法总是有的。
5.下面来看看错误信息
D/SensorService( 2614): nuSensorService thread starting...
I/dalvikvm( 2614): Could not find method com.semisky.autoservice.services.AutoService.getInstance, referenced from method com.android.server.ServerThread.initAndLoop
W/dalvikvm( 2614): VFY: unable to resolve static method 14434: Lcom/semisky/autoservice/services/AutoService;.getInstance ()Lcom/semisky/autoservice/services/AutoService;
D/dalvikvm( 2614): VFY: replacing opcode 0x71 at 0x0753
I/installd( 2253): new connection
...
...
I/SystemServer( 2614): Print AutoService
W/SystemServer( 2614): ***********************************************
F/SystemServer( 2614): BOOT FAILURE starting AutoService
F/SystemServer( 2614): java.lang.NoClassDefFoundError: com.semisky.autoservice.services.AutoService
F/SystemServer( 2614): at com.android.server.ServerThread.initAndLoop(SystemServer.java:817)
F/SystemServer( 2614): at com.android.server.SystemServer.main(SystemServer.java:1191)
F/SystemServer( 2614): at java.lang.reflect.Method.invokeNative(Native Method)
F/SystemServer( 2614): at java.lang.reflect.Method.invoke(Method.java:515)
F/SystemServer( 2614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
F/SystemServer( 2614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
F/SystemServer( 2614): at dalvik.system.NativeStart.main(Native Method)
上面的错误信息让人头痛,从字面意思来看说是找不到
com.semisky.autoservice.services.AutoService.getInstance方法,其实AutoService里面是有getInstance方法的。
6.解决办法
将Android.mk文件改成如下,重新编译push到机器上,解决此问题。
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := AutoService:AutoService_v0.1.jar
include $(BUILD_MULTI_PREBUILT)
# the library
# ============================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
$(call all-subdir-java-files) \
com/android/server/EventLogTags.logtags \
com/android/server/am/EventLogTags.logtags
LOCAL_SRC_FILES += $(call all-Iaidl-files-under,com)
LOCAL_MODULE:= services
LOCAL_JAVA_LIBRARIES := android.policy conscrypt telephony-common
LOCAL_STATIC_JAVA_LIBRARIES := AutoService
include $(BUILD_JAVA_LIBRARY)
include $(BUILD_DROIDDOC)