• In software development, the most time consuming process is not writing code, but compiling and debugging code
  • Software trial and error time
  1. Basic Android compilation process
  • Four steps to build:
    • Code compilation: Compile source code, R files, aiDL-generated files, etc. into.class files
    • Code synthesis: Use the dex tool to generate executable. Dex files from. Class files and third-party library files that the project depends on. If MultiDex is used, multiple dex files will be generated.
    • Resource packaging: The apkBuilder tool packages. Dex files, apt compiled resource files, and resource files in the tripartite library to generate signature aligned APK files
    • Sign and align: Use Jarsigner and Zipalign to sign and align files to produce the final APK file
  • Gradle builds an App Module Debug task chain
Gradle Clean assembleDebug -x lint Check -- StackTrace :app:clean Delete the build folder under module :app:preDebugBuild // Debug Version precompile :app:checkDebugManifest //AndroidManifest check : app: prepareDebugDependencies / / check the debug version of the dependence: app: compileDebugAidl / / compile the debug version of the aidl file: app: compileDebugRenderscript / / compile Renderscript file: app: generateDebugBuildConfig / / generated/source folder, Generate buildConfig folder :app:generateDebugAssets // Generate Assets file to asset folder :app:mergeDebugAssets under generated // Generate assets folder under intermediates, Assets from the other module/aar file copy come over: app: generateDebugResValues value file: / / generates res app: generateDebugResources / / generate Resources files App: mergeDebugResources / / merge (merger) resource files: app: processDebugManifest / / after the merge of the Manifest file in intermediates/manifests folder : app: processDebugResources / / processing resource files, generate R.t xt file, Also generate the corresponding multidex folder: app: generateDebugSources / / synthetic resource files in the generated folder to generate the corresponding R.j ava file: app: compileDebugJavaWithJavac / / using javac generates Java file: app: compileDebugNdk / / the NDK compiler: app: compileDebugSources / / compile resource files: app: transformClassesWithDexForDebug / / will be converted into a class file. Dex file: app: mergeDebugJniLibFolders / / merge jni. (so) files: the app: transformNative_libsWithMergeJniLibsForDebug / / replace files: jni app: processDebugJavaRes / / Java resources: app: transformResourcesWithMergeJavaResForDebug / / convert Java resource files App: validateSigningDebug / / verification signature: app: packageDebug / / packaging: app: assembleDebug / / apk compiledCopy the code
  1. InstantRun

AndroidStudio 2.0 introduced InstantRun, which stands for instant compilation and reduces app deployment and build time during compilation and development

  • Gradle2.0 or above, minSdkVersion15 or above
  • Build process: code changes — — > build > build – > application deployment — — > app restart > activity restart – > to complete the change to change
  • Implement just-in-time mechanism: after modifying code, incremental build (generate incremental DEX), and then choose to perform hot update, warm update or cold update by judging the complexity of the updated resource;
    • Hot deployment: To take effect, you do not need to restart the app or activity
    • Warm deployment: You can’t see updates until you restart the activity
    • Cold deployment: The app needs to be restarted, but not re-installed
  • Operating principle:
    1. Integrate the manifest of the project with manifest-merger, compile the synthesized Androidmanifest. XML file and RES resources into incremental APK through aAPT tool;

    2. After code modification, Java files are compiled into class files through Javac, and then packaged into dex files, which are also placed in incremental APK;

  1. Optimize Gradle compilation
  • Properties configuration optimization
# Enable parallel compilation, quite using multithreading, Android. EmableBuildCache =true # Enable build cache, Gradle 3.5 is a new caching mechanism that caches the output of all tasks, # unlike buildCache which only caches the external libs of dex, it can reuse the buildCache at any time, Build initialization requires many tasks, such as starting the Java VIRTUAL machine, loading the virtual machine environment, loading the class file, etc. # ensure that JVM build commands compile APK in the daemon. Daemon can greatly reduce the loading time of JVM and classes. The biggest advantage of daemon is that it helps to speed up the construction of multi-moudle projects. That just compile related Module org. Gradle. Configureondemand = true # configuration size of the virtual machine at compile time, increase the memory space of compile-time AndroidStudio use # - Xmx2048m: Specifies that the maximum heap memory that the JVM is allowed to allocate is 2048MB, which will be allocated on demand. # -xx :MaxPermSize= 512M: Specifies that the maximum amount of non-heap memory that can be allocated by the JVM is 512MB. org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8Copy the code
  • Task Task filtering

Selectively remove Gradle Task tasks that do not need to be run

tasks.whenTaskAdded(new Action<Task>() { @Override void execute(Task task) { if (task. Name. The contains (" lint ") / / no scan potential bugs can use the | | task. The name = = "clean" | | task. The name, the contains (" Aidl ") / / project used in the Aidl do not give up to the task | | task. Name. The contains (" mockableAndroidJar ") / / no test can be shut off when the | | task. The name, the contains (" UnitTest ") / / under test can be shut off | | Task. The name. The contains (" AndroidTest ") / / no test can be shut off when the | | task. The name, the contains (" the Ndk ") | | task. The name, the contains (" Jni ") / / in less than the Ndk and Jni when closed ) { task.enabled = false } } })Copy the code
  1. speed
  • Incremental compilation freeline:

Developed by the Android team of Ant Jubao, it can make full use of cached files, quickly compile and deploy code changes to the device in a few seconds, effectively reducing the amount of recompile and install time in daily development (good solution, no more words, is already cool).

Componentized distribution

The serial optimization rule is to reduce the duplication of operations — caching, and the parallel optimization rule to reduce the duplication of wheels is to separate the business coupling degree — decoupling, and the distribution of attention to smaller modules means deeper decoupling, more detailed splitting of the granularity of things, and the expansion of components

I am Jinyang, if you want to advance and learn more dry goods, welcome to pay attention to the public number “jinyang said,” receive my latest article