当前位置: 首页 > news >正文

安卓启动 性能提升 20-30% ,基准配置 入门教程

1.先从官方下载demohttps://github.com/android/codelab-android-performance/archive/refs/heads/main.zip

2.先用Android studio打开里面的baseline-profiles项目

3.运行一遍app,这里建议用模拟器,(Pixel 6 API 34)设备运行,因为基准配置 需要root权限,如果手机没有root,就用模拟器运行。

4.运行时会报This version (1.4.5) of the Compose Compiler requires Kotlin version 1.8.20 but you appear to be using Kotlin version 1.9.22 which is not known to be compatible.  Please consult the Compose-Kotlin compatibility map located at https://developer.android.com/jetpack/androidx/releases/compose-kotlin to choose a compatible version pair (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

5.修改libs.versions.toml里的 kotlin = "1.9.22" 改为 kotlin = "1.8.20"

6.再运行一下,private fun NavBackStackEntry.lifecycleIsResumed() = this.getLifecycle().currentState == Lifecycle.State.RESUMED 这里的getLifecycle()会报错,改成lifecycle

7.再运行一次,这一次应该可以启动成功了,图片不显示可以忽悠不管,因为这个图片是从谷歌那边加载过来的,需要飞机,我们重点是基准配置,如果前面步骤碰见其他问题,基本与该项目无关,可以自行百度和AI解决。

8.点击File->new module 选择baseline profile generator ,如图,如果你是java党,可以右边改改配置,因人而异,我这里就选择默认的kotlin,直接点finish

9.如果你碰见 配置文件一片红,就删除,重新创建,放app下面,就不会爆红了,如果你没有遇见以下问题,可以跳过此步

如果删不掉,是因为app在引用,把这段去掉,点击sync now,再重新删一下

10.在baselineprofile的build.gradle.kts文件中修改,添加代码如下,代码解释:useConnectedDevices = false意思是否在真机上运行,由于真机没有root,只能在模拟器上运行,所以选择false就行,然后配置模拟器 ,记得导入

import com.android.build.api.dsl.ManagedVirtualDevice
import com.android.build.api.dsl.ManagedVirtualDeviceandroid {.......................
.......................targetProjectPath = ":app"testOptions.managedDevices.devices {create<ManagedVirtualDevice>("pixel6Api31") {device = "Pixel 6"apiLevel = 31systemImageSource = "aosp"}}
}// This is the configuration block for the Baseline Profile plugin.
// You can specify to run the generators on a managed devices or connected devices.
baselineProfile {managedDevices += "pixel6Api31"useConnectedDevices = false
}

如图下

