Gradle: how do I include a local jar from a dependent java project in an Android build?

前端 未结 2 1963
盖世英雄少女心
盖世英雄少女心 2020-12-09 11:41

In my Android app, I\'m getting a java.lang.NoClassDefFoundError when the code that references code in a dependent .jar is executed. My project includes an Andr

相关标签:
2条回答
  • 2020-12-09 11:57

    In recent (4+, IIRC) versions of gradle, you can achieve this with the following configuration:

    compile (project(':ProjectIDependOn')) {
      transitive = true
    }
    

    Setting transitive to true when depending on another project will expose all of that project's libraries to this project.

    0 讨论(0)
  • 2020-12-09 11:59

    This is not directly possible as local jars are not declared as transitive dependencies in Gradle.

    You have two options:

    • merge the two jars in your java library so that the output contains the local jar.
    • create a different project with no source, only the jar, and make the project depend on it.

    The second option gives you the ability to have more than one project depend directly on the local jar (on top of it becoming a transitive dependency). To do it, create a new gradle project and just put in its build.gradle the following:

    configurations.create("default")
    artifacts.add("default", file('somelib.jar'))
    

    This simply register your jar as the default artifact published by the project and this will get consumed by the other projects.

    0 讨论(0)
提交回复
热议问题