Android和Maven:加快项目的编译和部署

时间:2021-02-05 20:23:19

Is there a way to speed up the compilation and deployment to device of the android project using maven?

有没有一种方法可以加速使用maven对android项目设备的编译和部署?

I tested build time of a blank android project (created from command line using 'android create project') in IntelliJ Idea - it took me 4 seconds from pressing the 'run' button until launching the app on device. Then I added maven support to it - now it takes almost 7 seconds.

我在IntelliJ Idea中测试了一个空白的android项目(用‘android create project’从命令行创建的)的构建时间——我用了4秒钟的时间按下‘run’按钮,直到在设备上启动应用程序。然后我为它添加了maven支持—现在几乎需要7秒。

For bigger projects it takes even more time. For example, blank project with ActionBarSherlock dependency added takes about 25-30 seconds to compile, deploy and run.

对于更大的项目,它需要更多的时间。例如,添加了ActionBarSherlock依赖项的blank project需要25-30秒来编译、部署和运行。

Is there a way to speed up this process?

有没有办法加快这个过程?

I would like to hear answers from Square developers (especially Jake Wharton) :) how long does it take your android projects to compile?

我想听听Square开发者(尤其是杰克·沃顿)的回答:)你的android项目需要多长时间才能编译?

6 个解决方案

#1


4  

Couple things I did which gave me a little improvement:

我做的几件事让我有了一点进步:

  • Turning off dex optimization for debug builds
  • 关闭用于调试构建的dex优化
  • Turning on library pre-dexing (which slows down the first build but speed up the followings)
  • 打开库预除(它减慢了第一个构建,但是加快了后续的构建)

Configuration is something like:

配置是:

<plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <inherited>true</inherited>
    <configuration>
        <!-- All your current configuration -->
        <dexOptimize>false</dexOptimize>
         <dex>
            <preDex>true</preDex>
            <preDexLibLocation>/tmp/predexedLibs</preDexLibLocation>
        </dex>
    </configuration>
</plugin>

BTW I suspect there are some problems with the incremental compiler and every time all the code is ricompiled from scratch.

顺便说一句,我怀疑增量编译器有一些问题,每次所有的代码都是从头编译的。

#2


3  

Maven 3 has multi-threaded builds. I'd at least try it out. Given you're already at 20 secs, it might not do much, but it certainly sped up our larger maven builds

Maven 3有多线程的构建。我至少要试一试。考虑到您已经达到了20秒,这可能不会有太大的作用,但是它确实加速了我们更大的maven构建

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

Parallel builds in Maven 3

Maven 3中的并行构建

#3


2  

25-30 Seconds to make, deploy and run an App? Lucky guy! I'm working on a Windows Laptop...

用25-30秒来制作、部署和运行应用程序?幸运的家伙!我正在用Windows笔记本电脑……

Here're the few things I do to speed up my development:

以下是我为加速发展所做的几件事:

  • I made some good progress by putting the emulators image and SD-Drive on an SSD.
  • 我在SSD上安装了模拟器映像和SD-Drive,取得了一些很好的进展。
  • Running the emulator from a snapshot starts it faster.
  • 从快照运行仿真器可以更快地启动它。
  • When it's possible I run my code on an actual device instead of an emulator - depending on the actual device this is much faster (2x on my Sony Xperia) even with the virtualized emulator. Of course the downside of this is, that you cannot test different Android versions and device profiles.
  • 如果可能的话,我将代码运行在实际的设备上,而不是仿真器上——这取决于实际的设备,这要快得多(在我的Sony Xperia上是2x),即使使用虚拟仿真器也是如此。当然,这样做的缺点是不能测试不同的Android版本和设备配置文件。

But still with all the little changes in the emulator, deployment goes thru the TCP/IP Stack which is the bottleneck for it.

但是由于仿真器中所有的小更改,部署仍然通过TCP/IP栈进行,这是它的瓶颈。

PS: I'm not Jake Wharton and dont know him. But if he can change something on the deployment he should do so :-)

PS:我不是杰克·沃顿,也不认识他。但如果他能改变部署上的一些东西,他应该这样做:

#4


1  

You can try setting higher -Xms and -Xmx options for MAVEN_OPTS.

您可以尝试为MAVEN_OPTS设置更高的-Xms和-Xmx选项。

Also, ensure maven in idea is not configured to download/syncronizing with repository for jars every time.

另外,确保每次不会将idea中的maven配置为下载/同步到jar的存储库中。

You can also through some decent hardware upgrade ;-)

你也可以通过一些不错的硬件升级;

#5


1  

The answer is somehow strange, but Jake Wharton advised to move to gradle-builds which are truly incremental. Also using Genymotion instead of SDK emulators and USB-devices gave me much needed decrease in deploy time.

