好吧,我承認我也開始學習那些標題黨了,從事android開發的都知道,使用as打包Jar是意見非常麻煩的事情,因爲as默認生成的是arr格式的架包,可最近我在開發時就非要打包Jar.
場景是這樣的,我需要寫一套SDK給Unity用,可Unity不認Arr格式的架包,於是我就得打包成Jar給他們使用,好了,有了問題,就得解決,操作步驟如下:
- 在需要打包的module的gradle中配置打包
// 刪除打包好的Jar
task clearJar(type: Delete) {
delete 'build/libs/phonelogsdk.jar'
}
// 打包Jar
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/libs/')
include('classes.jar')
rename ('classes.jar', 'phonelogsdk.jar')
}
makeJar.dependsOn(clearJar, build)
在終端執行生成JAR包
gradview makeJar
- 在module的build/libs/下就可以找到你的jar
- 備註:如果打包不成功報如下錯
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
只需要添加報錯提示代碼就行
android {
lintOptions {
abortOnError false
}
}
參考
- http://unclechen.github.io/2015/10/25/Gradle%E5%AE%9E%E8%B7%B5%E4%B9%8B%E6%89%93%E5%8C%85jar+Log%E5%BC%80%E5%85%B3%E8%87%AA%E5%8A%A8%E5%85%B3%E9%97%AD/
- http://yifeiyuan.me/2015/09/17/AndroidStudio%E5%AF%BC%E5%87%BAJar%E5%8C%85/
- https://www.aswifter.com/2015/06/16/android-studio-build-jar/
- http://chaosleong.github.io/blog/2015/08/02/android-studio-shi-yong-gradle-da-bao-jar/
- http://stackoverflow.com/questions/16746558/android-gradle-build-and-the-support-library/17153240#17153240
- http://stackoverflow.com/questions/16763090/how-to-export-library-to-jar-in-android-studio
- http://www.cnblogs.com/wuya/p/android-studio-gradle-export-jar-assets.html
- http://blog.csdn.net/hjq842382134/article/details/38538097
- http://blog.sina.com.cn/s/blog_93d133c601013hdm.html