日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

关于如何正确地在android项目中添加第三方jar包

發(fā)布時(shí)間:2024/4/15 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于如何正确地在android项目中添加第三方jar包 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

????????????? 在android項(xiàng)目中添加第三方j(luò)ar包雖然不是一個(gè)很復(fù)雜的問(wèn)題,但是確實(shí)給很多開(kāi)發(fā)者帶來(lái)了不小的困擾。我自己就曾經(jīng)碰到過(guò)calss not found exception、error inflating class等本質(zhì)都是第三方j(luò)ar包未被真正識(shí)別但報(bào)錯(cuò)指向其他問(wèn)題的狀況,碰到這類問(wèn)題時(shí)我們很容易會(huì)被這些指向不夠準(zhǔn)確的報(bào)錯(cuò)誤導(dǎo),因此正確地添加第三方j(luò)ar包就顯得很重要。網(wǎng)上關(guān)于這個(gè)問(wèn)題的信息也比較雜亂,因此希望借此文章對(duì)這個(gè)問(wèn)題進(jìn)行明確的解釋和解答。

????????????? 首先要說(shuō)的是為什么會(huì)出現(xiàn)這個(gè)問(wèn)題?我們?cè)谝酝_(kāi)發(fā)java程序的過(guò)程中都已經(jīng)習(xí)慣于通過(guò)add external archives來(lái)添加第三方的jar包,之所以不能在android項(xiàng)目中用同樣的方法引入第三方j(luò)ar包的原因,下面是來(lái)自eclipse j2ee開(kāi)發(fā)者之一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:

  • Download the library to your host development system.
  • Create a new folder, libs, in your Eclipse/Android project.
  • Right-click libs and choose Import -> General -> File System, then Next, Browse in the filesystem to find the library's parent directory (i.e.: where you downloaded it to).
  • Click OK, then click the directory name (not the checkbox) in the left pane, then check the relevant JAR in the right pane. This puts the library into your project (physically).
  • Right-click on your project, choose Build Path -> Configure Build Path, then click the Libraries tab, then Add JARs..., navigate to your new JAR in the libs directory and add it. (This, incidentally, is the moment at which your new JAR is converted for use on Android.)
  • What you've done here accomplishes two things:

  • Includes a Dalvik-converted JAR in your Android project.
  • Makes Java definitions available to Eclipse in order to find the third-party classes when developing (that is, compiling) your project's source code.
  • ?

        他的意思是android的dalvik虛擬機(jī)不能直接處理編譯過(guò)的java .class文件,因此直接添加第三方j(luò)ar包需要經(jīng)過(guò)一定的處理才能被android項(xiàng)目正常識(shí)別并使用。以下是他提供的步驟:

  • 下載第三方j(luò)ar包
  • 在android項(xiàng)目下創(chuàng)建一個(gè)libs目錄(就我理解及測(cè)試的經(jīng)驗(yàn),名稱并不固定,你完全可以取其他名稱)
  • 在eclipse中右鍵點(diǎn)擊libs目錄,依次選擇Import -> General -> File System,選中jar包所在目錄, 然后選中這個(gè)目錄下的jar包
  • 注:到這一步為止你就成功地把jar包添加到項(xiàng)目中(但是還沒(méi)有被android的虛擬機(jī)識(shí)別,因此如果這時(shí)你使用jar包中的類,編譯都無(wú)法通過(guò))

    ? ? 4.右鍵點(diǎn)擊項(xiàng)目名,依次選擇Build Path -> Configure Build Path,選擇library選項(xiàng)卡。點(diǎn)擊右邊的add jars,選擇libs目錄下的jar包

    ???? 注:這一步就幫助android虛擬機(jī)來(lái)處理之前添加的jar包

      理論上到這一步應(yīng)該就可以正常使用了,但是如果到這一步你調(diào)試還會(huì)出錯(cuò)的話可以嘗試右鍵點(diǎn)擊libs目錄,選擇build path –> configure as source folder ,基本上到這一步就可以正常使用第三方j(luò)ar包,繼續(xù)你的android開(kāi)發(fā)之旅了

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/developerY/archive/2013/04/18/3027997.html

    總結(jié)

    以上是生活随笔為你收集整理的关于如何正确地在android项目中添加第三方jar包的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。