答案有些奇怪,但杰克·沃顿建议把注意力转移到真正的增量式建筑上。此外,使用Genymotion而不是SDK模拟器和usb设备也减少了部署时间。

So now my build time decreased from ~1m to ~10-20sec.

所以现在我的构建时间从1米减少到10-20秒。

#6


0  

You can try to use tools that upload only delta changes to the emulator: only changed classes will be pushed to the device, should save some time. You can try the tool: http://www.instareloader.com/download/ use "inirwetrust" as the password

您可以尝试使用仅上传delta更改到模拟器的工具:只有更改了的类才会被推送到设备上,应该节省一些时间。您可以尝试这个工具:http://www.instareloader.com/download/使用“inirwetrust”作为密码。

#1


4  

Couple things I did which gave me a little improvement:

我做的几件事让我有了一点进步:

  • Turning off dex optimization for debug builds
  • 关闭用于调试构建的dex优化
  • Turning on library pre-dexing (which slows down the first build but speed up the followings)
  • 打开库预除(它减慢了第一个构建,但是加快了后续的构建)

Configuration is something like:

配置是:

<plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <inherited>true</inherited>
    <configuration>
        <!-- All your current configuration -->
        <dexOptimize>false</dexOptimize>
         <dex>
            <preDex>true</preDex>
            <preDexLibLocation>/tmp/predexedLibs</preDexLibLocation>
        </dex>
    </configuration>
</plugin>

BTW I suspect there are some problems with the incremental compiler and every time all the code is ricompiled from scratch.

顺便说一句,我怀疑增量编译器有一些问题,每次所有的代码都是从头编译的。

#2


3  

Maven 3 has multi-threaded builds. I'd at least try it out. Given you're already at 20 secs, it might not do much, but it certainly sped up our larger maven builds

Maven 3有多线程的构建。我至少要试一试。考虑到您已经达到了20秒,这可能不会有太大的作用,但是它确实加速了我们更大的maven构建

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

Parallel builds in Maven 3

Maven 3中的并行构建

#3


2  

25-30 Seconds to make, deploy and run an App? Lucky guy! I'm working on a Windows Laptop...

用25-30秒来制作、部署和运行应用程序?幸运的家伙!我正在用Windows笔记本电脑……

Here're the few things I do to speed up my development:

以下是我为加速发展所做的几件事:

  • I made some good progress by putting the emulators image and SD-Drive on an SSD.
  • 我在SSD上安装了模拟器映像和SD-Drive,取得了一些很好的进展。
  • Running the emulator from a snapshot starts it faster.
  • 从快照运行仿真器可以更快地启动它。
  • When it's possible I run my code on an actual device instead of an emulator - depending on the actual device this is much faster (2x on my Sony Xperia) even with the virtualized emulator. Of course the downside of this is, that you cannot test different Android versions and device profiles.
  • 如果可能的话,我将代码运行在实际的设备上,而不是仿真器上——这取决于实际的设备,这要快得多(在我的Sony Xperia上是2x),即使使用虚拟仿真器也是如此。当然,这样做的缺点是不能测试不同的Android版本和设备配置文件。

But still with all the little changes in the emulator, deployment goes thru the TCP/IP Stack which is the bottleneck for it.

但是由于仿真器中所有的小更改,部署仍然通过TCP/IP栈进行,这是它的瓶颈。

PS: I'm not Jake Wharton and dont know him. But if he can change something on the deployment he should do so :-)

PS:我不是杰克·沃顿,也不认识他。但如果他能改变部署上的一些东西,他应该这样做:

#4


1  

You can try setting higher -Xms and -Xmx options for MAVEN_OPTS.

您可以尝试为MAVEN_OPTS设置更高的-Xms和-Xmx选项。

Also, ensure maven in idea is not configured to download/syncronizing with repository for jars every time.

另外,确保每次不会将idea中的maven配置为下载/同步到jar的存储库中。

You can also through some decent hardware upgrade ;-)

你也可以通过一些不错的硬件升级;

#5


1  

The answer is somehow strange, but Jake Wharton advised to move to gradle-builds which are truly incremental. Also using Genymotion instead of SDK emulators and USB-devices gave me much needed decrease in deploy time.

答案有些奇怪,但杰克·沃顿建议把注意力转移到真正的增量式建筑上。此外,使用Genymotion而不是SDK模拟器和usb设备也减少了部署时间。

So now my build time decreased from ~1m to ~10-20sec.

所以现在我的构建时间从1米减少到10-20秒。

#6


0  

You can try to use tools that upload only delta changes to the emulator: only changed classes will be pushed to the device, should save some time. You can try the tool: http://www.instareloader.com/download/ use "inirwetrust" as the password

您可以尝试使用仅上传delta更改到模拟器的工具:只有更改了的类才会被推送到设备上,应该节省一些时间。您可以尝试这个工具:http://www.instareloader.com/download/使用“inirwetrust”作为密码。