Search Unity

Android files in jar library not accessible when using with Unity - Please help

Discussion in 'Android' started by andreasvourkos, Dec 24, 2013.

  1. andreasvourkos

    andreasvourkos

    Joined:
    Dec 20, 2013
    Posts:
    17
    Hello everyone,

    I am new with Unity. I have an Android library jar that I want to offer also as a plugin with Unity.

    Within the library jar i have a folder html which contains a file temp.json. In the jar file (if i unzip it) the structure is like this: html/temp.json (Note here that jar works fine when running with any Android app outside Unity)

    These is the steps that I followed to call my library through Unity:

    1) created folder Assets/Plugins/ Android in my Unity project hierarchy
    2) Placed my library jar file there along with AndroidManifest.xml
    3) Created a bridge.cs file that I use to call the functions in the jar file

    Functions from the jar are called, but within the code of the jar somewhere on the Android side during the library lifecycle I call:

    InputStream inputStream = MyClass.class.getClassLoader()
    .getResourceAsStream("html/temp.json");

    and I get a ResourceNotFound exception

    From what I have read Unity ignores these files(like .json) when packaging. Therefore from what I understood in order to keep them in the Jar when packaging I created the following Unity structure:

    Assets/Plugins/Android/assets/StreamingAssets/html/temp.json

    I realise that any file that is placed within the StreamingAssets folder get copied in the assets folder of the library.

    and in java I call now:

    inputStream = activity.getResources().getAssets().open("html/temp.json");

    however I still get an exception that the file is not found.


    Can anyone please help me/explain to me the procedure I have to follow to be able to read these files on the Java side while executing on Unity for Android?

    Thanks in advance for your time.

    Cheers,

    Andreas
     
  2. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    Copy your file temp.json to: Assets/StreamingAssets

    On Android you need to retreive this file with something like this:

    Code (csharp):
    1. InputStream in = context.getAssets().open("temp.json");
     
  3. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
    AFAIK Unity should include everything that is in Assets/Plugins/Android when you build the .apk

    Only limits with the plugins that I remember is that you cannot use R for finding the resources and that you cannot merge certain plugins to same Unity project since they use same filenames for resource files that they want.
    http://www.twodee.org/blog/?p=4951
     
  4. vladimirg

    vladimirg

    Joined:
    Jun 26, 2013
    Posts:
    43
    @Agent_007, at least in Unity >= 4.2, the R issue is no longer a limitation. If you include your res subdirectory in the plugin (instead of putting it in the .jar), Unity will do the correct thing and compile the R class with all your symbols from the above res subdirectory.
     
  5. andreasvourkos

    andreasvourkos

    Joined:
    Dec 20, 2013
    Posts:
    17
    @vladimirg @Agent_007 @jvil thank you for your responses. What if I have my files in my html folder? If I put them in Assets/Plugin/Android they get included in the jar like Assets/Plugin/Android/html or this happens only for res/ folder?

    And do I have to make changes in my Android side code or can I still reference this way with the same code like:

    InputStream inputStream = MyClass.class.getClassLoader()
    .getResourceAsStream("html/temp.json");

    Thanks in advance for your time!
     
    Last edited: Jan 6, 2014
  6. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    105
    Hi vladimirg - I'm curious how you found this information? It was a lifesaver, but I haven't been able to find it documented anywhere! Thanks so much for pointing this out, I have no idea how much longer I would have scoured the Internet for any reasonable answer.
     
  7. vladimirg

    vladimirg

    Joined:
    Jun 26, 2013
    Posts:
    43
    @rchapman, it's not very well documented. A good approach is to take a working plugin (e.g. Facebook) and look how they do things. Eventually the pieces of the puzzle fall together. Another solution is to ask on the forums :)