47

I'm trying to test using the following directory structure (which was setup by Android Studio):

test directory structure

I can run some tests just fine, and even the AllTests.java runs fine without the AndroidManifest.xml file even being there. The thing is, for one of my new tests, I need the android.permission.INTERNET permission. So, I added the following to the AndroidManifest.xml file located within the androidTest directory:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.core"
          android:versionCode="2"
          android:versionName="2.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Unfortunately, this doesn't work. I'm still getting the following error when I run one of my tests:

E/RestAPIRequestTest﹕ Permission denied (missing INTERNET permission?)

I've tried setting the package to be com.example.core.test in my AndroidManifest.xml file (since that is what it shows up as in my Settings->Apps list), but with no joy.

I'm thinking it's not even recognizing the AndroidManifest.xml file, since the version number doesn't show in the Settings for the test app, either.

How can I inject the correct permissions for my test project?

3
  • 3
    I know this was over a year ago, but did you ever get this resolved? My manifest in src/androidTest/AndroidManifest.xml is not being merged into the full manifest.
    – Andrew
    Nov 11, 2015 at 19:20
  • @Andrew I was not able to to find a satisfactory answer for this, unfortunately. Our test structure has changed significantly, so this is no longer an issue for me, but prior to the change, none of the answers were working for me. :(
    – jwir3
    Nov 16, 2015 at 17:30
  • "How can I inject the correct permissions for my test project?" This is what I want to ask
    – linjiejun
    Nov 3, 2020 at 8:44

6 Answers 6

53

I had a similar issue, where to fix it I did:

  • Create a folder named "debug" next to "androidTest" folder, which corresponds to the debug variant of the app,
  • Put an AndroidManifest.xml file, which contains the required permission, in said "debug" folder.
  • Now (re)build, and the permission works under test, since the test app uses the debug variant.

It's not ideal because it blurs the line between test and debug, which isn't quite the same thing.

Update; what's happening is that:

  • There are multiple build types, like "debug" and "release".

  • Each can have an AndroidManifest.xml file, inside related folder with matching name, like "debug" and "release" folders.

  • But "androidTest" is NOT a build type (at time of writing), hence Gradle's default value for testBuildType option is "debug" (as docs mention), like:

    android {
    
        // ...
    
        testBuildType "debug"
    }
    
  • Said inconsistency is handled poorly by so-called Gradle-plugin ("com.android.tools.build:gradle:7.1.3").

  • And in fact, the "androidTest/AndroidManifest.xml" file only works for library-projects (not Apps, at time of writting).

Note that even if we manually set path to manifest, like:

android {
    sourceSets {
        androidTest {
            manifest.srcFile "${project.rootDir}/AndroidManifest.xml"
        }
    }
}

It's silently ignored for androidTest source-set (tested with Gradle 7.5.1), while simialr setting works for main source-set.

2
  • 2
    After couple of month a stumbled again about this error and your solution helped me again!!!! GENIUS :D Sep 24, 2017 at 18:16
  • If you want to make sure you separate the release types then you can create a new release type that inherits some other release type and put the file to a folder named after the new release type.
    – racs
    Nov 14, 2023 at 0:00
17

In older versions of Android Studio and the Android Gradle plugin the androidTest/AndroidManifest.xml file was ignored. This was documented at the tools.android.com site at the time.

With the Android Studio 1.0+ and Android Gradle 1.0+ plugin launch in December 2014 the AndroidManifest.xml file should now be merged with the normal main/AndroidManifest.xml files (in addition to the debug and release manifest files if they exist). More details regarding the manifest merging rules are here.

If you still run into issues or are just debugging manifest related testing issues try this (Adapt this slightly for Windows):

  1. Drop to a terminal
  2. change to to your project directory cd MyApplication
  3. Build your project, assuming 'debug' is the build type you want to test with, but you could also be testing with 'release' or a build script defined one. ./gradlew assembleDebugTest
  4. Then inspect your test APK manifest: ls app/build/intermediates/manifests/test/debug/AndroidManifest.xml
  5. View your application APK manifest: ls app/build/intermediates/manifests/full/debug/AndroidManifest.xml
  6. A merge output log can be found detailing the manifest merging process: ls app/build/outputs/apk/manifest-merger-debug-report.txt

A couple of extra notes:

  • An instrumentation element is automatically added to your test APK's AndroidManifest.xml so you should only be adding extra activities, permissions, etc that your test APK needs.
  • If testing with mock locations your application APK will need the ACCESS_MOCK_LOCATION permission. You can add the permission to your debug/AndroidManifest.xml file or you can define that the test APK and the application APK should use the same userId when deployed (sharedUserId attribute in your AndroidManifest.xml).
8

This is a known problem.

Currently (AGP <= 3.4.X) is not supporting AndroidManifest test merging.

This is reported here: https://issuetracker.google.com/issues/127986458 and here there is the issue created by one of the Roboelectric maintainers.

The workaround as described here its near the same proposed by user3286293 and currently is the only way to have the manifest merged for testing purposes.

Hope to see a fix for AGP 3.5 or 3.6

6

You need to define that in build.gradle file:

android {
    sourceSets {
        androidTest.manifest.srcFile "src/androidTest/AndroidManifest.xml"
    }
}
2
  • FWIW you don't even need this anymore.
    – Saket
    Nov 9, 2023 at 1:36
  • true, this was for the old times. Apr 9 at 13:22
5

As specified here, during instrumented tests, there are generated two .apk files. If you take a look, the smaller one it's most probably the one named app-debug-androidTest-unaligned.apk and it actually does include the provided permissions.

Inspecting the file with aapt d permissions <apk_file_path>.apk can be useful to see a list of all of them.

Now, there might be an issue with the context itself where the permission is requested. I had a similar problem, trying to write some screenshots on SD card (thus needing the WRITE_EXTERNAL_STORAGE permission).

This answer helped me to fix the problem, although I cannot fully understand why it's necessary.

In few words, you'll need to declare the same android:sharedUserId in both manifests, in order to merge the permissions when both apks are installed on the same device - that happens when tests are running. This helped me to separate permissions needed just for testing from the one in production.

0

One solution would be like build main apk and test apk in single run. Example: ./gradlew clean :main:assembleDebug :main:assembleDebugAndroidTest.

This will create a new instrumented main application which has all extra permissions required for test application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.