Instrumented Tests Will Not Run -- 6 files found with path 'META-INF/LICENSE.md'
Asked Answered
P

6

19

Edit: Found out that MockK is causing this issue. I guess it is duplicating these files when I'm mocking my API request. When I remove MockK and/or Mockito. I do not get these errors. Any ideas?

Getting this error stating that there are these duplicate meta data files. I tried adding the packagingOptions block in my build.gradle file to exclude these files, but then my tests won't run at all. Is there a way to manually remove the duplicates? Where would these files be located? Any help is greatly appreciated. I am lost lol.

Tests:

    @RunWith(AndroidJUnit4::class)
    class ViewModelTests {
    
        @get:Rule(order = 1)
        val testRule = ActivityScenarioRule(MainActivity::class.java)
    
        private lateinit var viewModel: NewsViewModel
        private lateinit var repositoryImpl: RepositoryImpl
        private val context = InstrumentationRegistry.getInstrumentation().targetContext
    
        @Before
        fun setUp() {
            val newsDao = NewsDatabase.getDatabase(context).myDao()
            val newsApi = mockk<NewsApi>()
            viewModel = mockk()
            repositoryImpl = RepositoryImpl(newsApi, newsDao)
        }
    
        @Test
        fun test_empty_database() = runBlocking {
            assertEquals(0, repositoryImpl.getNewsFromDatabase.value?.size)
        }
    }

Error: Execution failed for task ':app:mergeDebugAndroidTestJavaResource'.

A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction 6 files found with path 'META-INF/LICENSE.md' from inputs: - /Users/sammorton/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter-params/5.8.2/ddeafe92fc263f895bfb73ffeca7fd56e23c2cce/junit-jupiter-params-5.8.2.jar - /Users/sammorton/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter-engine/5.8.2/c598b4328d2f397194d11df3b1648d68d7d990e3/junit-jupiter-engine-5.8.2.jar - /Users/sammorton/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter-api/5.8.2/4c21029217adf07e4c0d0c5e192b6bf610c94bdc/junit-jupiter-api-5.8.2.jar - /Users/sammorton/.gradle/caches/modules-2/files-2.1/org.junit.platform/junit-platform-engine/1.8.2/b737de09f19864bd136805c84df7999a142fec29/junit-platform-engine-1.8.2.jar - /Users/sammorton/.gradle/caches/modules-2/files-2.1/org.junit.platform/junit-platform-commons/1.8.2/32c8b8617c1342376fd5af2053da6410d8866861/junit-platform-commons-1.8.2.jar - /Users/sammorton/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter/5.8.2/5a817b1e63f1217e5c586090c45e681281f097ad/junit-jupiter-5.8.2.jar Adding a packagingOptions block may help, please refer to https://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/ResourcesPackagingOptions for more information

Parthinia answered 17/1, 2023 at 17:34 Comment(6)
I have the same problem. mockk 1.12.7 is the first version that causes it for me. Lower versions work OK. Is that the same for you too?Cultivar
I'll try that! I ended up just using fakes instead of a mocking library. I'll let you know if it works. Thank you!Parthinia
@Parthinia facing exact same problem, did you find any solution for this ?Acidophil
@AndroidKiller My solution was to use custom fakes of each dependency. Although some suggestions I found online included using packagingOptions to exclude the duplicated license, it didn't work for me as adding it to the build.gradle file caused my tests to fail. Trying older versions of mocking libraries also didn't solve the issue. There may be other solutions out there, and if I come across any, I'll be sure to share them with you. Here is one of the suggestions I found. - #33923961Parthinia
@Parthinia Thanks for responding. I added exclusion for those packages which were showing error for one of the dependency in gradle and it worked.Acidophil
Using MockWebServer solved all of my issues.Parthinia
P
24

For anyone else encountering this, the fix is, as suggested by the error output, to use an appropriate packaging (packagingOptions for older AGP versions) block:

packaging {
    resources.excludes.addAll(
        listOf(
            "META-INF/LICENSE.md",
            "META-INF/LICENSE-notice.md",
            ... // other conflicting META-INF bits
        )
    )
}

Credits to herrbert74

Pownall answered 2/6, 2023 at 11:27 Comment(1)
See https://mcmap.net/q/668995/-not-able-to-use-mockk-in-android-espresso-ui-testing for problems that may arise after this fix.Ferdinana
K
22

You can choose among the ResourcePackingOptions pickFirsts, merges, and excludes. Depending on the licenses, you might not be allowed to exclude them.

https://developer.android.com/reference/tools/gradle-api/8.0/com/android/build/api/dsl/ResourcesPackagingOptions

Android Studio projects default exclude licenses that don't require it (AL2 and LGPL2). For the rest, merging them together is a reasonable approach -

packagingOptions {
    resources {
        excludes += "/META-INF/{AL2.0,LGPL2.1}"
        merges += "META-INF/LICENSE.md"
        merges += "META-INF/LICENSE-notice.md"
    }
}
Kumamoto answered 13/6, 2023 at 6:30 Comment(3)
"Depending on the licenses, you might not be allowed to exclude them." - I find this answer highly underrated. Most answers online simply suggest excluding the license files without thought. This should be the accepted (and most highly voted) answer.Clownery
Thanks for this great answer! Seeing that merge is an option, I'll probably just merge all of them instead of excluding some - it makes for a cleaner section, and I wouldn't accidentally exclude a License I shouldn't.Penetration
using resources.excludes.add("META-INF/*") will make layout inspector not work aswell, so this is the better option for sure.Prance
M
5

for me getting rid of androidTestImplementation of MockK library solved my problem. if you aren't using androidTestImplementation of MockK or Mockito in your android tests remove these dependencies and run your code again. I was facing this bug when I was trying to run my compose test

Mcmillan answered 26/7, 2023 at 12:23 Comment(0)
F
3

Below code works for me

android { packagingOptions { resources.excludes.add("META-INF/*") } }
Festivity answered 17/4 at 12:9 Comment(0)
T
0

For gradle.kts and exclude happen only for tests:

testOptions {
    packaging {
        resources.excludes.add("META-INF/*")
    }
}

Btw, packagingOptions naming is deprecated

Thumbsdown answered 13/7 at 14:32 Comment(0)
S
0

For my case, in app's build.gradle, I use:

    packagingOptions {
        resources {
            excludes += [
                    'META-INF/*',
                    'license/*',
            ]
        }
    }

as the compiler complains more than one file with duplicated names found in these 2 folders.

Selfwinding answered 7/8 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.