java.lang.SecurityException: Permission Denial: starting instrumentation not allowed because package does not have a signature matching the target

Viewed 554

I am trying to build a Robotium based test app programmatically from BMI app. (https://f-droid.org/repository/browse/?fdid=com.zola.bmi&fdpage=10)

Environment:

BMI APK is built on the Windows environment using Android Studio. BMI test APK is built on Ubuntu tomcat based server environment programmatically.

When I tried to install and execute, I am getting the following error:

java.lang.SecurityException: Permission Denial: starting instrumentation ComponentInfo{com.zola.bmi.test/android.test.InstrumentationTestRunner} from pid=14074, uid=14074 not allowed because package com.zola.bmi.test does not have a signature matching the target com.zola.bmi

When I tried the following steps, the error is not appearing:

  1. Uninstall previous BMI app and test app

    adb -d uninstall com.zola.bmi
    adb -d uninstall com.zola.bmi.test
    
  2. Change test runner in BMI app -> build.gradle from testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" to testInstrumentationRunner "android.test.InstrumentationTestRunner"

  3. Build and install BMI app with Gradle tasks=compileDebugAndroidTestJavaWithJavac' (in Android Studio)

  4. Run test cases from Android studio (Now test cases should run properly)

  5. Take Keystore from C:\Users\natha.android\debug.keystore

  6. Copy this debug.keystore to server path '/opt/tomcat/.android'

  7. Build test APK

  8. Install using:

    adb install -r BMI-debug.apk
    

Problem: Whenever I tried to run the generated Robotium test app in a new Android device, '....does not have a signature matching the target' issue appears.

Update Section:

[Update] After some analysis, I have found the steps to eliminate this error in the following link: Manual Steps:

https://adventuresinqa.com/2011/11/29/how-to-resign-a-android-apk-file-for-testing/

If you want to test an apk file e.g. with Robotium the android app apk and your test project apk file MUST have the same signature!
Rename the apk file into a zip file, e.g. Name.apk = Name.zip
Unpack/ Unzip the zip file
Delete the META-INF folder
Repack/ Rezip the folder again to a zip file
Rename the zip file again to an apk file
Open a terminal window / Command prompt and enter (the jarsigner tool is located in the bin folder of your installed java sdk
jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android APPNAME.apk androiddebugkey

Note: These steps are automated in Linux environment in the following link:

https://github.com/thorikawa/re-sign-apk/blob/master/apksigner.py


Question

  1. How to avoid '...does not have a signature matching the target' error?
  2. Is it possible to build a BMI debug app without a signature to avoid this error?
  3. How to generate programmatically a Robotium test APK without this error?
  4. What magic Android studio does when the app and test are run from the Studio environment to eliminate this error?
  5. Is there any python script (in Windows environment) available to automate the manual steps provided in the 'Update Section' (similar to https://github.com/thorikawa/re-sign-apk)?

Build.gradle for BMI App

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "21.1.1"
    lintOptions {
        abortOnError false
    }
    defaultConfig {
        applicationId "com.zola.bmi"
        minSdkVersion 15
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
//        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled = true
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.6.0'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'junit:junit:4.12'
}

Build.gradle for generated Robotium based test APK

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.zola.bmi"
        minSdkVersion 8
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"       
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.3'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
}
0 Answers
Related