问题
Ok here is the thing, i put days into it, searching all the data base, so it's either i didn't search for the right words or there is no answer for it, hopefully “yet”.
Symptoms: -compiling the Android project DX compile all the jars every time the project is built - there is no reason to do it one time should be enough, any how it does it every time, every build can take long time instead of taking seconds it takes minutes.
The real problem:
-Adding several Jars cause to eclipse to crash -- now this is with these settings(heavy jars) -Xms256m -Xmx1024m -XX:+UseParallelGC -XX:PermSize=256M -XX:MaxPermSize=512M
(Which actually give better performance so if your eclipse compile slow you can add -XX:+UseParallelGC it will help)(I couldn't set it any highr than this, java heap size get to 750m when eclipse crash, gc doesn't work while DX compiling a jar, it tries between but the CPU is busy which doesn't let it, so at the time that it get to the forth-fifth jars which are 3MB each it has something like 600MB on the heap, this is why i used gcoverheadlimit)
Compiling each one or several of them together will compile it well(within the same android project), the problem is that i cannot find a way to compile them separately(then i will need to apks installed on the client device), and when i compile them together Eclipse crash.
I tried to compile them one by one, using cmd and DX, although it's built, when i build the project in eclispe it rebuild it, so it's like i did nothing, tried to convet the jars to DEX files too, didn't help.
I red many posts regarding to java heap size, this is not my case i even used gcoverheadlimit, too many jars cause it to crash, if someone has an idea/solution i will salute him/her ...
To summarize it all : I want to convert java regular jars to Dalvik format, then add it as a resource to an android project, so when the project is complied it won't need to convert the jars to Dalvik format because it's already done.
回答1:
Ok after a long research, i think that i know what is the problem, however i don't know how to workaround it yet.
Symptoms : compiling large jar and or several jars, cause to eclipse to crash, when it doesn't crash error message : trouble writing output: opcode == null , Conversion to Dalvik format failed with error 2 .
Cause : DX is unable to process more than it can contain in a string of 16bit, thus, there is no way to include(at least not in the mean time) more than the length of this string.
Result : unable to program complicated apps for Android as one APK.
Advice for android developers (i don't quite sure how you built it but): you can use arraylist for these strings, which each one of them writes itself in turn to classes.dex file, then, there will be no limit for writing code for one APK, and/or adding as many jars as the developer needs, this is an assumption, making it won't be easy but not too complicated too, definitly not to the ones who made Android, Agree ?
Workaround : sorry but i don't have a workaround yet, it took me some time to figure this problem, because the eclipse crashed and i didn't get the "opcode == null", error message so i thought that this is an eclipse issue, I will be glad to get second and third opinion on this one.
回答2:
Once the JARs have been built then add them as External JARs...
In Eclipse select the 'Project' menu -> 'Properties' then 'Java Build Path'. Select the 'Libraries' tab then click the 'Add External JARs...' button then browse and add them.
回答3:
I have had the same problem. There was lot of "Dx processing ..." messages in verbose output and it took about 3-4 minutes to build classes.dex(3.4 Mb). That was very slow and I felt a butthurt. Then I made a research and found out that the reason of the problem was in wrong eclipse JVM settings(eclipse.ini). BTW, I got this nasty config from some stackoverflow answer with more than 100 accepts.
Please make sure that eclipse.ini doesn't contains following line:
CompileThreshold=5
I removed this line and now compilation takes ~10 sec!!! Also, now I use ParallelGC instead of G1GC in eclipse config because it's more stable and it doesn't crash my IDE.
So, your main task is to revise your eclipse.ini and get rid of all suspicious and experimental parameters.
来源:https://stackoverflow.com/questions/6146655/compiling-jars-and-add-them-to-android-project