本文共 2374 字,大约阅读时间需要 7 分钟。
本帖最后由 walxlgf 于 2011-5-26 14:20 编辑 临时有一个安排,说实现一个带UI播放器的Jar给第三方调用,研究了一下,找到一份资料,但也不是很明白,最后打电话问人了,参考了一下百度的代码BaiduMapApi_Lib_Android_1.0.1.zip,终于有点眉目了,备查并分享一下。先贴一下步骤: [p=23, null, left]1, Export Library 1.1 Prepare source code 1.1.1 Create an Android project 1.1.2 Create source code, and fix all bug 1.1.3 remove AndroidManifest.xml 1.1.4 remove res/drawable/icon.png 1.2 Export library 1.2.1 On Package Explorer of Eclipse, right-click created project, and select Export 1.2.2 Select Jave -> JAR file, then press \”Next\” 1.2.3 Select resources to export, then press \”Next\” 1.2.4 Press \”Next\” 1.2.5 Press \”Finish\” Then the library is created. (Reference: Eclipse export jar files … 8b2fb2d0a2d3ad.html) [p=23, null, left] [p=23, null, left]2, Import Library You can use a third party JAR in your application by adding it to your Eclipse project as follows: In the Package Explorer panel, right-click on your project and select Properties. Select Java Build Path, then the tab Libraries. Press the Add External JARs… button and select the JAR file. Alternatively, if you want to include third party JARs with your package, create a new directory for them within your project and select Add Library… instead. [p=23, null, left]It is not necessary to put external JARs in the assets folder. 就不译了,也挺简单的,现在主要说一下我碰到的问题。我的想法很简单,自定义一个组件(关于自定组件,可以参考),这个自定义组件就是一个LinearLayout,背景设置为一个图片,图片路径为:res/drawable/bg.png,通过出this.setBackgroundResource(R.drawable.bg)实现,按照上述的步骤输出为jar后,给另一个app调用,发现图片根本无法输出,老是显示调用者的默认Icon. 反编了一下百度地图jar,发现它所有图片都是放在assets中,并不是放在res/drawable中,然后再查看了一下代码,其对图片的调用是通过AssetManager.open()方法来实现的,并非通过R.drawble来实现,修改后的代码如下:
图片不能通过R.drawable.xxxxx来调用,举一反三,是不是res/...下面的所有目录下的内容,比如layout文件、values下的strings、colors、dimens等都不能能过R.xx.xxxxx来调用。如果是这样的话,那真的很悲摧,因为播放器界面的按钮不少,而且还要不同分辨率的适配和国际化。代码量是可想而知了。贴源码吧。 这是jar包源代码,尚未删除AndroidManifest.xml和icon.png,在输出Jar包时把这两者删除后再输出. 调用jar包app源代码 |
转载地址:http://xfpci.baihongyu.com/