0

I'm building a NativeScript plugin and wrapping some functionality from a JAVA library. In most cases, I've seen users define a dependency with compile 'org.namespace:library:x.y.z' in src/platforms/android/include.gradle but in my case the library is not available in any JAVA repos and is a standalone .jar file.

I've tried some suggestions users have done with actual Android apps, but of course NativeScript is a little different and so far these methods aren't working.

Steps I've tried:

1) platforms/android/include.gradle

repositories {
  flatDir {
    dirs 'libs'
  }
}

dependencies {
  compile name: 'SimpleNetworking'
}

2) platforms/android/include.gradle

dependencies {
  compile files('libs/SimpleNetworking.jar')
}

Both attempts have ended up failing when testing this on a NativeScript app requiring this plugin as a dependency:

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all files for configuration 
':app:debugCompileClasspath'.
 > Could not find :SimpleNetworking:.
   Required by:
     project :app

The specific plugin I'm working to resolve can be found here.


Update

After reading this Android Studio Doc about build dependencies and changing the include.gradle file to look like:

dependencies {
  implementation files('libs/SimpleNetworking.jar')
}

It seems to have found the file! What appears to be broken now is something else:

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all files for configuration ':app:debugCompileClasspath'.
> Failed to transform file 'SimpleNetworking.jar' to match attributes {artifactType=processed-jar} using transform IdentityTransform
> Transform output file /Users/USERNAME/git/ons-testapp/platforms/android/app/libs/SimpleNetworking.jar does not exist.

I'm not sure if this is a related error or something new.

Pixxl
  • 815
  • 8
  • 17
  • Update -- I've adjusted the include.gradle to use `implementation files('libs/SimpleNetworking.jar` but have encountered a new error. I'll continue the investigation! – Pixxl Jan 31 '19 at 01:45
  • `flatDir` is not required for `JAR`. – Martin Zeitler Jan 31 '19 at 01:56
  • Yeah, it was a suggestion I had seen somewhere and tried. It didn't end up working. I switched to `implementation` and so far it seems to be included but now is not resolving for another reason. – Pixxl Jan 31 '19 at 01:57
  • 1
    this seems related: https://stackoverflow.com/a/53217799/549372 – Martin Zeitler Jan 31 '19 at 02:01
  • Yep that's what I ended up trying (see update in post) but it appears that the JAR file does not get carried over when setting up. I'm able to get it to work by manually pulling the JAR file into the app project. Not sure if this is now an error or an issue with NativeScript – Pixxl Jan 31 '19 at 02:05
  • Try directly placing your ^.jar or *.aar library directly in `platforms/android` as shown here https://docs.nativescript.org/plugins/Android-Plugins-Infrastructure#directory-structure . NativeScript should automatically compile the library and you could directly access it. – Nick Iliev Jan 31 '19 at 06:39

1 Answers1

2

You will just have to place your JAR / AAR files inside platforms/android, your plugin will automatically pick it up during compilation.

Manoj
  • 20,688
  • 3
  • 17
  • 36
  • The issue with that however is if anyone works on the project which uses this plugin in the future will have to make sure to never run `tns platform remove android` as then the entire library will be removed. This was part of the reason why it seemed like a better idea to abstract the JAR dependency to a plugin as I would have liked to not have that possible. – Pixxl Jan 31 '19 at 06:01
  • 1
    I meant the `platforms/android` folder within your plugin. Most plugins uses a jar / aar file in this way and it's never been a problem. I have tried it myself with a small [plugin here](https://github.com/manojdcoder/nativescript-dropbox). The `src-native` directory contains source code for my AAR file, which is being copied to `src/platforms/android/dropbox-release.aar` and same being picked by compiler without any further code / configuration changes. The procedure is similar for JAR. – Manoj Jan 31 '19 at 06:04
  • Hey Manoj, thanks for the help here! I think I was trying to use the `JAR` library too much like a normal native android app. The plugin was having problems referencing the file so I thought I needed to change *how* the `JAR` file was added and went down that rabbit hole. – Pixxl Feb 01 '19 at 02:44