Android项目构建过程分析

一、构建过程分析

我们知道,如果要在Android Studio后会编译整个项目并生成apk包,但是构建的过程中到底执行了什么task呢?我们在terminal中执行以下gradle assembleRelease看一下或者在点击最右侧的gradle任务栏,点击assembleRelease任务,如下图所示:


点击执行assembleRelease任务

在点击执行gradle assembleRelease任务后,可以看到依次执行了如下一系列任务,具体见下图:

20:56:59: Executing task 'assembleRelease'...

Executing tasks: [assembleRelease]

:app:preBuild UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:app:checkReleaseManifest
:app:preDebugBuild UP-TO-DATE
:app:prepareComAkexorcistRoundCornerProgressBar203Library
:app:prepareComAlipayEulerAndfix050Library
:app:prepareComAndroidDatabindingAdapters121Library
:app:prepareComAndroidDatabindingLibrary121Library
:app:prepareComAndroidSupportAnimatedVectorDrawable2340Library
:app:prepareComAndroidSupportAppcompatV72340Library
:app:prepareComAndroidSupportCardviewV72340Library
:app:prepareComAndroidSupportDesign2340Library
:app:prepareComAndroidSupportGridlayoutV72340Library
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareComAndroidSupportMultidex101Library
:app:prepareComAndroidSupportRecyclerviewV72340Library
:app:prepareComAndroidSupportSupportV42340Library
:app:prepareComAndroidSupportSupportVectorDrawable2340Library
:app:prepareComFacebookFbuiTextlayoutbuilderTextlayoutbuilder100Library
:app:prepareComFacebookFrescoAnimatedBase130Library
:app:prepareComFacebookFrescoAnimatedGif130Library
:app:prepareComFacebookFrescoAnimatedWebp130Library
:app:prepareComFacebookFrescoDrawee130Library
:app:prepareComFacebookFrescoFbcore130Library
:app:prepareComFacebookFrescoFresco130Library
:app:prepareComFacebookFrescoImagepipeline130Library
:app:prepareComFacebookFrescoImagepipelineBase130Library
:app:prepareComFacebookFrescoImagepipelineOkhttp3130Library
:app:prepareComFacebookFrescoWebpsupport130Library
:app:prepareComFacebookReactReactNative0503Library
:app:prepareComFacebookSoloaderSoloader010Library
:app:prepareComGithubMarkzhaiBlockcanaryAnalyzer150Library
:app:prepareComGithubMarkzhaiBlockcanaryAndroid150Library
:app:prepareComGoogleAndroidFlexbox025Library
:app:prepareComSensetimeCardscanner520Library
:app:prepareComSquareupLeakcanaryLeakcanaryAndroidNoOp13Library
:app:prepareIoReactivexRxjava2Rxandroid201Library
:app:prepareMeEverythingOverscrollDecorAndroid104Library
:app:prepareMeRelexCircleindicator122Library
:app:prepareMeRelexPhotodraweeview112Library
:app:prepareOrgWebkitAndroidJscR174650Library
:app:prepareRepluginHostLibDebugLibrary
:app:prepareReleaseDependencies
:app:compileReleaseRenderscript
:app:generateReleaseResValues
:app:generateReleaseResources
:app:mergeReleaseResources
:app:dataBindingProcessLayoutsRelease
:app:compileReleaseAidl
:app:generateReleaseBuildConfig
:app:rpGenerateReleaseHostConfig
:app:processReleaseResources
:app:generateReleaseSources
:app:dataBindingExportBuildInfoRelease
:app:incrementalReleaseJavaCompilationSafeguard
:app:compileReleaseJavaWithJavac
:app:compileReleaseNdk UP-TO-DATE
:app:compileReleaseSources
:app:lintVitalRelease
:app:mergeReleaseShaders
:app:compileReleaseShaders
:app:generateReleaseAssets
:app:mergeReleaseAssets
:app:rpGenerateReleaseBuiltinJson
:app:transformClassesWithJarMergingForRelease
:app:transformClassesWithMultidexlistForRelease
:app:transformClassesWithDexForRelease
:app:mergeReleaseJniLibFolders
:app:transformNative_libsWithMergeJniLibsForRelease
:app:transformNative_libsWithStripDebugSymbolForRelease
:app:processReleaseJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForRelease
:app:packageRelease
:app:assembleRelease

BUILD SUCCESSFUL

Total time: 4 mins 17.142 secs
21:01:20: Task execution finished 'assembleRelease'.

这个过程可以分为以下几个阶段:
1、预编译。如果主工程依赖了其他module,则被依赖的module也要进行编译。
2、整合资源。这个阶段所有的资源文件会被aapt(Android Asset Packaging Tool)打包。
3、编译。这个阶段源码被编译成字节码,而注解会在这个阶段被解释编译。源码被编译成字节码的过程如下图所示:

源码编译成字节码

4、字节码处理。这个阶段即上面中的即transform task处理阶段。
5、打包发布。这个阶段主工程的library会生成aar文件,而主工程会生成apk文件。

二、参考文章

  • https://blog.csdn.net/chongxue91/article/details/79565638
  • https://www.jianshu.com/p/4962634901fb
  • https://blog.csdn.net/wangwwish/article/details/77896453
  • https://www.jianshu.com/p/02dfa291d078
  • https://blog.csdn.net/owen_william/article/details/50988046

你可能感兴趣的:(Android项目构建过程分析)