Android编译慢(使用Eclipse)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2883635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:43:33  来源:igfitidea点击:

Android compilation is slow (using Eclipse)

androidperformancecompilation

提问by alex2k8

When I change any .java file and build, the compilation takes 16 seconds. I don't understand why it should be so slow!?

当我更改任何 .java 文件并构建时,编译需要 16 秒。我不明白为什么它应该这么慢!?

I enabled verbose output for Andoroid.

我为 Andoroid 启用了详细输出。

Window > Preferences > Android > Build output > Verbose.

The result output (Console > Android) is:

结果输出(控制台 > Android)是:

[19:46:10] Refreshing resource folders.
[19:46:10] Starting incremental Pre Compiler: Checking resource changes.
[19:46:10] Nothing to pre compile!
[19:46:10] Starting incremental Package build: Checking resource changes.
[19:46:10] ignored resource ...\bin\.\classes.dex
[19:46:10] processing ...\A.class
[19:46:10] processing ...\B.class
    ...
[19:46:21] processing com/google/inject/util/Providers.class...
[19:46:21] processing com/google/inject/util/Types.class...
[19:46:24] Using default debug key to sign package
[19:46:24] Packaging ...\bin\resources.ap_
[19:46:24] Packaging classes.dex
    ...
[19:46:25] Packaging ...\annotations.jar
[19:46:25] Build Success!
[19:46:25] Refreshing resource folders.
[19:46:25] Starting incremental Pre Compiler: Checking resource changes.
[19:46:26] Nothing to pre compile!

The "processing" of .class files took 14 seconds. And it "processed" all files, even from all .jar files linked. I think some thing wrong goes here, as only one .java file was changed.

.class 文件的“处理”耗时 14 秒。它“处理”了所有文件,甚至来自所有链接的 .jar 文件。我认为这里出了点问题,因为只更改了一个 .java 文件。

What can I do to improve the compilation speed?

我可以做些什么来提高编译速度?

回答by lencinhaus

I think there's a misunderstanding here. As you say, only the modified classes are recompiled (by Eclipse in a matter of milliseconds); after that, however, the ADT plugin takes every compiled class and translates it into Dalvik's bytecode format via the dx tool. The output of this process is a single file, classes.dex, which contains all the classes in your application, including the ones coming from referenced libraries. This last "translation" step is the one that takes longer because it's really poorly optimized: the ADT plugin doesn't cache anything, it just retranslates every class at every build (and it's painfully slow). For medium to big projects this gets really frustrating... I hope Google will improve that in a future ADT/SDK release.

我认为这里有一个误解。如您所说,仅重新编译修改后的类(由 Eclipse 在几毫秒内重新编译);然而,在那之后,ADT 插件获取每个已编译的类并通过 dx 工具将其转换为 Dalvik 的字节码格式。此过程的输出是一个文件 classes.dex,其中包含应用程序中的所有类,包括来自引用库的类。最后一个“翻译”步骤需要更长的时间,因为它的优化确实很差:ADT 插件不缓存任何东西,它只是在每次构建时重新翻译每个类(而且速度非常慢)。对于大中型项目,这真的很令人沮丧……我希望 Google 在未来的 ADT/SDK 版本中改进这一点。

回答by oae

One other workaround is:

另一种解决方法是:

  • disable the Android Package Builder (right-click on project#Properties#Builders)
  • use ant for build and deployment of apk
  • 禁用 Android Package Builder(右键单击 project#Properties#Builders)
  • 使用ant构建和部署apk

see android-workaround-for-slow-building-workspace-problem-in-eclipsefor details

看到Android的解决方法换建设缓慢工作空间问题,在月食的细节

回答by Xavier Ducrohet

ADT 21 pre-dex all libraries. This means regular compilation only re-dex the output of your project (faster) and then merges the result with the pre-dexed libraries (pretty quick).

ADT 21 pre-dex 所有库。这意味着常规编译只会重新 dex 项目的输出(更快),然后将结果与 pre-dexed 库合并(非常快)。

回答by Vysakh Prem

As alex2k8 mentioned I enabled verbose output. The culprit was AdMob jar file.

正如 alex2k8 提到的,我启用了详细输出。罪魁祸首是 AdMob jar 文件。

I'm using ADT 22.0. My application includes AdMob and during the running or debugging step, the class files in admob jar are converted to dex files which was really slow.

我正在使用 ADT 22.0。我的应用程序包含 AdMob,在运行或调试步骤期间,admob jar 中的类文件被转换为 dex 文件,这非常慢。

I removed the admob code and the jar from my project temporarily and the build process is normal again.

我暂时从我的项目中删除了admob代码和jar,构建过程再次正常。

回答by Marco HC

Every time you save, Eclipse package and dexing all files. But this is not necessary because you don't need to deploy your app in apk each time you save.

每次保存时,Eclipse 都会对所有文件进行打包和 dexing。但这不是必需的,因为您不需要每次保存时都在 apk 中部署您的应用程序。

Anyway, the key of this problem is to uncheck the option:

无论如何,这个问题的关键是取消选中该选项:

"Skip packaging and dexing until export or launch. (Speeds up automatic builds on file save.)"

“在导出或启动之前跳过打包和 dexing。(加快文件保存时的自动构建。)”

Inside of "Window --> Preferences --> Android --> Build"

“窗口 --> 首选项 --> Android --> 构建”内部

This will fix your problem.

这将解决您的问题。

Cheers

干杯

回答by iceheart

Increasing the memory available for Eclipse seems to help a lot. Try launching it like:

增加可用于 Eclipse 的内存似乎有很大帮助。尝试像这样启动它:

eclipse -vmargs -Xms1024m -Xmx2048m