記錄一下在生成dex文件時(shí)遇到三個(gè)問(wèn)題及解決方法
1.創(chuàng)建臨時(shí)目錄
首先在project目錄下創(chuàng)建一個(gè)臨時(shí)目錄,然后將項(xiàng)目修復(fù)完bug的代碼及目錄結(jié)構(gòu)拷貝一份出來(lái)放到臨時(shí)目錄中,然后刪除未做修改的代碼文件。
2.編譯class
在臨時(shí)目錄里打開Terminal 運(yùn)行javac Xxx.java將java文件編譯為Xxx.class文件。
3.生成dex
使用dx.bat 生成dex文件,查看工程的buildToolsVersion版本,然后在sdk的build-tools目錄中找到對(duì)應(yīng)的版本,在Terminal中輸入 :cd 對(duì)應(yīng)的build-tools目錄(例如:cd D:\Android\SDK\build-tools\29.0.2),然后運(yùn)行dx --dex --output upgrade.dex com/example/hotfix/Fixed.class
PS: upgrade.dex為生成dex路徑可自行修改,com/.../Fixed.class為要編譯的class文件路徑,根據(jù)實(shí)際情況修改 ,回車。
4.出現(xiàn)的問(wèn)題
輸入:dx --dex --output=D:\Android\workspace\PluginDemo\to-be-fixed\com\example\hotfix\fix.dex com\example\hotfix\FixedBean.class
1.問(wèn)題一
java.lang.UnsupportedClassVersionError: org/apache/lucene/store/Directory : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Exception in thread "main"
問(wèn)題原因:是因?yàn)殡娔X上有兩個(gè)版本的jdk(自己裝的一個(gè),AS帶的一個(gè)),編譯.class是jdk版本與生成dex時(shí)需要的jdk版本不一致導(dǎo)致的,可在cmd窗口查看對(duì)應(yīng)的jdk版本 java -version 、javac -version。
解決辦法:修改環(huán)境變量中path的jdk的路徑。
2.問(wèn)題二
輸入:dx --dex --output=D:\Android\workspace\PluginDemo\to-be-fixed\com\example\hotfix\fix.dex to-be-fixed\com\example\hotfix\FixedBean.class
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: com\example\hotfix\FixedBean.class: file not found
at com.android.dex.util.FileUtils.readFile(FileUtils.java:51)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:168)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:143)
at com.android.dx.command.dexer.Main.processOne(Main.java:678)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:575)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:310)
at com.android.dx.command.dexer.Main.runDx(Main.java:288)
at com.android.dx.command.dexer.Main.main(Main.java:244)
at com.android.dx.command.Main.main(Main.java:95)
1 error; aborting
問(wèn)題原因:class輸入路徑導(dǎo)致。
解決辦法:將to-be-fixed\com\example\hotfix\FixdBean.class替換為com\example\hotfix\FixdBean.class
3.問(wèn)題三
輸入:dx --dex --output=fix.dex com\example\hotfix\FixdBean.class
PARSE ERROR:
class name (com/example/hotfix/FixedBean) does not match path (FixdBean.class)
...while parsing FixedBean.class
1 error; aborting
問(wèn)題原因:還是因?yàn)閏lass路徑問(wèn)題導(dǎo)致的。
解決辦法:把com及子目錄拷貝一份到build-tools對(duì)應(yīng)的版本目錄中,也就是和dx.bat同及目錄下,再次運(yùn)行就可以成功生成.dex文件了。