0

I use gradle to build an Android project("projectA") that needs an external jar file for building.

I have a second project (Unity3d in this case, so let's call it "projectU") that creates a jar file, among other things. This external project is build via a separate build.gradle file.

Before projectA is built via gradle, I build projectU. Then, a few files are copied from the finished build of projectU into the gradle Android project directory of projectA.

Files that need to be copied from projectU to projectA:

1.) project data files that go into projectA/assets/ 2.) native libraries that go into projectA/src/main/jniLibs/ 3.) a file called unity-classes.jar that goes into projectA/libs/unity-classes.jar

When I copy those files manually and then build projectA it works well.

I now want to automate this via Gradle. I have a build.gradle for projectU, and one for projectA. The one in projectA has a task that copies the files mentioned above from projectU into the project directory of projectA.

When I build projectU manually via command line, and then after that projectA, it works perfectly:

gradlew :projectU:build gradlew :projectA:assemble

But when I set up projectU as a dependency of projectA inside of the build.gradle of projectA, the build of projectA fails, because it can't "see" the "unity-classes.jar" file. It's as if the file isn't in the build directory early enough.

If I run the same build for a second time (without cleaning before), it works fine, because the unity-classes.jar file is already there and is found by the gradle build of projectA.

Questions:

1.) How do I make sure that my build of projectU, and the task in projectA that copies the files over runs before ANY android task (or evaluation?) in projectA? Or

2.) Where do I have to copy the unity-classes.jar file so that it is included into the java compilation of projectA?

Or is there a different approach?

Thanks!

treesAreEverywhere
  • 3,722
  • 4
  • 28
  • 51

2 Answers2

0

Yes, you can do that. I've done it before using Gradle but it has been a long time and I don't quite remember. I've been using Maven for the last year and it can also load files from the system as well. A good dependency management system gives you this flexibility.

Does this link help you at all?

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • This seems to go in the right direction. The question is then how to combine the file dependency with the build dependency. ==> If the file doesn't exist, run task X. Or, if any source file for the task that makes the file dependency doesn't exist, run that task. – treesAreEverywhere Aug 07 '14 at 16:20
0

If I have understood what you are trying to achieve on your second question, I would suggest a "libs" directory in project A containing the unity-classes.jar and then in your dependencies (build.gradle) this should find it..

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs') }

Perhaps keep a copy of the unity-classes.jar in the libs folder, so your project can build.

Hope this is of some use to you.

Jesson Atherton
  • 644
  • 7
  • 21
  • My problem is that this doesn't work because when the first time the project is built, the unity-classes.jar file won't be there "early enough" for the build of projectA to see it. – treesAreEverywhere Aug 08 '14 at 09:40
  • I so it's kind of a timing issue? So is the ProjectU actually building the unity-classes.jar or is unity-classes.jar a dependency of projectU? If it is a dependency then I would put a copy in the libs folder of ProjectA. – Jesson Atherton Aug 08 '14 at 12:10
  • have a look at this.. it's not ideal but it could make your issue workable. http://stackoverflow.com/questions/21293888/gradle-how-to-add-some-delay-pause-hang-in-gradle – Jesson Atherton Aug 08 '14 at 12:12
  • Hmm, well that will delay the build, but why would a delay fix this? Shouldn't dependent tasks execute sequentially? I think maybe my projectU has to place the jar it generates into a separate local directory that serves as a local repo. Then projectA will get it form there. But I would like to have the dependencies intact: If the sources of projectU change, it would rebuild the jar. And this should work from the gradle command line call that builds the android apk. It's called build automation after all :) – treesAreEverywhere Aug 08 '14 at 12:22
  • The issue ( or what I don't understand) seems to be that the jar dependencies are set up in the first configuration phase. So if I copy the jar file into the libs directory as part of a task, the compiler won't "see" them. – treesAreEverywhere Aug 08 '14 at 12:25
  • The purpose of the delay, would be that before projectA builds, it would give a slight amount of time for the .jar to finish. It just sounds to me like, the dependency is being called before its completed. but like I said it's not ideal, but could be a temp fix. – Jesson Atherton Aug 08 '14 at 12:34
  • The only other potential solution I can think of would be something similar to app engine. compile project(path: ':backend', configuration: 'android-endpoints') So it would depend on a build configuration as such. It should be outlined here somewhere http://www.gradle.org/docs/current/userguide/multi_project_builds.html – Jesson Atherton Aug 08 '14 at 12:35