参考自:http://blog.csdn.net/chenkai19920410/article/details/60589100?utm_source=tuicool&utm_medium=referral
参考文章基于Mac,命令行一些地址有微小差异,下面是我在ubuntu下的操作流程
首先New一个HelloWorld工程,复杂的项目可能有其他问题.熟悉这一流程在HelloWorld工程是最方便的
(自定义保存编译结果的目标目录)
root@xwpengCP:/home/xwpeng/AndroidStudioProjects/HandPackApk# mkdir -p ./app/src/main/{gen,build,out}
(aapt"Android Asset Packaging Tool"工具生成R.java)
root@xwpengCP:~/Android/Sdk/build-tools/25.0.0# ./aapt package -f -M /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/AndroidManifest.xml -I /root/Android/Sdk/platforms/android-25/android.jar -S /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/res/ -J /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/gen/ -m
styles.xml需要改一下:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Customize your theme here. -->
<!-- <item name="colorPrimary">@android:@colorcolorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>-->
</style>
</resources>
生成的R.java在我们自定义的gen目录下
(编译java文件)
javac -source 1.7 -target 1.7 -encoding UTF-8 -bootclasspath /root/Android//Sdk/platforms/android-25/android.jar -d /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/build/ /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/java/android/xwpeng/handpackapk/*.java /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/gen/android/xwpeng/handpackapk/*.java
生成的class文件在builder目录
(dx将class文件打包成.dex)
root@xwpengCP:~/Android/Sdk/build-tools/25.0.0# ./dx --dex --output=/home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/build/classes.dex /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/build/
(将资源文件打包成res.apk)
./aapt package -f -M /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/AndroidManifest.xml -I /root/Android/Sdk/platforms/android-25/android.jar -S /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/res/ -F /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/out/res.apk
生成的res.apk在out目录
(生成apkbuilder工具)
root@xwpengCP:~/Android/Sdk/tools# cat android | sed -e 's/com.android.sdkmanager.Main/com.android.sdklib.build.ApkBuilderMain/g' > apkbuilder
(改变权限)
root@xwpengCP:~/Android/Sdk/tools# chmod +x apkbuilder
(用apkbuilder将dex打进res.apk生成app.apk)
root@xwpengCP:~/Android/Sdk/tools# ./apkbuilder /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/out/app.apk -v -u -z /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/out/res.apk -f /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/build/classes.dex
(签名)
默认的debug签名文件路径/root/.android/debug.keystore
root@xwpengCP:~/.android# jarsigner -verbose -keystore /root/.android/debug.keystore -storepass android -keypass android /home/xwpeng/AndroidStudioProjects/HandPackApk/app/src/main/out/app.apk androiddebugkey