3

I have a library with the most simple manifest possible

<manifest package="com.xxx.xxx.android" />

But this library needs to get two metadata values from Android Manifest to work properly, so to run my tests I added to src/test/AndroidManifest.xml the following code:

<manifest package="com.xxx.xxx.android"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <application>
        <meta-data
            android:name="var1"
            android:value="value1"/>
        <meta-data
            android:name="var2"
            android:value="value2"/>
    </application>
</manifest>

As I've read from the documentation I have to set the new AndroidManifest.xml path in gradle. I'm trying to do this way:

android {
    ...
    buildTypes {
        debug {
            testCoverageEnabled true
        }

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        String sharedTestDir = 'src/sharedTest/java'
        String testManifest = 'src/test/AndroidManifest.xml'
        test {
            java.srcDir sharedTestDir
            manifest.srcFile testManifest
        }
        androidTest {
            java.srcDir sharedTestDir
            manifest.srcFile testManifest
        }
    }

    testOptions.unitTests.includeAndroidResources true
}

But it doesn't work and I'm not able to get the metadata in my tests. It only works if I set the manifest.srcFile for main, instead of test and/or androidTest.

Victor Laerte
  • 6,446
  • 13
  • 53
  • 102

1 Answers1

4

Try src/debug/AndroidManifest.xml in combination with testBuildType "debug".

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • If I do this it works, but I start to get an error when trying to compile a sample using the library about `Manifest merge failing`. Do I do add something more in Manifest? – Victor Laerte Jan 16 '19 at 19:51
  • @VictorLaerte this should ordinary work barely based upon two alternate `AndroidManifest.xml`... could only imagine, that the path changes in `build.gradle` might mess up the default behavior. is there maybe a more detailed error message, which tells why it fails to merge? – Martin Zeitler Jan 16 '19 at 20:08
  • I believe it's not wrongful. Doing the way you said the tests run successfully the only problem now is that my sample is not able to merge manifests. (Sample is also running debug mode) – Victor Laerte Jan 16 '19 at 20:21
  • 1
    the `LibManifest.xml` has no `application` tag - and the `SampleManifest.xml` does not open the `application` tag... which might explain the `Manifest merger failed with multiple errors`, see https://developer.android.com/studio/build/manifest-merge – Martin Zeitler Jan 16 '19 at 20:22
  • Sorry, the SampleManifest is opening the tag but I omited. But you right in LibManifest.xml. I'll try again – Victor Laerte Jan 16 '19 at 20:24
  • Doing the way you told me, looking to the documentation it says that application tag should be combined. All of them now have but it's still not combining, can you see what I'm still doing wrong? :/ – Victor Laerte Jan 16 '19 at 20:37
  • As you said looking to documentation and adding `tools:replace="android:value"` in my sample it works. This might be helpful to add in the original answer. This link also helped a lot https://stackoverflow.com/a/42023614/1965189 – Victor Laerte Jan 16 '19 at 20:50