با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
برای بهترین تجربه کاربری، باید برنامه خود را بهینه سازی کنید تا آن را تا حد امکان کوچک و سریع کنید. بهینهساز برنامه ما که R8 نام دارد، برنامه شما را با حذف کد و منابع استفاده نشده، بازنویسی کد برای بهینهسازی عملکرد زمان اجرا و موارد دیگر سادهسازی میکند. برای کاربران شما، این به این معنی است:
برای فعال کردن بهینهسازی برنامه، isMinifyEnabled = true (برای بهینهسازی کد) و isShrinkResources = true (برای بهینهسازی منابع) را در اسکریپت ساخت سطح برنامه نسخه انتشار خود تنظیم کنید، همانطور که در کد زیر نشان داده شده است. توصیه می کنیم همیشه هر دو تنظیمات را فعال کنید. همچنین توصیه میکنیم بهینهسازی برنامه را فقط در نسخه نهایی برنامهتان که قبل از انتشار آزمایش میکنید فعال کنید - معمولاً نسخه انتشار شما - زیرا بهینهسازیها زمان ساخت پروژه شما را افزایش میدهند و به دلیل روشی که کد را تغییر میدهد میتواند اشکالزدایی را سختتر کند.
علاوه بر این، با حذف این خط از فایل gradle.properties پروژه خود، بررسی کنید که R8 از قابلیت های بهینه سازی کامل خود استفاده می کند، در صورت وجود:
android.enableR8.fullMode=false# Remove this line from your codebase.
توجه داشته باشید که فعال کردن بهینهسازی برنامه، درک ردیابی پشته را دشوار میکند، به خصوص اگر R8 نام کلاسها یا روشها را تغییر نام دهد. برای دریافت ردیابی پشته که به درستی با کد منبع شما مطابقت دارد، به بازیابی ردیابی پشته اصلی مراجعه کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-30 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-30 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Enable app optimization\n\nFor the best user experience, you should optimize your app to make it as small\nand fast as possible. Our app optimizer, called R8, streamlines your app by\nremoving unused code and resources, rewriting code to optimize runtime\nperformance, and more. To your users, this means:\n\n- Faster startup time\n- Improved rendering and runtime performance\n- Fewer [ANRs](/topic/performance/anrs/keep-your-app-responsive)\n\n| **Important:** You should always enable optimization for your app's release build; however, you probably don't want to enable it for tests or libraries. For more information about using R8 with tests, see [Test and troubleshoot the\n| optimization](/topic/performance/app-optimization/test-and-troubleshoot-the-optimization). For more information about enabling R8 from libraries, see [Optimization for library authors](/topic/performance/app-optimization/library-optimization).\n\nTo enable app optimization, set `isMinifyEnabled = true` (for code optimization)\nand `isShrinkResources = true` (for resource optimization) in your [release\nbuild's](/studio/publish/preparing#turn-off-debugging) app-level build script as shown in the following code. We recommend\nthat you always enable both settings. We also recommend enabling app\noptimization only in the final version of your app that you test before\npublishing---usually your release build---because the optimizations increase the\nbuild time of your project and can make debugging harder due to the way it\nmodifies code. \n\n### Kotlin\n\n```kotlin\nandroid {\n buildTypes {\n release {\n\n // Enables code-related app optimization.\n isMinifyEnabled = true\n\n // Enables resource shrinking.\n isShrinkResources = true\n\n proguardFiles(\n // Default file with automatically generated optimization rules.\n getDefaultProguardFile(\"proguard-android-optimize.txt\"),\n\n ...\n )\n ...\n }\n }\n ...\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n buildTypes {\n release {\n\n // Enables code-related app optimization.\n minifyEnabled true\n\n // Enables resource shrinking.\n shrinkResources true\n\n // Default file with automatically generated optimization rules.\n proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')\n\n ...\n }\n }\n}\n```\n\nAdditionally, verify that R8 uses its full optimization capabilities by\nremoving this line from your project's `gradle.properties` file, if it exists: \n\n android.enableR8.fullMode=false # Remove this line from your codebase.\n\nNote that enabling app optimization makes stack traces difficult to understand,\nespecially if R8 renames class or method names. To get stack traces that\ncorrectly correspond to your source code, see\n[Recover the original stack trace](/topic/performance/app-optimization/test-and-troubleshoot-the-optimization#recover-original-stack-trace).\n\nIf R8 is enabled, you should also [create Startup Profiles](/topic/performance/baselineprofiles/dex-layout-optimizations) for even better\nstartup performance.\n\nIf you enable app optimization and it causes errors, here are some strategies to\nfix them:\n\n- [Add keep rules](/topic/performance/app-optimization/add-keep-rules) to keep some code untouched.\n- [Adopt optimizations incrementally](/topic/performance/app-optimization/adopt-optimizations-incrementally).\n- Update your code to [use libraries that are better suited for optimization](/topic/performance/app-optimization/choose-libraries-wisely).\n\n| **Caution:** Tools that replace or modify R8's output can negatively impact runtime performance. R8 is careful about including and testing many optimizations at the code level, in [DEX layout](/topic/performance/app-optimization/6), and in correctly producing Baseline Profiles - other tools producing or modifying DEX files may break these optimizations, or otherwise regress performance."]]