Configure Your Construct

From Hikvision Guides
Jump to: navigation, search

The Android construct system compiles app assets and source code, and packages them into APKs that you can test, deploy, signal, and distribute. Android Studio uses Gradle, a complicated construct toolkit, to automate and handle the construct process, whereas allowing you to outline flexible custom construct configurations. Every construct configuration can outline its own set of code and resources, while reusing the parts common to all variations of your app. The Android plugin for Gradle works with the build toolkit to supply processes and configurable settings which might be specific to constructing and testing Android functions.




Gradle and the Android plugin run independent of Android Studio. This means you can build your Android apps from inside Android Studio, the command line on your machine, or on machines the place Android Studio is just not installed (akin to continuous integration servers). If you are not using Android Studio, you can learn the way to build and run your app from the command line. The output of the construct is identical whether or not you are building a undertaking from the command line, on a distant machine, or utilizing Android Studio.



Word: Because Gradle and the Android plugin run independently from Android Studio, it is advisable to update the construct tools separately. Learn the discharge notes to discover ways to replace Gradle and the Android plugin.



The flexibility of the Android build system allows you to carry out custom construct configurations with out modifying your app's core source files. This part helps you understand how the Android build system works, and the way it might probably provide help to customise and automate a number of construct configurations. For those who simply want to learn more about deploying your app, see Constructing and Running from Android Studio. To start creating custom build configurations straight away using Android Studio, see Configuring Construct Variants.



The build process



The construct course of involves many tools and processes that convert your challenge into an Android Application Bundle (APK). The construct course of may be very versatile, so it is helpful to grasp a few of what is happening below the hood.



Determine 1. The construct technique of a typical Android app module.



The build process for a typical Android app module, as proven in figure 1, follows these common steps:



1. The compilers convert your source code into DEX (Dalvik Executable) files, which embrace the bytecode that runs on Android devices, and every thing else into compiled resources.2. The APK Packager combines the DEX recordsdata and compiled resources right into a single APK. Before your app may be installed and deployed onto an Android device, nonetheless, the APK have to be signed.3. The APK Packager indicators your APK using both the debug or launch keystore: 1. If you're constructing a debug model of your app, that is, an app you intend just for testing and profiling, the packager signs your app with the debug keystore. Android Studio routinely configures new tasks with a debug keystore.2. If you're building a release model of your app that you intend to launch externally, the packager indicators your app with the release keystore. To create a release keystore, examine signing your app in Android Studio.



At the end of the construct course of, you've gotten both a debug APK or release APK of your app that you need to use to deploy, check, or launch to exterior customers.



Customized construct configurations



Gradle and the Android plugin help you configure the following features of your build:



Construct configuration files



Creating customized construct configurations requires you to make changes to one or more construct configuration information, or construct.gradle files. These plain text files use Area Specific Language (DSL) to describe and manipulate the construct logic utilizing Groovy, which is a dynamic language for the Java Digital Machine (JVM). You don’t must know Groovy to start out configuring your build as a result of the Android plugin for Gradle introduces most of the DSL components you want. To study extra concerning the Android plugin DSL, learn the DSL reference documentation.



When starting a new mission, Android Studio automatically creates some of these information for you, as proven in determine 2, and populates them based mostly on wise defaults.



Determine 2. The default undertaking structure for an Android app module.



There are a number of Gradle construct configuration files which are a part of the standard venture structure for an Android app. Before you can start configuring your construct, it is important to know the scope and function of each of those information, and the basic DSL elements they should outline.



The Gradle settings file



The settings.gradle file, located in the root mission directory, tells Gradle which modules it ought to include when building your app. For most tasks, the file is easy and solely contains the next:



Nevertheless, multi-module projects have to specify each module that ought to go into the ultimate construct.



The top-degree build file



The highest-level construct.gradle file, situated in the root challenge listing, defines construct configurations that apply to all modules in your challenge. By default, the top-level build file makes use of the buildscript block to outline the Gradle repositories and dependencies which are widespread to all modules within the mission. contextual backlinks describes the default settings and DSL components you will discover in the highest-level construct.gradle after creating a brand new mission.



