使用Jetpack Benchmark执行测试时,INSTALL_FAILED_DUPLICATE_PERMISSION

问题描述

我有一个使用自定义权限的项目

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.company.app">

    <uses-permission android:name="com.company.app.permission.MY_PERMISSION" />

    <permission
        android:name="com.company.app.permission.MY_PERMISSION"
        android:description="@string/permission_access_summary"
        android:label="@string/permission_access"
        android:protectionLevel="signature" />

</manifest>

然后我添加了Jetpack Benchmark模块:

基准/build.gradle

plugins {
    id 'com.android.library'
    id 'androidx.benchmark'
    id 'kotlin-android'
}

android {
    compileSdkVersion 29
    buildToolsversion "29.0.3"

    defaultConfig.minSdkVersion 21

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        testInstrumentationRunner 'androidx.benchmark.junit4.AndroidBenchmarkRunner'
    }

    testBuildType = "release"
    buildTypes {
        debug {
            // Since debuggable can"t be modified by gradle for library modules,// it must be done in a manifest - see src/androidTest/AndroidManifest.xml
            minifyEnabled true
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"),"benchmark-proguard-rules.pro"
        }
        release {
            isDefault = true
            signingConfig signingConfigs.debug
        }
    }
}

dependencies {
    androidTestImplementation "androidx.benchmark:benchmark-junit4:$androidx_benchmark_version"
    androidTestImplementation "junit:junit:$junit_version"
    androidTestImplementation "androidx.test:runner:$test_runner_version"
    androidTestImplementation "androidx.test:rules:$test_rules_version"
    androidTestImplementation "androidx.test.ext:junit:$androidx_junit_version"

    // Add your dependencies here. Note that you cannot benchmark code
    // in an app module this way - you will need to move any code you
    // want to benchmark to a library module:
    // https://developer.android.com/studio/projects/android-library#Convert
    androidTestImplementation project(":app")
}

benchmark / src / androidTest / AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.company.app.test">

    <!--
      Important: disable debugging for accurate performance results
      In a com.android.library project,this flag must be disabled from this
      manifest,as it is not possible to override this flag from Gradle.
    -->
    <application
        android:debuggable="false"
        android:requestLegacyExternalStorage="true"
        tools:ignore="HardcodedDebugMode"
        tools:replace="android:debuggable" />
</manifest>

运行基准测试时,它可以成功构建,但无法安装

安装未成功。无法安装该应用程序: INSTALL_Failed_DUPLICATE_PERMISSION

apk列表:[0] '/Users/lap12846/avengers/ironman/demoapp/benchmark/build/outputs/apk/androidTest/release/benchmark-release-androidTest.apk' 安装失败,原因是:'null'

我的构建变体:应用:调试,基准测试:发布


请注意,如果我在运行测试之前卸载了该应用程序,则它将起作用。但这会删除我所有应用程序的数据。

解决方法

您可以在基准/src/androidTest/AndroidManifest.xml中的“权限”标签中添加tools:node =“ remove”:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.company.app.test">

    <permission
        android:name="com.company.app.permission.MY_PERMISSION"
        tools:node="remove"/>

    <!--
      Important: disable debugging for accurate performance results
      In a com.android.library project,this flag must be disabled from this
      manifest,as it is not possible to override this flag from Gradle.
    -->
    <application
        android:debuggable="false"
        android:requestLegacyExternalStorage="true"
        tools:ignore="HardcodedDebugMode"
        tools:replace="android:debuggable" />
</manifest>

参考:https://developer.android.com/studio/build/manifest-merge