INSTALL_FAILED_DUPLICATE_PERMISSION 使用 Jetpack Benchmark 执行测试时

INSTALL_FAILED_DUPLICATE_PERMISSION when execute test using Jetpack Benchmark

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

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 模块:

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>

当我 运行 基准测试时,它构建成功但 安装失败

Installation did not succeed. The application could not be installed: INSTALL_FAILED_DUPLICATE_PERMISSION

List of apks: [0] '/Users/lap12846/avengers/ironman/demoapp/benchmark/build/outputs/apk/androidTest/release/benchmark-release-androidTest.apk' Installation failed due to: 'null'

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


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

您可以在 benchmark/src/androidTest/AndroidManifest 中的“许可”标签中添加 tools:node="删除"。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">

    <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