Configure venture-broad properties



For Android initiatives that include a number of modules, it may be helpful to outline sure properties at the venture stage and share them throughout all the modules. You may do this by including further properties to the ext block in the highest-level build.gradle file.



To access these properties from a module in the same challenge, use the following syntax in the module's build.gradle file (you possibly can learn extra about this file within the section beneath).



Authority backlinks : Though Gradle lets you outline challenge-vast properties at the module degree, you should avoid doing so because it causes the modules that share these properties to be coupled. Module coupling makes it harder to later export a module as a stand-alone undertaking and successfully prevents Gradle from utilizing parallel mission execution to speed up multi-module builds.



The module-stage build file



The module-degree construct.gradle file, situated in every venture/module/ listing, permits you to configure build settings for the precise module it's situated in. Configuring these construct settings allows you to supply customized packaging options, resembling further construct sorts and product flavors, and override settings in the main/ app manifest or top-degree construct.gradle file.



This sample Android app module construct.gradle file outlines some of the basic DSL components and settings that it is best to know.



Gradle properties files



Gradle additionally consists of two properties files, positioned in your root project directory, that you can use to specify settings for the Gradle build toolkit itself:



ndk.dir - Path to the NDK. This property has been deprecated. Any downloaded variations of the NDK can be put in within the ndk directory inside the Android SDK listing.sdk.dir - Path to the SDK.cmake.dir - Path to CMake.ndk.symlinkdir - in Android Studio 3.5+, creates a symlink to the NDK that may be shorter than the installed NDK path.



Remap the NDK to a shorter path (Windows solely)



The commonest subject with Windows lengthy paths is that instruments (similar to ld.exe) in the installed NDK folder end up with very deep paths, but the instruments don't help lengthy paths properly.



In native.properties, you possibly can set the property ndk.symlinkdir to request that the Gradle plugin create a symlink to the NDK. The trail of that symlink could be shorter than the existing NDK folder. For example, ndk.symlinkdir = C:\ will result in the following symlink: C: dk\19.0.5232133



Syncing project with Gradle information



When you make adjustments to the construct configuration information in your challenge, Android Studio requires that you just sync your challenge information in order that it may import your build configuration changes and run some checks to make sure your configuration won't create construct errors.



To sync your undertaking information, click on Sync Now in the notification bar that seems when you make a change, as shown in determine 3, or click on Sync Undertaking from the menu bar. If Android Studio notices any errors together with your configuration, for instance, your supply code uses API options that are only out there in an API degree higher than your compileSdkVersion, the Messages window seems to explain the problem.



Figure 3. Syncing the undertaking with build configuration files in Android Studio.



Source units



Android Studio logically groups supply code and sources for every module into source units. A module’s important/ source set includes the code and sources used by all its construct variants. Further source set directories are non-compulsory, and Android Studio does not robotically create them for you whenever you configure new build variants. However, creating source sets, similar to foremost/, helps organize information and sources that Gradle ought to solely use when building certain versions of your app:



Observe: In case you configure your build to mix multiple product flavors, you may create supply set directories for each mixture of product flavors between the flavor dimensions: src/productFlavor1ProductFlavor2/



For example, to generate the "fullDebug" version of your app, the build system merges code, settings, and resources from following source sets:



- src/fullDebug/ (the construct variant source set)- src/debug/ (the construct kind source set)- src/full/ (the product taste supply set)- src/essential/ (the principle supply set)



Word: When you create a brand new file or directory in Android Studio, using the File > New menu choices, you may create it for a selected supply set. The supply sets you possibly can select from are based in your build configurations, and Android Studio mechanically creates the required directories if they don't already exist.



If completely different source units include completely different variations of the identical file, Gradle uses the following priority order when deciding which file to make use of (supply units on the left override the information and settings of source units to the fitting):



This permits Gradle to use information which are particular to the build variant you are trying to construct whereas reusing actions, software logic, and sources that are common to other variations of your app. When merging a number of manifests, Gradle uses the identical priority order, so each construct variant can define totally different components or permissions in the final manifest. To study extra about creating custom supply sets, go to Create Source Units for Construct Variants.