常见问题的思路建议-闪退、ANR原因、ANR、黑白屏、UI异常

本文探讨了Android应用中常见的问题,包括闪退、ANR及其原因。闪退可能源于空指针、空函数、死循环、内存泄露和溢出、网络和UI错误。ANR主要发生在BroadcastReceiver执行过长或主线程阻塞。避免ANR的方法包括避免在UI线程做耗时操作、减少onReceiver负担和合理使用Handler。同时,文章还提及了APP冷启动的黑白屏问题和UI异常,以及解决盒子塌陷的CSS技巧。

闪退

空指针:对象没有初始化就使用了;

空函数:有事件源但没有对应的事件对象;

死循环:没有合理的循环控制流;

内存泄露:同一个对象不停地开辟,而且没有释放;

内存溢出:程序所消耗的内存大于硬件提供的内存;

网络:异步加载时提前调用了数据(现象是在弱网时,根源是空指针);

界面UI:像拍照没有附加于ControlView;

主线程:

public class MainActivity4 extends AppCompatActivity { private static final String TAG = "camera2api"; // 1. 权限与相机相关变量 private static final int REQUEST_CAMERA_PERMISSIONS = 100; private TextureView mTextureView; // 预览显示载体 private Button mToggleFilterBtn; // 滤镜切换按钮 private CameraManager mCameraManager; // Camera2的“相机管理器”(列举相机、获取信息) private StreamConfigurationMap configMap; private String cameraId; // 当前使用的相机ID private CameraDevice mCameraDevice; // 代表物理相机设备(打开后才能操作) private CameraCaptureSession mCaptureSession; // 相机会话(所有预览/拍照操作通过它发起) private HandlerThread mBackgroundThread; // 相机操作后台线程(避免阻塞UI) private Handler mBackgroundHandler; // 2. OpenGL渲染器(连接Camera2和GPU) private CameraRenderer mCameraRenderer; private GLSurfaceView mGLSurfaceView; // 隐藏的GLSurfaceView(调度OpenGL渲染,不直接显示) // 3. Canvas绘制相关(叠加取景框) private Paint mFramePaint; // 画笔(绘制取景框) private volatile boolean isDrawingActive = false; // 核心控制标志 private SurfaceTexture surfaceTexture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); // 初始化UI控件 initViews(); // 初始化Canvas画笔(用于叠加取景框) initCanvasPaint(); // 初始化OpenGL渲染器(隐藏的GLSurfaceView,仅用于调度GPU) initRenderer(); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "判断是否有相机权限"); List<String> permissions=new ArrayList<>(); permissions.add(Manifest.permission.CAMERA); permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE); permissions.add(Manifest.permission.RECORD_AUDIO); if (!checkPermission(permissions)){ Log.i(TAG, "没有相机权限——>开始请求相机权限"); ActivityCompat.requestPermissions(this, permissions.toArray(new String[0]), REQUEST_CAMERA_PERMISSIONS); } Log.e(TAG, "1.1"); Log.i(TAG, "授权判断"); if (mTextureView.isAvailable()) { Log.i(TAG, "授权成功"); openCamera(); } Log.e(TAG, "1"); } // -------------------------- // 初始化UI控件 // -------------------------- private void initViews() { mTextureView = findViewById(R.id.texture_view); mToggleFilterBtn = findViewById(R.id.btn_toggle_filter); try { // 初始化相机管理器 mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); cameraId = mCameraManager.getCameraIdList()[0]; CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId); configMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); } catch (CameraAccessException e) { throw new RuntimeException(e); } Log.i(TAG, "使用相机ID: " + cameraId); Log.i(TAG, "设置监听: " ); // 滤镜切换按钮点击事件(需在GL线程执行,避免线程安全问题) mToggleFilterBtn.setOnClickListener(v -> { // queueEvent:将任务提交到GL线程执行 mGLSurfaceView.queueEvent(() -> mCameraRenderer.toggleFilter()); }); Log.e(TAG, "1.2"); // TextureView监听:Surface可用时打开相机 mTextureView.setSurfaceTextureListener(surfaceTextureListener); } private final TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) { Log.e(TAG, "1.3"); // 检查权限,有权限则打开相机 if (ActivityCompat.checkSelfPermission(MainActivity4.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { Log.e(TAG, "1.4"); openCamera(); } else { Log.e(TAG, "1.5"); // 申请相机权限 ActivityCompat.requestPermissions(MainActivity4.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSIONS); } // 重写TextureView的onDraw方法(演示Canvas绘制) mTextureView.setWillNotDraw(false); // 允许TextureView执行onDraw startDrawingThread(); // 启动绘制线程 // -------------------------- // 演示Canvas绘制(在预览上叠加白色取景框) // -------------------------- // 注意:Canvas绘制需在SurfaceTexture可用后执行,且每次预览刷新后需重绘 mTextureView.postInvalidate(); // 触发onDraw } @Override public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) { // 预览尺寸变化时,重新设置渲染视口(由OpenGL渲染器处理) } @Override public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) { // TextureView销毁时,释放相机资源 closeCamera(); return false; } @Override public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) { // 每次预览帧更新后,重绘Canvas(避免取景框消失) mTextureView.postInvalidate(); } }; // 新增辅助方法:检查单权限 private boolean checkPermission(List<String> permissions) { boolean isPermissionFlag=true; for (String permission : permissions) { if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){ isPermissionFlag=false; } } return isPermissionFlag; } private void startDrawingThread() { new Thread(() -> { while (isDrawingActive) { Canvas canvas = mTextureView.lockCanvas(); if (canvas != null) { drawFrameWithCanvas(canvas); // 执行绘制逻辑 mTextureView.unlockCanvasAndPost(canvas); } try { Thread.sleep(16); } catch (InterruptedException e) {} // 60FPS } }).start(); } // -------------------------- // 初始化Canvas画笔(绘制取景框) // -------------------------- private void initCanvasPaint() { mFramePaint = new Paint(); mFramePaint.setColor(Color.WHITE); // 白色取景框 mFramePaint.setStrokeWidth(5); // 线宽5px mFramePaint.setStyle(Paint.Style.STROKE); // 空心框 } // -------------------------- // Canvas绘制:在预览上叠加取景框(CPU层面) // -------------------------- private void drawFrameWithCanvas(Canvas canvas) { if (canvas == null) return; // 取景框尺寸:居中,宽高为屏幕的80% int screenWidth = canvas.getWidth(); int screenHeight = canvas.getHeight(); int frameSize = Math.min(screenWidth, screenHeight) * 4 / 5; // 80%尺寸 int left = (screenWidth - frameSize) / 2; int top = (screenHeight - frameSize) / 2; int right = left + frameSize; int bottom = top + frameSize; // 绘制取景框(空心矩形) canvas.drawRect(left, top, right, bottom, mFramePaint); } // -------------------------- // 初始化OpenGL渲染器(隐藏的GLSurfaceView) // -------------------------- private void initRenderer() { // 创建隐藏的GLSurfaceView(仅用于调度OpenGL,不添加到布局) mGLSurfaceView = new GLSurfaceView(this); mGLSurfaceView.setEGLContextClientVersion(2); // 使用OpenGL ES 2.0(移动端通用) mCameraRenderer = new CameraRenderer(this); // 注册回调(确保非空) mCameraRenderer.setSurfaceTextureListener(this::handleSurfaceTextureReady); // 同步启动GL线程 mGLSurfaceView.setRenderer(mCameraRenderer); mGLSurfaceView.onResume(); // 关键:强制启动渲染线程 // 等待纹理初始化(最多500ms) synchronized (mGLSurfaceView) { try { mGLSurfaceView.wait(500); } catch (InterruptedException e) { Log.w(TAG, "等待纹理初始化超时"); } } // 渲染模式:持续渲染(30帧/秒,保证预览流畅) mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); } // 回调方法(GL线程触发) private void handleSurfaceTextureReady(SurfaceTexture surfaceTexture) { synchronized (mGLSurfaceView) { mGLSurfaceView.notifyAll(); // 唤醒等待线程 this.surfaceTexture = surfaceTexture; } } // -------------------------- // Camera2核心1:打开相机(原生API流程) // -------------------------- private void openCamera() { Log.e(TAG, "————————>"+ Log.getStackTraceString(new Throwable())); Log.e(TAG, "1.6"); // 启动后台线程(Camera2操作必须在后台执行,避免阻塞UI) startBackgroundThread(); try { // 1. 打开相机(需权限,回调中获取CameraDevice) if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { return; } mCameraManager.openCamera(cameraId, new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice camera) { Log.e(TAG, "相机打开成功"); // 相机打开成功,保存CameraDevice实例 mCameraDevice = camera; // 下一步:配置预览会话(Camera2的核心流程) createPreviewSession(); } @Override public void onDisconnected(@NonNull CameraDevice camera) { // 相机关闭连接,释放资源 camera.close(); mCameraDevice = null; } @Override public void onError(@NonNull CameraDevice camera, int error) { // 相机出错,释放资源并退出 camera.close(); mCameraDevice = null; Toast.makeText(MainActivity4.this, "相机打开失败:" + error, Toast.LENGTH_SHORT).show(); finish(); } }, mBackgroundHandler); // 用后台线程处理回调 } catch (CameraAccessException e) { e.printStackTrace(); Toast.makeText(this, "相机访问异常:" + e.getMessage(), Toast.LENGTH_SHORT).show(); } } // -------------------------- // Camera2核心2:创建预览会话(配置输出目标+发送预览请求) // -------------------------- private void createPreviewSession() { try { // 1. 获取OpenGL渲染器的SurfaceTexture,包装成Surface(Camera2的输出目标) surfaceTexture = mCameraRenderer.getSurfaceTexture(); if (surfaceTexture == null) { Toast.makeText(this, "SurfaceTexture为空", Toast.LENGTH_SHORT).show(); return; } // 2. 配置预览尺寸(匹配相机支持的尺寸,避免画面变形) if (configMap == null) { Toast.makeText(this, "相机配置异常", Toast.LENGTH_SHORT).show(); return; } // 获取相机支持的SurfaceTexture输出尺寸,选择第一个(简化处理,实际需选与屏幕比例匹配的) Size[] previewSizes = configMap.getOutputSizes(SurfaceTexture.class); Size previewSize = previewSizes[0]; // 设置SurfaceTexture的默认缓冲区尺寸(与相机输出尺寸一致) surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight()); // 包装成Surface(Camera2的输出目标必须是Surface) Surface previewSurface = new Surface(surfaceTexture); // 3. 构建预览请求(告诉Camera2:要执行“预览”操作) CaptureRequest.Builder previewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); previewRequestBuilder.addTarget(previewSurface); // 相机输出的YUV数据发送到这个Surface // 4. 配置预览参数(自动对焦、自动曝光,提升体验) previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); // 连续对焦 previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); // 自动曝光+闪光灯 // 5. 创建相机会话(Camera2的“工作模式”,所有操作通过会话发起) mCameraDevice.createCaptureSession(Collections.singletonList(previewSurface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { // 会话配置成功,保存会话实例 mCaptureSession = session; try { // 发送“重复预览请求”(持续输出YUV数据,30帧/秒) CaptureRequest previewRequest = previewRequestBuilder.build(); // setRepeatingRequest:重复执行请求,直到停止 mCaptureSession.setRepeatingRequest(previewRequest, null, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { Toast.makeText(MainActivity4.this, "预览会话配置失败", Toast.LENGTH_SHORT).show(); } }, mBackgroundHandler); // 用后台线程处理回调 } catch (CameraAccessException e) { e.printStackTrace(); Toast.makeText(this, "创建会话异常:" + e.getMessage(), Toast.LENGTH_SHORT).show(); } } // -------------------------- // 启动相机操作后台线程 // -------------------------- private void startBackgroundThread() { mBackgroundThread = new HandlerThread("CameraBackground"); // 线程名(方便调试) mBackgroundThread.start(); mBackgroundHandler = new Handler(mBackgroundThread.getLooper()); // 线程的“遥控器” } // -------------------------- // 停止后台线程(避免内存泄漏) // -------------------------- private void stopBackgroundThread() { if (mBackgroundThread != null) { mBackgroundThread.quitSafely(); // 安全退出线程 try { mBackgroundThread.join(); // 等待线程执行完毕 mBackgroundThread = null; mBackgroundHandler = null; } catch (InterruptedException e) { e.printStackTrace(); } } } // -------------------------- // 关闭相机,释放所有资源 // -------------------------- private void closeCamera() { // 停止预览请求 if (mCaptureSession != null) { try { mCaptureSession.stopRepeating(); // 停止重复请求 mCaptureSession.abortCaptures(); // 终止所有未完成的请求 mCaptureSession.close(); } catch (CameraAccessException e) { e.printStackTrace(); } mCaptureSession = null; } // 关闭相机设备 if (mCameraDevice != null) { mCameraDevice.close(); mCameraDevice = null; } // 停止后台线程 stopBackgroundThread(); } // -------------------------- // 权限申请结果回调 // -------------------------- @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CAMERA_PERMISSIONS) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { // 权限拒绝,提示并退出 Toast.makeText(this, "需要相机权限才能预览", Toast.LENGTH_SHORT).show(); finish(); } } } // -------------------------- // 生命周期:暂停时释放资源 // -------------------------- @Override protected void onPause() { closeCamera(); super.onPause(); } @Override protected void onDestroy() { if (surfaceTexture != null) { surfaceTexture.release(); // 释放纹理资源 surfaceTexture = null; } super.onDestroy(); } }public class CameraRenderer extends AppCompatActivity implements GLSurfaceView.Renderer { private static final String TAG = "camera2api"; // 1. 基础变量 private Context mContext; private SurfaceTexture mSurfaceTexture; // 连接Camera2和GPU的桥梁(接收YUV数据) private int mCameraTextureId; // GPU纹理ID(存储YUV数据的“容器”) private int mProgramNoFilter; // 无滤镜OpenGL程序(顶点+片段着色器) private int mProgramBlackWhite; // 黑白滤镜OpenGL程序 private int mCurrentProgram; // 当前使用的程序(默认无滤镜) private boolean mIsBlackWhite = false; // 滤镜开关 // 2. 顶点坐标和纹理坐标(固定值:覆盖全屏) // 顶点坐标:左下、右下、左上、右上(OpenGL坐标系:-1~1) private float[] mVertexCoords = {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f}; // 纹理坐标:解决相机纹理上下颠倒问题(0~1,对应顶点坐标) private float[] mTexCoords = {0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f}; // 转为GPU可读取的FloatBuffer(避免CPU/GPU数据格式不兼容) private FloatBuffer mVertexBuffer; private FloatBuffer mTexCoordBuffer; // 3. 构造方法(初始化坐标缓冲区) public CameraRenderer(Context context) { mContext = context; // 初始化顶点坐标缓冲区 mVertexBuffer = ByteBuffer.allocateDirect(mVertexCoords.length * 4) .order(ByteOrder.nativeOrder()) // 按GPU原生字节序排列 .asFloatBuffer() .put(mVertexCoords); mVertexBuffer.position(0); // 重置读取指针 // 初始化纹理坐标缓冲区 mTexCoordBuffer = ByteBuffer.allocateDirect(mTexCoords.length * 4) .order(ByteOrder.nativeOrder()) .asFloatBuffer() .put(mTexCoords); mTexCoordBuffer.position(0); } // 在CameraRenderer类中添加 public interface SurfaceTextureListener { void onSurfaceTextureCreated(SurfaceTexture surfaceTexture); } private SurfaceTextureListener mListener; public void setSurfaceTextureListener(SurfaceTextureListener listener) { mListener = listener; } // -------------------------- // 渲染器核心1:初始化OpenGL环境(只执行1次,在GL线程) // -------------------------- @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // 1. 创建相机专用纹理(告诉GPU:这是相机YUV数据,不是普通图片) mCameraTextureId = createCameraTexture(); // 2. 绑定纹理到SurfaceTexture(让Camera2的YUV数据流入GPU) mSurfaceTexture = new SurfaceTexture(mCameraTextureId); // 当有新YUV帧时,通知GLSurfaceView刷新渲染(保证预览流畅) GLSurfaceView mGLSurfaceView = (GLSurfaceView) findViewById(R.id.texture_view); mSurfaceTexture.setOnFrameAvailableListener(surfaceTexture -> { ((GLSurfaceView) findViewById(R.id.texture_view)).requestRender(); }); // 修复1:必须设置帧监听器(否则onDrawFrame不触发) mSurfaceTexture.setOnFrameAvailableListener(st -> { if (mGLSurfaceView != null) { mGLSurfaceView.requestRender(); } }); // 修复2:空指针防护 if (mListener != null) { mListener.onSurfaceTextureCreated(mSurfaceTexture); } else { Log.w(TAG, "SurfaceTexture回调未注册!"); } // 3. 编译并链接两个OpenGL程序(无滤镜+黑白滤镜) String vertexShader = loadShaderFromRaw(R.raw.vertex_shader); // 通用顶点着色器 String fragNoFilter = loadShaderFromRaw(R.raw.frag_shader_no_filter); String fragBlackWhite = loadShaderFromRaw(R.raw.frag_shader_black_white); mProgramNoFilter = createOpenGLProgram(vertexShader, fragNoFilter); mProgramBlackWhite = createOpenGLProgram(vertexShader, fragBlackWhite); // 默认使用无滤镜程序 mCurrentProgram = mProgramNoFilter; } // -------------------------- // 渲染器核心2:每帧渲染(约30次/秒,在GL线程) // -------------------------- @Override public void onDrawFrame(GL10 gl) { // 1. 清空屏幕(避免上一帧画面残留,黑色背景) GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // 2. 更新纹理数据(从Camera2获取最新YUV帧,同步到GPU纹理) mSurfaceTexture.updateTexImage(); // 3. 切换当前OpenGL程序(根据滤镜开关选择无滤镜/黑白) mCurrentProgram = mIsBlackWhite ? mProgramBlackWhite : mProgramNoFilter; GLES20.glUseProgram(mCurrentProgram); // 激活当前程序(GPU开始执行该程序的着色器) // 4. 绑定顶点坐标(告诉GPU:画面要画在屏幕的哪个位置) int vPositionLoc = GLES20.glGetAttribLocation(mCurrentProgram, "vPosition"); GLES20.glEnableVertexAttribArray(vPositionLoc); // 启用顶点属性 // 传递顶点坐标给顶点着色器:2个值为1组(x,y),float类型,不归一化,无偏移,从mVertexBuffer读取 GLES20.glVertexAttribPointer(vPositionLoc, 2, GLES20.GL_FLOAT, false, 0, mVertexBuffer); // 5. 绑定纹理坐标(告诉GPU:纹理如何映射到屏幕顶点) int vTexCoordLoc = GLES20.glGetAttribLocation(mCurrentProgram, "vTexCoord"); GLES20.glEnableVertexAttribArray(vTexCoordLoc); GLES20.glVertexAttribPointer(vTexCoordLoc, 2, GLES20.GL_FLOAT, false, 0, mTexCoordBuffer); // 6. 绑定相机纹理(告诉GPU:要处理的YUV数据在这个纹理中) GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // 激活纹理单元0(GPU有多个纹理单元,这里用第0个) GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mCameraTextureId); // 绑定相机纹理 // 传递纹理单元0给片段着色器的sTexture变量(告诉片段着色器:采样这个纹理) int sTextureLoc = GLES20.glGetUniformLocation(mCurrentProgram, "sTexture"); GLES20.glUniform1i(sTextureLoc, 0); // 7. 绘制画面(GPU执行着色器,将处理后的RGB数据写入Surface) // GL_TRIANGLE_STRIP:用4个顶点画2个三角形,覆盖全屏(效最高) GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); // 8. 禁用顶点属性(避免GPU资源泄漏) GLES20.glDisableVertexAttribArray(vPositionLoc); GLES20.glDisableVertexAttribArray(vTexCoordLoc); // -------------------------- // 演示Canvas绘制(CPU层面叠加简单UI:如白色取景框) // -------------------------- // 注意:Canvas绘制需在UI线程或SurfaceTexture的回调中执行,这里仅演示逻辑 // 实际代码需在MainActivity的SurfaceTextureListener中处理(见步骤4) } // -------------------------- // 渲染器核心3:视图尺寸变化(如屏幕旋转,执行1次) // -------------------------- @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // 设置渲染视口(画面显示的区域:左上角(0,0),宽width,高height,即全屏) GLES20.glViewport(0, 0, width, height); } // -------------------------- // 辅助方法1:创建相机专用纹理 // -------------------------- private int createCameraTexture() { int[] textures = new int[1]; // 1. 向GPU申请1个纹理ID(类似“分配GPU内存地址”) GLES20.glGenTextures(1, textures, 0); int textureId = textures[0]; // 2. 绑定纹理(指定纹理类型为“相机专用纹理”GL_TEXTURE_EXTERNAL_OES) GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); // 3. 设置纹理过滤参数(避免拉伸时画面模糊/失真) // 缩小过滤:线性插值(画面缩小时平滑) GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); // 放大过滤:线性插值(画面放大时平滑) GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); // 纹理边缘处理: clamp_to_edge(边缘像素不重复,避免黑边) GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); // 4. 解绑纹理(避免后续操作污染当前纹理) GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); return textureId; } // -------------------------- // 辅助方法2:从raw目录加载着色器代码 // -------------------------- private String loadShaderFromRaw(int rawId) { try { InputStream is = mContext.getResources().openRawResource(rawId); byte[] buffer = new byte[is.available()]; is.read(buffer); is.close(); return new String(buffer, "UTF-8"); // 转为字符串格式的着色器代码 } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("加载着色器失败:" + e.getMessage()); } } // -------------------------- // 辅助方法3:创建OpenGL程序(编译+链接着色器) // -------------------------- private int createOpenGLProgram(String vertexShaderCode, String fragmentShaderCode) { // 1. 编译顶点着色器 int vertexShader = compileShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); // 2. 编译片段着色器 int fragmentShader = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); // 3. 链接程序(将两个着色器组合为GPU可执行的“指令集”) int program = GLES20.glCreateProgram(); GLES20.glAttachShader(program, vertexShader); // 附加顶点着色器 GLES20.glAttachShader(program, fragmentShader); // 附加片段着色器 GLES20.glLinkProgram(program); // 链接程序 // 4. 检查链接结果(避免编译失败) int[] linkStatus = new int[1]; GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0); if (linkStatus[0] == 0) { String errorLog = GLES20.glGetProgramInfoLog(program); GLES20.glDeleteProgram(program); // 删除无效程序 throw new RuntimeException("OpenGL程序链接失败:" + errorLog); } return program; } // -------------------------- // 辅助方法4:编译单个着色器 // -------------------------- private int compileShader(int type, String shaderCode) { int shader = GLES20.glCreateShader(type); // 创建着色器(顶点/片段) GLES20.glShaderSource(shader, shaderCode); // 设置着色器代码 GLES20.glCompileShader(shader); // 编译着色器 // 检查编译结果 int[] compileStatus = new int[1]; GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0); if (compileStatus[0] == 0) { String errorLog = GLES20.glGetShaderInfoLog(shader); GLES20.glDeleteShader(shader); // 删除无效着色器 throw new RuntimeException("着色器编译失败:" + errorLog); } return shader; } // -------------------------- // 对外提供的方法:切换滤镜(需在GL线程执行) // -------------------------- public void toggleFilter() { mIsBlackWhite = !mIsBlackWhite; } // -------------------------- // 对外提供的方法:获取SurfaceTexture(供Camera2绑定输出目标) // -------------------------- public SurfaceTexture getSurfaceTexture() { return mSurfaceTexture; } }看一下是什么问题,仍然不能正常预览
最新发布
09-18
请问这个类中的get_current_activity()方法,在其他用例文件,都有那些调用方式,方法如下: # !/usr/bin/env python # -*- coding: utf-8 -*- from typing import Optional import threading import subprocess import openpyxl import re from hytest import * import time import random from appium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import WebDriverException import os import zipfile import tempfile import shutil from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException import cv2 import numpy as np from lib.common import ADBHelper, get_app_driver, get_sup_app_driver, Operate from selenium.webdriver.common.by import By from PIL import Image import statistics from openpyxl import load_workbook from hytest import * from appium import webdriver import sys import os import re import time import platform import subprocess from appium.webdriver.common.multi_action import MultiAction from appium.webdriver.common.touch_action import TouchAction from selenium.webdriver.common.by import By from appium.webdriver.common.appiumby import AppiumBy from multiprocessing.dummy import Process from selenium.webdriver import ActionChains import yaml, ruamel.yaml import hytest from selenium.common.exceptions import ( TimeoutException, ElementNotInteractableException, StaleElementReferenceException, NoSuchElementException, WebDriverException ) ############################################################################### CURRENT_TIME = datetime.now().strftime('%Y%m%d%H%M%S') GLOBAL_REPORT_FOLDER = f"./reports/task_{CURRENT_TIME}_report" os.makedirs(GLOBAL_REPORT_FOLDER, exist_ok=True) class AppiumAutomationUtils: def __init__(self, device_name, platform_version, is_large_install=False): self.is_large_install = is_large_install # 根据是否为大文件安装场景设置超时 self.timeout = 300 if is_large_install else 30 self.desired_caps = { "platformName": "Android", "deviceName": device_name, "appium:platformVersion": platform_version, "appium:automationName": "UiAutomator2", "newCommandTimeout": self.timeout, "appium:adbExecTimeout": 60000 } self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps) self.last_activity_time = time.time() self.folder_name = GLOBAL_REPORT_FOLDER self.report_file = os.path.join(self.folder_name, "report.xlsx") self.report_files = os.path.join(self.folder_name, "downapp_report.xlsx") self.report_checkfile = os.path.join(self.folder_name, "checkapp_report.xlsx") def restart_appium_server(self): # 启动Appium服务 start_cmd = 'appium -a 127.0.0.1 -p 4723 --session-override --allow-insecure=adb_shell' subprocess.Popen(start_cmd, shell=True) time.sleep(10) @property def get_driver(self): return self.driver def start_app(self, package_name): crash_count, anr_count = 0, 0 flower_count, black_count, white_count = 0, 0, 0 try: self.driver.activate_app(package_name) time.sleep(5) start_time = time.time() while time.time() - start_time < 10: status = self.driver.query_app_state(package_name) if status != 2: if self.check_app_crash(): crash_count += 1 break if self.check_app_anr(): anr_count += 1 is_flower, is_black, is_white = self.verify_screen() if is_flower: flower_count += 1 if is_black: black_count += 1 if is_white: white_count += 1 time.sleep(3) except Exception as e: if self.check_app_crash(): crash_count += 1 elif self.check_app_anr(): anr_count += 1 raise RuntimeError(f"应用启动失败: {str(e)}") return crash_count, anr_count, flower_count, black_count, white_count def check_screen(self): timestamp = int(time.time() * 1000) temp_dir = "D:/problem/temp_screenshots/" os.makedirs(temp_dir, exist_ok=True) temp_path = os.path.join(temp_dir, f"temp_{timestamp}.png") try: screenshot = self.driver.get_screenshot_as_png() with open(temp_path, 'wb') as f: f.write(screenshot) except Exception as e: return False, False, False, None img_cv = cv2.imread(temp_path) if img_cv is None: os.remove(temp_path) return False, False, False, None height, width, _ = img_cv.shape total_pixels = height * width aspect_ratio = width / height if 1.5 <= aspect_ratio <= 2.0: num_rows, num_cols = 4, 8 elif aspect_ratio > 2.0: num_rows, num_cols = 3, 12 else: num_rows, num_cols = 3, 6 block_h, block_w = height // num_rows, width // num_cols block_list = [] FEATURE_THRESHOLDS = {'variance': 150, 'entropy': 2.2, 'edge_density': 0.08, 'contrast': 40} for row in range(num_rows): for col in range(num_cols): y_start = max(0, row * block_h) y_end = min(height, (row + 1) * block_h) x_start = max(0, col * block_w) x_end = min(width, (col + 1) * block_w) block = img_cv[y_start:y_end, x_start:x_end] gray = cv2.cvtColor(block, cv2.COLOR_BGR2GRAY) h, w = gray.shape block_area = h * w variance = np.var(gray) hist = cv2.calcHist([gray], [0], None, [256], [0, 256]).flatten() prob = hist / (block_area + 1e-7) entropy = -np.sum(prob * np.log(prob + 1e-7)) edges = cv2.Canny(gray, 50, 150) edge_pixels = np.count_nonzero(edges) edge_density = edge_pixels / block_area contrast = np.max(gray) - np.min(gray) block_list.append({ 'variance': variance, 'entropy': entropy, 'edge_density': edge_density, 'contrast': contrast, 'area': block_area, 'coords': (x_start, y_start, x_end, y_end) }) # 原花屏检测逻辑 all_variances = [b['variance'] for b in block_list] all_entropies = [b['entropy'] for b in block_list] global_variance_mean = statistics.mean(all_variances) global_entropy_mean = statistics.mean(all_entropies) dynamic_thresholds = { 'variance': global_variance_mean - 1.5 * np.std(all_variances), 'entropy': global_entropy_mean - 1.5 * np.std(all_entropies) } flower_blocks = [b for b in block_list if sum([b['variance'] < dynamic_thresholds['variance'], b['entropy'] < dynamic_thresholds['entropy'], b['edge_density'] > FEATURE_THRESHOLDS['edge_density'], b['contrast'] < FEATURE_THRESHOLDS['contrast']]) >= 3 and b['area'] >= (total_pixels * 0.01)] is_flower_screen = (sum(b['area'] for b in flower_blocks) / total_pixels) > 0.1 # 黑白屏检测逻辑 img_pil = Image.open(temp_path).convert('RGBA') color, total = (0, 0, 0), 0 for count, (r, g, b, a) in img_pil.getcolors(img_pil.size[0] * img_pil.size[1]): if a != 0: color = (color[0] + r * count, color[1] + g * count, color[2] + b * count) total += count if total > 0: dominant_color = (int(color[0] / total), int(color[1] / total), int(color[2] / total)) mean_brightness = statistics.mean(dominant_color) is_black_screen = mean_brightness < 10 is_white_screen = mean_brightness > 254 else: is_black_screen, is_white_screen = True, False return is_flower_screen, is_black_screen, is_white_screen, temp_path def verify_screen(self): """ 二次检测屏幕异常,减少检测屏幕异常误报 """ save_dir = "D:/problem/problemphoto/" os.makedirs(save_dir, exist_ok=True) temp_files = [] # 第一次检测 first_flower, first_black, first_white, first_temp = self.check_screen() if first_temp: temp_files.append(first_temp) if not (first_flower or first_black or first_white): self.clean_temp_files(temp_files) return False, False, False time.sleep(0.8) second_flower, second_black, second_white, second_temp = self.check_screen() if second_temp: temp_files.append(second_temp) # 最终判定:两次同类异常才保存 final_flower = first_flower and second_flower final_black = first_black and second_black final_white = first_white and second_white # 截图保存逻辑 if final_flower or final_black or final_white: timestamp = time.strftime("%Y%m%d_%H%M%S", time.localtime()) anomaly_types = [] if final_flower: anomaly_types.append("flower") if final_black: anomaly_types.append("black") if final_white: anomaly_types.append("white") filename = f"{timestamp}_{'_'.join(anomaly_types)}.png" # 保存二次检测的临时截图到目标目录 shutil.copy(second_temp, os.path.join(save_dir, filename)) self.log_error() # 清理所有临时文件(无论是否保存) self.clean_temp_files(temp_files) return final_flower, final_black, final_white def clean_temp_files(self, temp_files): """ 辅助方法:安全删除临时文件 """ for path in temp_files: if os.path.exists(path): try: os.remove(path) except Exception as e: pass def install_app(self, apk_path): """使用 os.system 执行 adb 命令""" command = f"adb install -d -r -g {apk_path}" exit_code = os.system(command) if exit_code == 0: print("应用安装成功") else: print("安装失败") def random_operation(self, duration,package_name,timeout=25): crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count = 0, 0, 0, 0, 0 # 获取设备屏幕尺寸 screen = self.driver.get_window_size() screen_width = screen['width'] screen_height = screen['height'] start_time = time.time() last_operation_time = time.time() # 记录最后一次成功操作的时间 while time.time() - start_time < duration: # 检测全局超时 if time.time() - last_operation_time > timeout: print(f"操作超时({timeout}秒无响应),结束测试") break #驻留检测 try: currentt_package = self.get_current_package().lower() target_package = package_name.lower() if currentt_package != target_package: print(f"当前包名不匹配目标!!!") self.driver.activate_app(package_name) time.sleep(3) last_operation_time = time.time() except Exception as e: print(f"驻留检测失败:{e}") try: # 随机选择操作类型(点击/滑动/输入) operation = random.choice(['click', 'swipe', 'input']) if operation == 'click': # 随机坐标点击 x = random.randint(0, screen_width) y = random.randint(0, screen_height) self.driver.tap([(x, y)], duration=50) time.sleep(0.3) elif operation == 'swipe': # 随机方向滑动(上下左右) direction = random.choice(['up', 'down', 'left', 'right']) if direction == 'up': self.driver.swipe(screen_width // 2, screen_height * 3 // 4, screen_width // 2, screen_height // 4, 500) elif direction == 'down': self.driver.swipe(screen_width // 2, screen_height // 4, screen_width // 2, screen_height * 3 // 4, 500) elif direction == 'left': self.driver.swipe(screen_width * 3 // 4, screen_height // 2, screen_width // 4, screen_height // 2, 500) elif direction == 'right': self.driver.swipe(screen_width // 4, screen_height // 2, screen_width * 3 // 4, screen_height // 2, 500) time.sleep(0.5) elif operation == 'input': input_elements = self.driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.EditText") if input_elements: input_element = random.choice(input_elements) input_element.click() random_digits = ''.join(str(random.randint(0, 9)) for _ in range(random.randint(1, 10))) input_element.send_keys(random_digits) time.sleep(0.8) if self.check_app_crash(): crash_count += 1 if self.check_app_anr(): anr_count += 1 is_flower, is_black, is_white = self.verify_screen() if is_flower: flower_screen_count += 1 if is_black: black_screen_count += 1 if is_white: white_screen_count += 1 last_operation_time = time.time() except Exception as e: pass return (crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count) def restart_app(self, times, package_name): """ 带重试逻辑的应用重启方法,支持 Activity 动态获取 :param times: 重启循环次数 :param package_name: 目标应用包名 """ crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count = 0, 0, 0, 0, 0 for _ in range(times): try: # 步骤1:终止应用并等待 self.driver.terminate_app(package_name) time.sleep(5) # 步骤2:尝试激活应用 self.driver.activate_app(package_name) time.sleep(5) except Exception as e: self.last_activity_time = time.time() retry_count = 0 while retry_count < 2: try: self.driver.activate_app(package_name) time.sleep(5) current_package = self.driver.current_package if package_name == current_package: break else: print(f"第{retry_count + 1}次启动未启动成功") retry_count += 1 time.sleep(5) except Exception as retry_e: continue if self.check_app_crash(): crash_count += 1 if self.check_app_anr(): anr_count += 1 is_flower, is_black, is_white = self.verify_screen() if is_flower: flower_screen_count += 1 if is_black: black_screen_count += 1 if is_white: white_screen_count += 1 return crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count def uninstall_app(self, package_name): try: self.driver.terminate_app(package_name) os.system(f"adb uninstall {package_name}") return True except: return False pass def generate_report(self, app_name, package_name, crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count, app_version, install_result, start_result,uninstall_result): if not os.path.exists(self.report_file): wb = openpyxl.Workbook() sheet = wb.active sheet.append(["序号", "应用名称", "包名", "应用版本号", "安装应用", "启动应用", "闪退次数", "ANR次数", "花屏次数", "黑屏次数", "白屏次数", "卸载结果","统计"]) row_number = 1 else: wb = openpyxl.load_workbook(self.report_file) sheet = wb.active row_number = len(sheet['A']) install_result_str = "成功" if install_result else "失败" start_result_str = "成功" if start_result else "失败" uninstall_result_str = "成功" if uninstall_result else "失败" has_failure = (not install_result) or (not start_result) or \ (crash_count > 0 or anr_count > 0 or flower_screen_count > 0 or black_screen_count > 0 or white_screen_count > 0) status = "fail" if has_failure else "pass" sheet.append([ row_number, app_name, package_name, app_version, install_result_str, start_result_str, crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count,uninstall_result_str, status ]) # 保存文件 wb.save(self.report_file) def generate_report_even_failed(self, app_name, package_name, crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count, app_version, install_result, start_result,uninstall_result): try: self.generate_report(app_name, package_name, crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count, app_version, install_result, start_result,uninstall_result) except Exception as e: print(f"生成报告时出错:{str(e)}") def log_error(self): current_timestamp = datetime.now().strftime('%Y%m%d%H%M%S') log_folder = f"D:/problem/logs/{current_timestamp}" os.makedirs(log_folder, exist_ok=True) adb_pull_command = f"pull /data/log/hilogs {log_folder}" ADBHelper().adb(adb_pull_command) time.sleep(10) adb_pull_command = f"pull /data/log/dropbox {log_folder}" ADBHelper().adb(adb_pull_command) time.sleep(10) def quit_driver(self): self.driver.quit() def click_element_by_texts(self, texts): """循环匹配文本点击页面文本元素""" screenshot_dir = "D:/problem/clickscreenshot" if not os.path.exists(screenshot_dir): os.makedirs(screenshot_dir) print(f"已创建截图文件夹:{screenshot_dir}") for text in texts: try: element = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, f'new UiSelector().text("{text}")') if element.is_enabled() and element.is_displayed(): element.click() return True except: continue else: screenshot_name = f"{int(time.time())}.png" screenshot_path = os.path.join(screenshot_dir, screenshot_name) self.driver.save_screenshot(screenshot_path) return False def get_app_version(self, package_name): """获取当前应用版本""" try: result = os.popen(f"adb shell dumpsys package {package_name} | findstr versionName").read().strip() if result: parts = result.split('=') if len(parts) > 1: return parts[1] return "未知版本" except: return "未知版本" def check_app_crash(self): """检测应用是否闪退""" try: current_activity = self.get_current_activity() home_activity = "com.huawei.android.launcher.unihome.UniHomeLauncher" if current_activity == home_activity: print(f"应用发生闪退!!!") self.scrennphoto_problem(problem_type="crash") self.log_error() return True else: return False except Exception as e: print(f"检测闪退时出错: {e}") return None def scrennphoto_problem(self, problem_type: str): """封装:截图并记录日志(根据问题类型生成不同目录)""" base_dir = "D:/problem/" screenshot_dir = os.path.join(base_dir, f"{problem_type}photo/") os.makedirs(screenshot_dir, exist_ok=True) timestamp = time.strftime('%Y%m%d_%H%M%S') screenshot_path = os.path.join(screenshot_dir, f"{problem_type}_{timestamp}.png") if self.driver.save_screenshot(screenshot_path): print(f"截图保存成功:{screenshot_path}") else: print(f"截图保存失败:{screenshot_path}") # def check_app_anr(self): # """判断当前页面是否存在ANR问题""" # try: # # 执行ANR检测命令 # result = subprocess.check_output( # "adb shell logcat -d | grep -i ANR", # shell=True, # text=True, # stderr=subprocess.STDOUT # ) # # 判断是否存在ANR # if "ANR" in result: # self.scrennphoto_problem(problem_type="anr") # self.log_error() # return True # return True # except Exception as e: # return False def check_app_anr(self): """判断当前页面是否存在ANR问题""" anr_keywords = ["无响应", "关闭应用", "是否将其关闭", "等待"] try: has_anr_screen = False for keyword in anr_keywords: elements = self.driver.find_elements( by=AppiumBy.XPATH, value=f"//*[contains(@text, '{keyword}')]" ) if elements: has_anr_screen = True break if has_anr_screen: print(f"检测到ANR:日志存在ANR记录且屏幕显示无响应提示") self.scrennphoto_problem(problem_type="anr") self.log_error() return True else: return False except Exception as e: print(f"ANR检测异常:{str(e)}") return False def is_target_activity(self, package_name, activity): """判断当前 Activity 是否属于目标应用""" return activity and activity.startswith(package_name) def get_current_activity(self): """获取当前Android设备的Activity名称""" try: # 执行ADB命令获取窗口信息 command = "adb shell dumpsys window | findstr mCurrentFocus" result = subprocess.check_output(command, shell=True, text=True, timeout=5) except subprocess.CalledProcessError: return "错误:ADB命令执行失败,请检查设备连接" except subprocess.TimeoutExpired: return "错误:命令执行超时,请确认ADB服务正常" except Exception as e: return f"错误:{str(e)}" if not result.strip(): return "提示:未获取到窗口信息,请先打开一个应用" # 用正则表达式匹配/后面的内容 match = re.search(r'/([^ }]+)', result) if match: return match.group(1) else: return "提示:未找到Activity名称,输出格式可能不一致" def get_current_package(self): """验证应用是否下载成功""" current_package = self.driver.current_package # 获取当前包名 return current_package def pull_download_apk(self): """ 从Android设备提取指定包名的APK文件 """ # 固定参数设置 adb_path = 'adb' output_dir = 'D:\\apk' # 获取当前应用包名 try: package_name = self.get_current_package() if not package_name: raise ValueError("无法获取当前应用包名") except Exception as e: raise RuntimeError(f"获取包名失败: {str(e)}") # 确保输出目录存在 # os.makedirs(output_dir, exist_ok=True) if not os.path.exists(output_dir): os.makedirs(output_dir) # 获取包安装路径 cmd_get_path = f"{adb_path} shell pm path {package_name}" try: result = subprocess.run(cmd_get_path, capture_output=True, text=True, shell=True, timeout=30) if result.returncode != 0: raise RuntimeError(f"获取包路径失败: {result.stderr.strip()}") # 解析输出结果 output = result.stdout.strip() if not output: raise ValueError(f"未找到包名为 '{package_name}' 的应用") # 提取APK路径 (取第一个路径) apk_paths = re.findall(r'package:(.+)', output) if not apk_paths: raise ValueError(f"无法解析包路径: {output}") device_path = apk_paths[0].strip() print(f"设备路径: {device_path}") except subprocess.TimeoutExpired: raise RuntimeError("获取包路径超时,请检查设备连接") # 创建本地文件名和路径 local_filename = f"{package_name}.apk" local_path = os.path.join(output_dir, local_filename) # 执行pull命令 cmd_pull = f"{adb_path} pull {device_path} \"{local_path}\"" try: result = subprocess.run(cmd_pull, capture_output=True, text=True, shell=True, timeout=60) if result.returncode != 0: raise RuntimeError(f"提取APK失败: {result.stderr.strip()}") except subprocess.TimeoutExpired: raise RuntimeError("提取APK超时,文件可能过大") # 验证文件是否成功提取 if not os.path.exists(local_path): raise FileNotFoundError(f"文件提取失败: {local_path}") print(f"成功提取APK到: {local_path}") return local_path def check_downapp_verify(self, package_name, app_name): """检测应用是否下载正确(返回字典)""" try: current_package = self.get_current_package().lower() expected_package = package_name.lower() # 构造返回结果(包含状态、应用名、包名、原因) result = { "status": None, "app_name": app_name, "package_name": package_name, "reason": "" } if current_package == expected_package: return True else: result["status"] = False result["reason"] = f"下载应用包名不符" return result except Exception as e: return { "status": None, "app_name": app_name, "package_name": package_name, "reason": f"检测应用时出错: {e}" } def downapp_report(self, app_name, package_name, download_result, pullapk_result, remark=None): if not os.path.exists(self.report_files): wb = openpyxl.Workbook() sheet = wb.active sheet.append(["应用名称", "包名", "下载结果", "上传结果", "统计", "备注"]) row_number = 1 else: wb = openpyxl.load_workbook(self.report_files) sheet = wb.active row_number = len(sheet['A']) download_result_str = "成功" if download_result else "失败" pullapk_result_str = "成功" if pullapk_result else "失败" has_failure = (not download_result) or (not pullapk_result) status = "fail" if has_failure else "pass" sheet.append([ app_name, package_name, download_result_str, pullapk_result_str, status, remark ]) wb.save(self.report_files) def click_element_with_swipe(self,driver, target_id, target_text, timeout=10, max_swipe=0): """ 定位并点击同时满足ID和文本条件的元素(未找到时下滑重试),返回操作结果 """ uiautomator_selector = f'new UiSelector().resourceId("{target_id}").textContains("{target_text}")' located = False for attempt in range(max_swipe + 1): try: element = WebDriverWait(driver, timeout).until( EC.element_to_be_clickable(("-android uiautomator", uiautomator_selector)) ) element.click() print(f"成功点击元素(ID={target_id}, 文本={target_text})") located = True break except TimeoutException: if attempt < max_swipe: print(f"第{attempt + 1}次定位超时,尝试下滑...") self.swipe_down(driver) else: print(f"已尝试{max_swipe + 1}次,未找到符合条件的元素") except Exception as e: print(f"操作失败,原因:{str(e)}") located = False break return located def swipe_down(self, driver, duration=500, swipe_times=1): """ 动态计算坐标实现下滑(页面向下滚动) """ # 获取屏幕尺寸 window_size = driver.get_window_size() x = window_size["width"] y = window_size["height"] # x, y = ADBHelper().get_phone_size() x1 = x * 0.5 y1 = y * 0.9 x2 = x * 0.5 y2 = y * 0.2 for i in range(swipe_times): driver.swipe(x1, y1, x2, y2, duration) print(f"第{i + 1}/{swipe_times}次下滑操作完成,等待页面加载...") driver.implicitly_wait(3) print(f"全部{swipe_times}次下滑操作执行完毕") def swipe_up(self, driver, duration=500, swipe_times=1): """ 动态计算坐标实现上滑(页面向上滚动) """ # 获取屏幕尺寸 # window_size = driver.get_window_size() # x = window_size["width"] # y = window_size["height"] x, y = ADBHelper().get_phone_size() x1 = x * 0.5 y1 = y * 0.2 x2 = x * 0.5 y2 = y * 0.9 for i in range(swipe_times): driver.swipe(x1, y1, x2, y2, duration) print(f"第{i + 1}/{swipe_times}次上滑操作完成,等待页面加载...") driver.implicitly_wait(3) print(f"全部{swipe_times}次上滑操作执行完毕") def go_back(self, driver, times=1, interval=3): """ :param times: 要点击返回键的次数(需≥0,默认1次) :param interval: 每次点击的间隔时间(秒,默认3秒) """ # 执行返回键点击 try: for i in range(times): driver.press_keycode(4) print(f"已点击返回键(第{i + 1}/{times}次)") time.sleep(interval) return True except WebDriverException as e: print(f"返回键操作失败,原因:{str(e)}") pass return False def wait_element_click(self,driver, locator, timeout=600): """ 等待元素可点击后尝试点击,返回点击是否成功 """ try: element = WebDriverWait(driver, timeout).until( EC.element_to_be_clickable(locator) ) except Exception as e: return False try: element.click() return True except (ElementNotInteractableException, StaleElementReferenceException, WebDriverException): return False def download_AG_app(self, driver, app_name, package_name): market_package = "com.huawei.appmarket" try: for i in range(2): driver.press_keycode(187) try: driver.find_element(By.ID, 'com.huawei.android.launcher:id/clear_all_recents_image_button').click() except: pass driver.press_keycode(3) except: pass time.sleep(10) driver.activate_app(market_package) time.sleep(10) self.click_element_with_swipe(driver, target_id='com.huawei.appmarket:id/enter_button', target_text='暂不安装') self.click_element_with_swipe(driver, target_id='android:id/button2', target_text='以后再说') self.swipe_up(driver, swipe_times=3) driver.find_element(By.ID, 'com.huawei.appmarket:id/fixed_search_view').click() time.sleep(3) src_text = driver.find_element(By.ID, "com.huawei.appmarket:id/search_src_text") src_text.set_text(app_name) time.sleep(3) driver.find_element(By.ID, 'com.huawei.appmarket:id/hwsearchview_search_text_button').click() time.sleep(3) result1 = self.click_element_with_swipe(driver, target_id='com.huawei.appmarket:id/ItemTitle',target_text=f'{app_name}', max_swipe=3) # 可以在应用市场搜索到该应用 if result1 == True: time.sleep(5) # 场景1:应用未安装 result2= self.click_element_with_swipe(driver, target_id='com.huawei.appmarket:id/hwprogressbutton_percentage_text_view',target_text='安装') if result2 == True: open_text = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("打开")') result3=self.wait_element_click(driver, open_text) # 应用规定时间内安装完成 if result3 == True: time.sleep(5) self.click_element_with_swipe(driver,target_id='com.android.permissioncontroller:id/permission_allow_button',target_text='允许') else: # 下载超时&开发者原因暂不支持下载 self.click_element_with_swipe(driver,target_id='com.huawei.appmarket:id/hwprogressbutton_percentage_text_view',target_text='%') self.go_back(driver, times=3) return { "status": "notime", "app_name": app_name, "package_name": package_name, "reason": f"{app_name}下载超时&开发者原因暂不支持下载 " } # 场景2:应用已存在 else: time.sleep(30) result4 = self.click_element_with_swipe(driver,target_id='com.huawei.appmarket:id/hwprogressbutton_percentage_text_view', target_text='打开') if result4 == True: time.sleep(5) self.click_element_with_swipe(driver, target_id='com.android.permissioncontroller:id/permission_allow_button',target_text='允许') else: pass else: failure_info = { "status": "fail1", "app_name": app_name, "package_name": package_name, "reason": f"应用市场未找到应用:{app_name}" } self.go_back(driver, times=3) return failure_info def check_apk_architecture(self,apk_path: str, app_name: str, package_name: str) -> dict: """ 检测APK架构并返回指定格式的结果字典 :param apk_path: APK文件路径 :param app_name: 应用名称(需外部传入) :param package_name: 应用包名(需外部传入) :return: 包含检测状态的字典(check_info) """ # 初始化默认结果(检测失败状态) check_info = { "status": "fail", "app_name": app_name, "package_name": package_name, "remark": "检测失败" } x64_archs = {'arm64-v8a', 'x86_64', 'mips64'} x32_archs = {'armeabi', 'armeabi-v7a', 'x86', 'mips'} detected_64 = set() detected_32 = set() try: if not os.path.isfile(apk_path): check_info["remark"] = "检测失败:APK文件不存在" return check_info with zipfile.ZipFile(apk_path, 'r') as zip_ref: all_members = zip_ref.namelist() for member in all_members: member = member.replace('\\', '/') if member.startswith("lib/"): lib_subpath = member.split("lib/")[1].split('/') if len(lib_subpath) < 1: continue arch_dir = lib_subpath[0].lower() if not arch_dir: continue if arch_dir in x32_archs: detected_32.add(arch_dir) elif arch_dir in x64_archs: detected_64.add(arch_dir) # 判断检测结果 has_64bit = len(detected_64) > 0 if has_64bit: # 64位检测成功 check_info.update({ "status": "success", "remark": "应用64位,已保留" }) else: # 32位检测成功 os.remove(apk_path) check_info.update({ "status": "success", "remark": "应用32位,已删除" }) except Exception as e: check_info["remark"] = f"检测失败:{e}" return check_info def checkapp_report(self, app_name, package_name, check_result): if not os.path.exists(self.report_checkfile): wb = openpyxl.Workbook() sheet = wb.active sheet.append(["应用名称", "包名", "检测结果"]) row_number = 1 else: wb = openpyxl.load_workbook(self.report_checkfile) sheet = wb.active row_number = len(sheet['A']) sheet.append([ app_name, package_name, check_result ]) wb.save(self.report_checkfile)
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值