In android studio (1.1.0) how to add dependency on java file from other source?

I am trying to include the C library. I have an NDK part. However, the library includes the Java interface as a package that imports the top-level application file. The assembly cannot find the import of this package. I think I need to add a dependency, but cannot find how to do it. I don't want to just copy the package to the project because I hope to have multiple projects using this library and this is just not a good coding practice. It seems like I should add the classpath somewhere, but gradle / Android Studio seems to override everything I've tried. Anyone have any suggestions? Many thanks. Chuck

+3


source to share


3 answers


If you are trying to import the .jar library have a look at the build.gradle file (application level). From there you will see a "dependencies" box, which should look like this.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // other dependencies should be added here
}

      

Just add the following line to the dependencies box:

compile files('PATH_TO_YOUR_JAR_FILE/YOUR_JAR_FILE.jar')

      



The path to your .jar file can be either absolute or relative (relative root is the "application" module).

Finally, you will need to sync your project with the Gradle files for the changes to be committed to the account.

If that doesn't work, can you be more specific about how you compile your C library using Gradle, or are you using the Android.mk file?

0


source


Okay, I gave up. At this point, I don't know if there is a way to include the .java file from somewhere other than the application directory. So I copied the directory structure (org / x / y / something.java) in my app's / src / main / java directory and built it completely. Then I had to copy the relevant lib files (* .so) to the appropriate directory (in app / src / main / jniLibs / armeabi - built ndk-build APP_PLATFORM = 9) to build and it works. I don't know what I would do if I ever had to change this Java package and it was included in several projects! If so, it seems like a huge limitation in Java.



0


source


One solution would be to automate a copy of external files before each compilation, so you will always have the latest version when you compile your project.

To do this, you can easily create a Gradle task that runs an OS command such as "cp" (see this page for more details).

Once you have configured the copy task, you need to add a dependency between it and the compilation task by adding the following to your build.gradle (application level) file:

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn NAME_OF_YOUR_COPYING_TASK
}

      

0


source







All Articles