博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android的Apk编译和第三方jar包的使用

Posted on 2012-01-13 21:59  sinojelly  阅读(1671)  评论(1编辑  收藏  举报

[Android]命令行编译APK

(2010年04月18日) 发表于 ChinaUnix

手动编译你的apk,可以让资源目录不命名为res, 你能够命名任何你想要的名字。

你可以在以下目录发现ant脚本: <SDK_HOME>\platforms\android-1.5\templates\android-rules.xml

第一步: 产生R文件以及包资源
aapt  package -f -M ${manifest.file} -F ${packaged.resource.file} -I ${path.to.android-jar.library} -S ${android-resource-directory} [-m -J ${folder.to.output.the.R.java}]


第二步:编译java源文件以及R.java文件
use javac


第三步:将classes文件转化成Dalvik自节码
use dx.bat
dx.bat  –dex  –output=${output.dex.file}  ${compiled.classes.directory}  ${jar files..}

第四步:创建未签名的apk
use apkbuilder

apkbuilder  ${output.apk.file} -u -z  ${packagedresource.file} -f  ${dex.file}

or

apkbuilder  ${output.apk.file} -u -z  ${packagedresource.file} -f  ${dex.file}  -rf  ${source.dir}  -rj  ${libraries.dir}

-rf = resources required for compiled source files?
-rj = resources required for jar files

第六步: 产生一个key
use keytool

第七步骤: 对APK进行签名
use jarsigner

jarsigner  -keystore ${keystore} -storepass  ${keystore.password} -keypass ${keypass} -signedjar ${signed.apkfile} ${unsigned.apkfile} ${keyalias}


第八步: 发布
use adb
adb -d install -r ${signed.apk}

Inspecting your APK file:

aapt list -v latest.apk

Open questions:
1. Can you include more than one dex file in the apk?
2. Can you have dex file named other than classes.dex in the apk?
3. Does an apk have to have a packaged resource?

转自:

http://itnewsvendor.appspot.com/447063-android_%E5%91%BD%E4%BB%A4_%E7%BC%96%E8%AF%91.html

 

android 打包第三方jar包

Add a comment 1,362 views June 30th, 2011 Charlie

android项目开发时会用到第三方提供的jar包,通常情况下按照eclipse导入jar包的方法(选择项目,右键点击Build Path,Build Path–>Add Libraries–>User Library–>Next–>User Libraries–>New–>Add JARs,选择第三方的jar包),就可以将jar包导入到项目。

根据这样的打包方式时,eclipse导入jar包后,eclipse编译器也能识别,即项目不会因为缺少jar包而报错,但是运行android程序的时候却没有找到包而报错了。APK文件也很小,也就是jar包并没有打包到APK文件中。

在android项目中找到.classpath文件并打开,文件代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <classpath>  
  3. <classpathentry kind="src" path="src"/>  
  4. <classpathentry kind="src" path="gen"/>  
  5. <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>  
  6. <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/box2d"/>  
  7. <classpathentry kind="output" path="bin"/>  
  8. </classpath>  

发现eclipse导入包操作后,写入了这行代码

<classpathentry kind=”con” path=”org.eclipse.jdt.USER_LIBRARY/box2d”/>

这行代码应该是eclipse内部识别的,并不会打包到模拟器上,所以运行到模拟器上找不到相应的jar包。

为了在android内部能找到jar包,于是在android项目内新建一个lib文件夹,将jar包放在lib文件夹内。
并将上面的那行代码删除,添加一行代码:

<classpathentry kind=”lib” path=”lib/jbox2d-2.0.1-full.jar”/>

jbox2d-2.0.1-full.jar是lib文件下的jar包,可添加多个jar包。

即最终修改代码为:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <classpath>  
  3. <classpathentry kind="src" path="src"/>  
  4. <classpathentry kind="src" path="gen"/>  
  5. <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>  
  6. <classpathentry kind="lib" path="lib/jbox2d-2.0.1-full.jar"/>  
  7. <classpathentry kind="output" path="bin"/>  
  8. </classpath>  

最后刷新eclipse项目,会发现项目bin目录的APK文件大小增大,说明APK文件已经包含jar包文件,重新clean项目并运行即可。

转自:http://www.yoyong.com/archives/254