11.在baselineprofile找到BaselineProfileGenerator类,自己项目可以根据自己情况更改,但是由于我们是demo,就演示一下,代码如下

  @Testfun generate() {// This example works only with the variant with application id `com.example.baselineprofiles_codelab`."rule.collect(packageName = "com.example.baselineprofiles_codelab",// See: https://d.android.com/topic/performance/baselineprofiles/dex-layout-optimizationsincludeInStartupProfile = true) {// This block defines the app's critical user journey. Here we are interested in// optimizing for app startup. But you can also navigate and scroll through your most important UI.// Start default activity for your apppressHome()startActivityAndWait()// TODO Write more interactions to optimize advanced journeys of your app.// For example:// 1. Wait until the content is asynchronously loaded// 2. Scroll the feed content// 3. Navigate to detail screen// 1. Wait until the content is asynchronously loaded.waitForAsyncContent()// 2. Scroll the feed content.scrollSnackListJourney()// 3. Navigate to detail screen.goToSnackDetailJourney()// Check UiAutomator documentation for more information how to interact with the app.// https://d.android.com/training/testing/other-components/ui-automator}}fun MacrobenchmarkScope.waitForAsyncContent() {device.wait(Until.hasObject(By.res("snack_list")), 5_000)val contentList = device.findObject(By.res("snack_list"))// Wait until a snack collection item within the list is rendered.contentList.wait(Until.hasObject(By.res("snack_collection")), 5_000)}fun MacrobenchmarkScope.scrollSnackListJourney() {val snackList = device.findObject(By.res("snack_list"))// Set gesture margin to avoid triggering gesture navigation.snackList.setGestureMargin(device.displayWidth / 5)snackList.fling(Direction.DOWN)device.waitForIdle()}fun MacrobenchmarkScope.goToSnackDetailJourney() {val snackList = device.findObject(By.res("snack_list"))val snacks = snackList.findObjects(By.res("snack_item"))// Select snack from the list based on running iteration.val index = (iteration ?: 0) % snacks.sizesnacks[index].click()// Wait until the screen is gone = the detail is shown.device.wait(Until.gone(By.res("snack_list")), 5_000)}

12.点击run按钮旁边三颗点,选择Edit Configurations...,然后点击左上角+,添加Gradle,在RUN下面一行添加:app:generateBaselineProfile -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile,点击OK就行,然后运行,如图:

如果出现> 'compileNonMinifiedReleaseJavaWithJavac' task (current target is 1.8) and 'compileNonMinifiedReleaseKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

把compileOptions里的兼容版本改成对应的版本就行了,由于我是用的java 17,就改成17就行了,然后重新运行

compileOptions {sourceCompatibility = JavaVersion.VERSION_17targetCompatibility = JavaVersion.VERSION_17
}

如果没有出现上面问题,可以忽悠不管

13.运行大概需要等5-6分钟,如果太久了,建议重新运行一下,因设备而异,运行完成的话,在app项目的src->release->generated->baselineProfiles文件下,生成2个txt文件,一个是1.8W行-2.5W行的baseline-prof.txt文件和startup-prof.txt文件,因项目而异,如果基准配置更多,生成的可能更多,由于我们只生成了,异步加载,点击,滚动,差不多2W行

如图:

14. 使用模拟器测试速度,在 baselineprofile 的 build.gradle.kts下的defaultConfig 里添加 testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR",代码解释,这段是用来印制模拟器的错误

defaultConfig {minSdk = 28targetSdk = 34testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR"
}

15.在baselineprofile 项目的StartupBenchmarks类里添加和前面基准配置的 代码一样,模拟和基准操作

代码如下:

  private fun benchmark(compilationMode: CompilationMode) {// This example works only with the variant with application id `com.example.baselineprofiles_codelab`."rule.measureRepeated(packageName = "com.example.baselineprofiles_codelab",metrics = listOf(StartupTimingMetric()),compilationMode = compilationMode,startupMode = StartupMode.COLD,iterations = 10,setupBlock = {pressHome()},measureBlock = {startActivityAndWait()waitForAsyncContent()// 2. Scroll the feed content.scrollSnackListJourney()// 3. Navigate to detail screen.goToSnackDetailJourney()})}fun MacrobenchmarkScope.waitForAsyncContent() {device.wait(Until.hasObject(By.res("snack_list")), 5_000)val contentList = device.findObject(By.res("snack_list"))// Wait until a snack collection item within the list is rendered.contentList.wait(Until.hasObject(By.res("snack_collection")), 5_000)}fun MacrobenchmarkScope.scrollSnackListJourney() {val snackList = device.findObject(By.res("snack_list"))// Set gesture margin to avoid triggering gesture navigation.snackList.setGestureMargin(device.displayWidth / 5)snackList.fling(Direction.DOWN)device.waitForIdle()}fun MacrobenchmarkScope.goToSnackDetailJourney() {val snackList = device.findObject(By.res("snack_list"))val snacks = snackList.findObjects(By.res("snack_item"))// Select snack from the list based on running iteration.val index = (iteration ?: 0) % snacks.sizesnacks[index].click()// Wait until the screen is gone = the detail is shown.device.wait(Until.gone(By.res("snack_list")), 5_000)}

16.然后,直接右键运行,这个测试类,在模拟器(Pixel 6 API 31) 以上运行,我建议在模拟器(Pixel 6 API 34)运行,因为API31,可能会报下面错误,如果出现了,就切到API34

如果出现了java.lang.IllegalStateException: Error: did not detect tracing on after 5000 ms  ,我建议切换到模拟器(Pixel 6 API34),运行就不有问题17.运行效果如下

StartupBenchmarks_startupCompilationBaselineProfiles
timeToFullDisplayMs      min   821.2,   median   908.4,   max 1,114.1
timeToInitialDisplayMs   min   438.9,   median   514.8,   max   678.4
Traces: Iteration 0 1 2 3 4 5 6 7 8 9
Timed out waiting for process (com.example.baselineprofiles_codelab) to appear on Pixel_6_API_34 [emulator-5556].
WARNING: Running on Emulator
Benchmark is running on an emulator, which is not representative of
real user devices. Use a physical device to benchmark. Emulator
benchmark improvements might not carry over to a real user's
experience (or even regress real device performance).

StartupBenchmarks_startupCompilationNone
timeToFullDisplayMs      min   984.5,   median 1,157.2,   max 1,257.2
timeToInitialDisplayMs   min   498.9,   median   606.3,   max   668.4
Traces: Iteration 0 1 2 3 4 5 6 7 8 9

StartupBenchmarks_startupCompilationNone 表示没有基准上运行

StartupBenchmarks_startupCompilationBaselineProfiles 表示在有基准上运行

1,157.2 ->908.4,提升大概200毫秒,差不多百分之20多,到这里教程就结束了,谢谢大家

相关文章:

安卓启动 性能提升 20-30% ,基准配置 入门教程

1.先从官方下载demohttps://github.com/android/codelab-android-performance/archive/refs/heads/main.zip 2.先用Android studio打开里面的baseline-profiles项目 3.运行一遍app&#xff0c;这里建议用模拟器&#xff0c;&#xff08;Pixel 6 API 34&#xff09;设备运行&a…...

Linux C/C++目录和文件的更多操作

1.access()库函数 用于判断当前用户对目录或文件的存取权限 #include<unistd.h>int accsee(const char *pathname,int mode);参数说明&#xff1a; pathname //目录或文件名 mode //需要判断的存取权限&#xff0c;在<unistd.h>预定义如下#define R_OK 4 //读权…...

如何高效地向Redis 6插入亿级别的数据

如何高效地向Redis插入亿级别的数据 背景不可用的方案可用方案:利用管道插入其他命令&#xff1a;参考&#xff1a; 背景 上一条记录&#xff1b;80G的存储&#xff1b;10几个文件&#xff0c;如何快速导入是一个大问题&#xff0c;也是一个很棘手的问题&#xff1b;如下将给出…...

中国历年肥料进口数量统计报告

数据来源于国家统计局&#xff0c;为1991年到2021年我国每年肥料进口数量统计。 2021年&#xff0c;我国进口肥料909万吨&#xff0c;比上年减少151万吨。 数据统计单位为&#xff1a;万吨 数据说明&#xff1a; 数据来源于国家统计局&#xff0c;为海关进出口统计数 我国肥料…...

即时通讯视频会议平台,WorkPlus本地化部署解决方案

随着现代科技的快速发展&#xff0c;传统的会议方式已经不再满足企业和组织的需求。即时通讯视频会议以其便利性和高效性&#xff0c;成为了现代企业沟通和协作的重要工具。通过即时通讯视频会议&#xff0c;企业可以实现无时差的交流和远程协作&#xff0c;增强团队合作和提高…...

Java的数据库编程-----JDBC

目录 一.JDBC概念&使用条件&#xff1a; 二.mysql-connector驱动包的下载与导入&#xff1a; 三.JDBC编程&#xff1a; 使用JDBC编程的主要五个步骤&#xff1a; 完整流程1&#xff08;更新update&#xff09;&#xff1a; 完整流程2(查询query)&#xff1a; 一.JDB…...

如何获取SSL证书,消除网站不安全警告

获取SSL证书通常涉及以下几个步骤&#xff1a; 选择证书颁发机构&#xff08;CA&#xff09;&#xff1a; 你需要从受信任的SSL证书颁发机构中选择一个&#xff0c;比如DigiCert、GlobalSign、JoySSL等。部分云服务商如阿里云、腾讯云也提供免费或付费的SSL证书服务。 生成证…...

Unity动画系统介绍

Unity动画系统介绍 Animator组件&#xff1a; 这是Unity中用于控制动画状态的组件&#xff0c;它与Animator Controller一起工作&#xff0c;可以基于游戏逻辑来切换不同的动画状态。 Animator Controller&#xff1a; 这是一个用于管理动画状态机的组件&#xff0c;它允许…...

Three.js-实现加载图片并旋转

1.实现效果 2. 实现步骤 2.1创建场景 const scene new THREE.Scene(); 2.2添加相机 说明&#xff1a; fov&#xff08;视场角&#xff09;&#xff1a;视场角决定了相机的视野范围&#xff0c;即相机可以看到的角度范围。较大的视场角表示更广阔的视野&#xff0c;但可能…...

ACM实训第25天

第四套 第一道&#xff08;修改&#xff09; #include<stdio.h> #include<string.h> int cnt[10]; void count_digits(int n,int* cnt){for(int i1;i<n;i){int numi;while(num){cnt[num%10];num/10;}} } int main(){int t;scanf("%d\n",&t);whi…...

GraphQL(2):使用express和GraphQL编写helloworld

1 安装express、graphql以及express-graphql 在项目的目录下运行一下命令。 npm init -y npm install express graphql express-graphql -S 2 新建helloworld.js 代码如下&#xff1a; const express require(express); const {buildSchema} require(graphql); const grap…...

Vue中的计算属性和侦听器:提升响应式编程的艺术

引言 Vue.js是一个用于构建用户界面的渐进式框架&#xff0c;它的核心特性之一是响应式编程。Vue通过数据绑定和响应式系统&#xff0c;使得开发者能够以声明式的方式处理数据变化。在Vue中&#xff0c;计算属性&#xff08;Computed Properties&#xff09;和侦听器&#xff…...

JavaScript倍速播放视频

F12打开开发者工具&#xff0c;打开控制台&#xff0c;输入这行代码&#xff0c;视频即可加速播放&#xff0c; 可以调整倍速&#xff08;2&#xff0c;4&#xff0c;8&#xff0c;16&#xff09; document. getElementsByTagName("video")[0]. playbackRate16...

ER图介绍

在数据库设计和建模中&#xff0c;实体-关系图&#xff08;Entity-Relationship Diagram&#xff0c;简称ER图&#xff09;是一个至关重要的工具。ER图通过图形化的方式描述了现实世界中的实体&#xff08;Entity&#xff09;及其之间的关系&#xff08;Relationship&#xff0…...

Oracle通过datax迁移线上表到历史库

历史数据迁移 线上库数据增长迅速&#xff0c;需要定期清理历史数据&#xff0c;因为异地灾备&#xff0c;但是带宽很小&#xff0c;不能使用数据泵直接往历史库导数&#xff0c;会导致本地机房到灾备机房的带宽被占满&#xff0c;调研过flink、golden gate、datax&#xff0c…...

java基础-深拷贝和浅拷贝

java中有一个概念叫深拷贝和浅拷贝&#xff0c;那这两个是什么意思呢&#xff1f;其实你可以对比一下c中的传值和传引用的问题。 深拷贝 即两个相同的对象地址不同&#xff0c;比如对象A通过拷贝出来对象B&#xff0c;在对B对象进行操作时不会影响到A对象的内容。 浅拷贝 和…...

Java数组操作

数组拓展 1.1 数组拷贝 需求&#xff1a;定义一个方法arraycopy, 从指定源数组中从指定的位置开始复制指定数量的元素到目标数组的指定位置。 1.2. 排序操作 需求&#xff1a;完成对int[] arr new int[]{2,9,6,7,4,1}数组元素的升序排序操作. 1.2.1.冒泡排序 对未排序的各元素…...

C++vector及其实现

第一个参数是类型(可以是自定义也可以是内置类型) 相当于生成一个该类型的数组 allocator是空间配置器 遍历 1.下标遍历 2.迭代器遍历 3.范围for 对象访问 有名对象访问 匿名对象访问 隐式类型转换 成员函数 sort 使用sort需要包含头文件algorithm eg. sort的使用非…...

路由策略实验1

先把地址全部配通 对R1 对R2 对R4 对R3 对R5 对R6 对R7 然后起路由协议 对R1 对R2 对R3 对R4 对R5 对R6 对R7...

含情脉脉的进程

冯诺依曼体系结构 一个计算机在工作的时候是怎样的呢&#xff1f; 我们所认识的计算机都是由一个个的硬件组件组成&#xff1a; 输入设备&#xff1a;键盘、鼠标、摄像头、话筒、磁盘、网卡 中央处理器&#xff08;CPU&#xff09;&#xff1a;运算器、控制器 输出设备&#x…...

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器的上位机配置操作说明

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器专为工业环境精心打造&#xff0c;完美适配AGV和无人叉车。同时&#xff0c;集成以太网与语音合成技术&#xff0c;为各类高级系统&#xff08;如MES、调度系统、库位管理、立库等&#xff09;提供高效便捷的语音交互体验。 L…...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

Python 包管理器 uv 介绍

Python 包管理器 uv 全面介绍 uv 是由 Astral&#xff08;热门工具 Ruff 的开发者&#xff09;推出的下一代高性能 Python 包管理器和构建工具&#xff0c;用 Rust 编写。它旨在解决传统工具&#xff08;如 pip、virtualenv、pip-tools&#xff09;的性能瓶颈&#xff0c;同时…...

使用Matplotlib创建炫酷的3D散点图:数据可视化的新维度

文章目录 基础实现代码代码解析进阶技巧1. 自定义点的大小和颜色2. 添加图例和样式美化3. 真实数据应用示例实用技巧与注意事项完整示例(带样式)应用场景在数据科学和可视化领域,三维图形能为我们提供更丰富的数据洞察。本文将手把手教你如何使用Python的Matplotlib库创建引…...

#Uniapp篇:chrome调试unapp适配

chrome调试设备----使用Android模拟机开发调试移动端页面 Chrome://inspect/#devices MuMu模拟器Edge浏览器&#xff1a;Android原生APP嵌入的H5页面元素定位 chrome://inspect/#devices uniapp单位适配 根路径下 postcss.config.js 需要装这些插件 “postcss”: “^8.5.…...

算法:模拟

1.替换所有的问号 1576. 替换所有的问号 - 力扣&#xff08;LeetCode&#xff09; ​遍历字符串​&#xff1a;通过外层循环逐一检查每个字符。​遇到 ? 时处理​&#xff1a; 内层循环遍历小写字母&#xff08;a 到 z&#xff09;。对每个字母检查是否满足&#xff1a; ​与…...

Mysql8 忘记密码重置,以及问题解决

1.使用免密登录 找到配置MySQL文件&#xff0c;我的文件路径是/etc/mysql/my.cnf&#xff0c;有的人的是/etc/mysql/mysql.cnf 在里最后加入 skip-grant-tables重启MySQL服务 service mysql restartShutting down MySQL… SUCCESS! Starting MySQL… SUCCESS! 重启成功 2.登…...

关于uniapp展示PDF的解决方案

在 UniApp 的 H5 环境中使用 pdf-vue3 组件可以实现完整的 PDF 预览功能。以下是详细实现步骤和注意事项&#xff1a; 一、安装依赖 安装 pdf-vue3 和 PDF.js 核心库&#xff1a; npm install pdf-vue3 pdfjs-dist二、基本使用示例 <template><view class"con…...

Linux系统部署KES

1、安装准备 1.版本说明V008R006C009B0014 V008&#xff1a;是version产品的大版本。 R006&#xff1a;是release产品特性版本。 C009&#xff1a;是通用版 B0014&#xff1a;是build开发过程中的构建版本2.硬件要求 #安全版和企业版 内存&#xff1a;1GB 以上 硬盘&#xf…...