Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Include CPP code for Android in Unity 2017.4

Discussion in 'Android' started by samuelstow, Mar 18, 2020.

  1. samuelstow

    samuelstow

    Joined:
    Mar 15, 2019
    Posts:
    6
    Hi,

    I'm trying to include C++ code in my Android plugin. It works as intended in 2018 and 2019 versions of Unity, but I note that 2017 versions don't support including raw C++ files in the Plugins/Android directory.

    In 2018+, when you include the code in the Plugins/Android directory, you can import them using [DLLImport("__Internal")]. How can I achieve this outcome in 2017.4, using for example compiled .so files or similar? Or does unity support .aar archives containing .so files?

    Cheers

    Sam
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    It does. This is the reference way for pre-straight-C-inclusion. Here's how I did it:

    - made a blank activity project in Android Studio
    - made an android library as part of that project
    - made a JNI folder in that library
    - stuck my C/C++ files in there to compile (beware of name mangling! use your extern "C" {} as necessary)

    When Android Studio project is built, it has an interim .AAR which I drop right into Unity.

    Also, you decorate declare it by the library name, like so:

    Code (csharp):
    1. [DllImport ("mylibname")]
    2. private static extern System.IntPtr dispatcher1_entrypoint1(int opcode1, int arg1);
    That's for a C function:

    Code (csharp):
    1. char *dispatcher1_entrypoint1( int opcode, int arg);
     
    samuelstow likes this.
  3. samuelstow

    samuelstow

    Joined:
    Mar 15, 2019
    Posts:
    6
    Interesting thanks! Where's the library name pulled from in this case?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I think it was just the name of the library I chose. I called the entire project vanilla, and the library within it vanilib. (I did NOT put that text above; I replaced it with 'mylibname' obviously). Here's a top-level shot:

    Screen Shot 2020-03-18 at 11.48.30 AM.png

    Unfortunately it does not seem to build anymore in Android Studio but I don't care because I always just do a
    ./gradlew clean assembleRelease
    inside the library folder and get my .AAR built and over to the Unity side that way, obviously into the Plugins/Android subdir.
     
  5. samuelstow

    samuelstow

    Joined:
    Mar 15, 2019
    Posts:
    6
    Thanks. I've managed to get an aar built with .so files using cmake in Android studio, but IL2CPP doesn't seem to recognize the library, or load it at least, because I'm still getting undefined reference errors.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Interesting. Try it with Mono... I have not upgraded that particular project to IL2CPP: it is a legacy one in Unity5 so I'm exempted from 64bit until April 2021.

    Another thing to make sure is that your native build targets are at least equal to all the CPU targets you're building in Android IL2CPP.

    Maybe I better start looking into 64bit for that project too... :/
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Two additional things to try:

    - make sure it's not being stripped out (do the whole link.xml thingy)

    - open your APK in something like ClassyShark and look in the DEX files for the namespace of your library.