4

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

sam_1234
  • 117
  • 12
  • 2
    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? – Giorgos Kylafas Jan 23 '23 at 08:51
  • 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! – sam_1234 Jan 23 '23 at 15:55
  • @sam_1234 facing exact same problem, did you find any solution for this ? – Android Killer Mar 09 '23 at 10:40
  • @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. - https://stackoverflow.com/questions/33923461/how-do-i-resolve-duplicate-files-copied-in-apk-meta-inf – sam_1234 Mar 09 '23 at 23:18
  • @sam_1234 Thanks for responding. I added exclusion for those packages which were showing error for one of the dependency in gradle and it worked. – Android Killer Mar 10 '23 at 11:25
  • Using MockWebServer solved all of my issues. – sam_1234 Mar 27 '23 at 04:03

3 Answers3

5

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

Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
1

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

0

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"
    }
}