پیکربندی سفارشی کردن سیستم بیلد اندروید :
گردل (Gradle) و پلاگین Android به شما یاری می نماید تا فرآیند ایجاد کرد APK اندروید
طراحی اپلیکیشن در مشهد خویش را سفارشی سازی فرمائید:
Build types
Build types خصوصیت های خاصی را که Gradle هنگام بیلد و پکیجینگ نرم افزار شما به کارگیری می نماید، تعریفوتمجید می نماید و به صورت معمول برای روند گوناگون چرخه پیشرفت شما تنظیمات میشود. مثلا، debug build type آیتم هایی را برای دیباگ تعریف و تمجید می نماید و APK را با debug key امضا می نماید. برای ساختن نرم افزار دستکم می بایست یک نوع Build را تمجید نمائید ( اندروید استودیو بطور پیش فرض گونه های دیباگ را منتشر کرده و ساخت و ساز می نماید.)
Product flavors
Product flavors ورژن های متفاوت نرم افزار شمارا علامت میدهد یعنی ممکن میباشد شما بخواهید ورژن های بدون پول و مبلغی از نرم افزار خویش را برای یوزرها منتشر نمائید. میتوانید با استعمال از کد ها و منابع متعدد، خلال به کار گیری از نصیب هایی که برای همگی ورژن های اپلیکیشن شما مشترک میباشند ،Product flavors را سفارشی فرمایید. Product flavors از روی میل میباشد و شما بایستی آنان را بصورت دستی ساخت نمایید.
Build variants
یک build variant ترکیبی از build type و product flavor میباشد و تنظیمات گردل برای ایجاد کرد نرمافزار شماست. با به کار گیری از build type ، میتوانید ورژن دیباگ product flavor خویش را در طی گسترش یا این که ورژن های امضا گردیده از product flavor خویش برای توزیع بسازید. اگرچه build type را به صورت بدون واسطه تنظیمات نمیکنید ، البته build type و product flavor را که تشکیل میدهد تنظیمات میکنید.
Manifest entries
میتوانید مقادیر بعضا از خاصیت فولدر مانیفست را در تنظیمات build type انتخاب نمایید. این مقادیر ساختگی مقادیر جان دار در فولدر مانیفست را رد می نمایند. درصورتیکه میخواهید برای ماژول های خویش Multiple APK ساختوساز فرمائید این عمل اثرگذار میباشد. دراین نصیب هریک از فولدر های APK دارنده یک اسم گوناگون، دستکم ورژن SDK یا این که ورژن SDK غرض میباشند. وقتی که مانیفست های متفاوت جان دار میباشند، Gradle پیکربندی مانیفست را مخلوط می نماید.
Dependencies
سیستم بیلد، Dependencies پروژه را از سیستم فولدر محلی شما و مخازن از شیوه بدور مدیر می نماید. این دستور سبب ساز میشود که پکیج های باینری Dependencies های خویش را در فهرست پروژه خویش به طور دستی کاوش ، بارگیری و اسکن فرمائید.
Signing
سیستم بیلد اندروید به شما قابلیت و امکان میدهد پیکربندی امضای را در تنظیمات ایجاد کرد معین فرمایید و قادر است بطور اتوماتیک APK های شمارا در طول روند تشکیل داد امضا نماید. سیستم بیلد اندروید ورژن دیباگ را با یک کلید پیش فرض و سند طومار با استعمال از اعتبارنامه های شناخته گردیده امضا می نماید تا از ورود سریع راز عبور در طی ایجاد کرد دوری نماید. سیستم بیلد ورژن ریلیز را امضا نمی نماید مگر اینکه صریحاً تنظیمات امضای این Build را انتخاب نمائید. در صورتیکه release key ندارید، میتوانید یک کدام از مواقعی را که در Sign in نرم افزار خویش توضیح داده گردیده ، ساخت و ساز فرمایید.
Code and resource shrinking
سیستم بیلد اندروید شمارا توانمند می سازد برای هر نوع ایجاد کرد یک فولدر، قانون ها ProGuard را انتخاب نمایید. هنگام ایجاد کرد نرم افزار، سیستم بیلد تیم ای از ضوابط مطلوب را برای معدود کردن کد و منابع شما با استعمال از ابزارهای خرد سازی داخلی آن، مانند R8 جاری ساختن می نماید.
Multiple APK support
سیستم بیلد اندروید شمارا قوی می سازد تا بطور اتوماتیک APK های متفاوتی بسازید که هریک صرفا کد و منابع موردنیاز برای تراکم برگه اکران خاص یا این که رابط باینری اپ (ABI) را دارا هستند.
پوشه Build Configuration
تولید پیکربندی Build سفارشی، شمارا مکلف به تولید تغییر و تحول در یک یا این که یک سری فولدر build configuration یا این که پوشه های build.gradle می نماید. این فولدر های متنی معمولی از Domain Specific Language) DSL) برای تعریف و بازنویسی منطق تشکیل داد با استعمال از Groovy استعمال می نمایند که یک گویش پویا برای ماشین مجازی جاوا (JVM) میباشد. برای استارت تنظیمات Build خویش نیازی به پی بردن Groovy ندارید چون پلاگین Android برای Gradle بیشتر عنصرها DSL ما یحتاج شمارا معرفی می نماید.
هنگام استارت یک پروژه نو، اندروید استودیو ، به صورت اتومات بعضی از این پوشه ها را ، همانگونه که در صورت ۲ نماد داده گردیده است ، برای شما تولید می نماید و بر پایه ی پیش فرض سنجیده آن ها را توده می نماید.
یکسری پوشه تنظیمات Gradle build وجود داراست که جزئی از ساختار پروژه استاندارد برای یک نرم افزار اندرویدی میباشد. پیش از آغاز به کانفیگ Build خویش ، اساسی میباشد که دامنه و مقصود هر مورد از این فولدر ها و عنصرها اساسی DSL آنانرا تعریفوتمجید نمایید.
پوشه Gradle settings
فابل settings.gradle ، که در دایرکتوری پروژه root ، به Gradle وجود دارااست میگوید که ماژول های آن را می بایست هنگام تشکیل داد نرم افزار include شوند. برای اکثر پروژه ها ، فولدر معمولی میباشد و صرفا موردها ذیل را مشمول میگردد:
include ‘:app’
با این هم اکنون ، پروژه های تعدادی ماژوله نیاز داراهستند تا ماژول هایی که به ایجاد کرد آخری امداد می نماید انتخاب نمایند.
پوشه top-level build
پوشه build.gradle سطح بالا که در فهرست پروژه ریشه واقع شدهاست، پیکربندی Build را تمجید می نماید که برای تمامی ماژول های پروژه شما ایفا میگردد. به صورت پیش فرض ، پوشه top-level build از بلوک buildscript برای تعریف و تمجید مخازن و تعلق های Gradle که برای کلیه ماژول های پروژه مشترک میباشد به کار گیری می نماید. مثال کد تحت پیکربندی پیش فرض و عنصرها DSL را میتواند پس از تولید یک پروژه نو، در build.gradl top-level بیابید.
/**
* The buildscript block is where you configure the repositories and
* dependencies for Gradle itself—meaning, you should not include dependencies
* for your modules here. For example, this block includes the Android plugin for
* Gradle as a dependency because it provides the additional instructions Gradle
* needs to build Android app modules.
*/
buildscript {
/**
* The repositories block configures the repositories Gradle uses to
* search or download the dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use local
* repositories or define your own remote repositories. The code below defines
* JCenter as the repository Gradle should use to look for its dependencies.
*
* New projects created using Android Studio 3.0 and higher also include
* Google's Maven repository.
*/
repositories {
google()
jcenter()
}
/**
* The dependencies block configures the dependencies Gradle needs to use
* to build your project. The following line adds Android plugin for Gradle
* version 3.6.0 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0'
}
}
/**
* The allprojects block is where you configure the repositories and
* dependencies used by all modules in your project, such as third-party plugins
* or libraries. However, you should configure module-specific dependencies in
* each module-level build.gradle file. For new projects, Android Studio
* includes JCenter and Google's Maven repository by default, but it does not
* configure any dependencies (unless you select a template that requires some).
*/
allprojects {
repositories {
google()
jcenter()
}
}
تنظیمات ویژگی ها project-wide
برای پروژه های اندرویدی که مشتمل بر یکسری ماژول می باشند، ممکن میباشد تمجید بعضا از خصوصیات در سطح پروژه و اشتراک آن ها در آحاد ماژول ها اثر گذار باشد. شما میتوانید این عمل را با افزودن خاصیت مازاد به بلوک ext در پوشه build.gradle top-level ایفا دهید.
buildscript {...}
allprojects {...}
// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
// The following are only a few examples of the types of properties you can define.
compileSdkVersion = 28
// You can also create properties to specify versions for dependencies.
// Having consistent versions between modules can avoid conflicts with behavior.
supportLibVersion = "28.0.0"
...
}
...
برای دسترسی بدین مختصات از یک ماژول در به عبارتی پروژه ، از syntax ذیل در پوشه build.gradle ماژول به کار گیری فرمایید.
android {
// Use the following syntax to access properties you defined at the project level:
// rootProject.ext.property_name
compileSdkVersion rootProject.ext.compileSdkVersion
...
}
...
dependencies {
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
...
}
پوشه module-level build
فولدر build.gradle در سطح ماژول ، که در هر project/module/ directory جایدارد ، به شما قابلیت و امکان میدهد پیکربندی Build را برای ماژول خاصی که در آن جایدارد تنظیمات نمایید. تنظیمات این پیکربندی به شما قابلیت و امکان میدهد مورد های پکیجینگ سفارشی ، مانند build types مازاد را ارائه دهید. و product flavors ، و پیکربندی در فایلbuild.gradle top-level یا این که main/ app manifest نادیده گرفته میشوند.
این مثال ماژول نرم افزار Android build build.gradle برخی از ارکان و پیکربندی مهم DSL را معلوم می نماید که بایستی بدانید.
/**
* The first line in the build configuration applies the Android plugin for
* Gradle to this build and makes the android block available to specify
* Android-specific build options.
*/
apply plugin: 'com.android.application'
/**
* The android block is where you configure all your Android-specific
* build options.
*/
android {
/**
* compileSdkVersion specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*/
compileSdkVersion 28
/**
* buildToolsVersion specifies the version of the SDK build tools, command-line
* utilities, and compiler that Gradle should use to build your app. You need to
* download the build tools using the SDK Manager.
*
* This property is optional because the plugin uses a recommended version of
* the build tools by default.
*/
buildToolsVersion "29.0.2"
/**
* The defaultConfig block encapsulates default settings and entries for all
* build variants, and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
/**
* applicationId uniquely identifies the package for publishing.
* However, your source code should still reference the package name
* defined by the package attribute in the main/AndroidManifest.xml file.
*/
applicationId 'com.example.myapp'
// Defines the minimum API level required to run the app.
minSdkVersion 15
// Specifies the API level used to test the app.
targetSdkVersion 28
// Defines the version number of your app.
versionCode 1
// Defines a user-friendly version name for your app.
versionName "1.0"
}
/**
* The buildTypes block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies Proguard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the default Proguard rules file.
*/
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/**
* The productFlavors block is where you can configure multiple product flavors.
* This allows you to create different versions of your app that can
* override the defaultConfig block with their own settings. Product flavors
* are optional, and the build system does not create them by default.
*
* This example creates a free and paid product flavor. Each product flavor
* then specifies its own application ID, so that they can exist on the Google
* Play Store, or an Android device, simultaneously.
*
* If you declare product flavors, you must also declare flavor dimensions
* and assign each flavor to a flavor dimension.
*/
flavorDimensions "tier"
productFlavors {
free {
dimension "tier"
applicationId 'com.example.myapp.free'
}
paid {
dimension "tier"
applicationId 'com.example.myapp.paid'
}
}
/**
* The splits block is where you can configure different APK builds that
* each contain only code and resources for a supported screen density or
* ABI. You'll also need to configure your build so that each APK has a
* different versionCode.
*/
splits {
// Settings to build multiple APKs based on screen density.
density {
// Enable or disable building multiple APKs.
enable false
// Exclude these densities when building multiple APKs.
exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
}
}
}
/**
* The dependencies block in the module-level build configuration file
* specifies dependencies required to build only the module itself.
* To learn more, go to Add build dependencies.
*/
dependencies {
implementation project(":lib")
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
پوشه Gradle properties :
Gradle همینطور مشتمل بر دو پوشه خاص در دایرکتوری پروژه root شما میباشد که میتوانید از آن ها برای تهیه پیکربندی مرتبط با گروه ابزار Gradle build خویش به کار گیری فرمائید:
gradle.properties
اینجاست که میتوانید پیکربندی project-wide Gradle را مانند حداکثر اندازه Gradle daemon’s تهیه و تنظیم نمائید.
local.properties
مختصات local environment را برای سیستم ایجاد کرد را تهیه و تنظیم می نماید ، مثلا مفاد ذیل:
ndk.dir – مسیر NDK. این خواص از در میان رفته میباشد. هر ورژن بارگیری گردیده از NDK در فهرست ndk در درون فهرست SDK Android نصب شود
sdk.dir – مسیر SDK
cmake.dir – مسیر CMake
ndk.symlinkdir – در +Android Studio 3.5 ، یک راه و روش ارتباطی با NDK ساخت و ساز می نماید که میتواند از مسیر نصب گردیده NDK کوتاه خیس باشد