关于如何正确地在android项目中添加第三方jar包
????????????? 在android項目中添加第三方jar包雖然不是一個很復雜的問題,但是確實給很多開發者帶來了不小的困擾。我自己就曾經碰到過calss not found exception、error inflating class等本質都是第三方jar包未被真正識別但報錯指向其他問題的狀況,碰到這類問題時我們很容易會被這些指向不夠準確的報錯誤導,因此正確地添加第三方jar包就顯得很重要。網上關于這個問題的信息也比較雜亂,因此希望借此文章對這個問題進行明確的解釋和解答。
????????????? 首先要說的是為什么會出現這個問題?我們在以往開發java程序的過程中都已經習慣于通過add external archives來添加第三方的jar包,之所以不能在android項目中用同樣的方法引入第三方jar包的原因,下面是來自eclipse j2ee開發者之一Russ Bateman的解釋:
????? ? ? ? ? I'm an Eclipse JEE developer and have been in the habit for many years of adding third-party libraries via the "User Library" mechanism in Build Path. Of course, there are at least 3 ways to add a third-party library, the one I use is the most elegant, in my humble opinion.
This will not work, however, for Android, whose Dalvik "JVM" cannot handle an ordinary Java-compiled class, but must have it converted to a special format. This does not happen when you add a library in the way I'm wont to do it.
Instead, follow the (widely available) instructions for importing the third-party library, then adding it using Build Path (which makes it known to Eclipse for compilation purposes). Here is the step-by-step:
What you've done here accomplishes two things:
?
他的意思是android的dalvik虛擬機不能直接處理編譯過的java .class文件,因此直接添加第三方jar包需要經過一定的處理才能被android項目正常識別并使用。以下是他提供的步驟:
注:到這一步為止你就成功地把jar包添加到項目中(但是還沒有被android的虛擬機識別,因此如果這時你使用jar包中的類,編譯都無法通過)
? ? 4.右鍵點擊項目名,依次選擇Build Path -> Configure Build Path,選擇library選項卡。點擊右邊的add jars,選擇libs目錄下的jar包
???? 注:這一步就幫助android虛擬機來處理之前添加的jar包
理論上到這一步應該就可以正常使用了,但是如果到這一步你調試還會出錯的話可以嘗試右鍵點擊libs目錄,選擇build path –> configure as source folder ,基本上到這一步就可以正常使用第三方jar包,繼續你的android開發之旅了
?
?
轉載于:https://www.cnblogs.com/developerY/archive/2013/04/18/3027997.html
總結
以上是生活随笔為你收集整理的关于如何正确地在android项目中添加第三方jar包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flex 当鼠标悬停在DataGrid某
- 下一篇: 设置代码ios中根据文本设置label高