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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Building an Android Plugin (C++ calling Java)

Discussion in 'Android' started by Phil™, Feb 1, 2011.

  1. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    I've downloaded the sample project, which appears to include all the source to the java and c++ parts of the plugin. So I now have a src folder which contains Android.mk, Application.mk, build_plugins.sh, JavaClass.java and NativeJavaBridge.cpp. At some point I'm going to modify some of the code in both parts of the plugin, but I can experiment with that on my own. Hopefully. What I'd like to know to start with is how do I *build* this plugin. I know the compiled parts are included (JavaClass.jar and libJavaBridge.so) but I want to actually learn the process by which I build them both so that I know how it all works before I start modifying any source.

    For reference, I have installed the SDK, the NDK and the JDK. I'm trying to do all of this on Windows. I have a feeling that I don't have all the necessary things installed. None of the items referenced (eg: ndk-build) are standalone programs or batch files, and the docs seem to just assume that I know what to do with them. So possibly, they're assuming I'm on Linux/MacOS.

    I've been digging through the docs to the NDK, but that expects me to have files that I simply don't have. I'm assuming that UT have helpfully included all the files I need to build the plugin, so I must be looking at the wrong stuff. They're probably building an application, which will (necessarily) be different to building a static lib.

    EDIT: I've installed Cygwin, and I think I've figured out the arcane syntaxt to run ndk-build, if indeed that's even what I'm supposed to be doing. It doesn't work. It says

    "Your APP_BUILD_SCRIPT points to an unknown file /path/to/src/jni/Android.mk"

    But my Unity src folder has the Android.mk file in the parent folder. Am I supposed to create a folder and move it there? Is it looking for a different Android.mk file?
     
    Last edited: Feb 1, 2011
  2. waruwaru

    waruwaru

    Joined:
    Jan 2, 2011
    Posts:
    30
    Cut n' paste the command and the output here would be helpful
     
  3. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Sorry, I went back and edited my post since then. I managed to find the syntax. ( I didn't realize that you needed to preface paths with ./ to indicate that you're starting from the current directory.) I didn't get much further though.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    ndk has several requirements actually, its not just NDK and Cygwin, but there are more things you need to setup and configure correctly.

    Google has pretty throughout documentation on how to setup the NDK and work with it, for you of interest is the setup part here, which you must follow completely or it won't work.

    That won't make the workflow less of a pain and its a shame google had the nerve to come out with this mess as their solution in the light of what WebOS, Windows Mobile 6.5, WP7 and iOS had in place long long before NDK appeared
     
    Last edited: Feb 2, 2011
  5. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Yes, I mentioned the JDK and SDK above. I'm pretty sure I have everything installed and setup right now or it wouldn't be complaining about files it can't find in the Unity-provided source code.

    The workflow is pretty shocking though, can't argue with that. I can't say as I've developed for Windows Mobile before, but the Android workflow is certainly pretty poor by comparison to iOS.

    I see there's a file called build_plugins.sh. I opened it with a text editor and it seems to be a script to compile. So I think I'm getting closer. How do I call this? Is it a script I'm supposed to give to bash?
     
    Last edited: Feb 2, 2011
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    ./build_plugins.sh in cygwin would start it, given bash is present in the cygwin installation


    as for the workflow: the path to Windows Mobile and especially Windows Phone 7 is another heap cleaner and easier than the iOS one. No provision signing idiocity and alike and MSDN is still the best documentation on globe out of my view (the lack of examples for not so logic functions and the sometimes pretty stupid useless stuff apple writes into its docs has cost me countless hours when doing native iOS development)
     
  7. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    I figured that would be the case with Windows Mobile. Apple's docs and provisioning let it down quite badly, but at least the documentation is actually there. With Android, it feels as though they're relying on third party software and other people's documentation for so much that you need to be searching in eight different places.

    Ok, well I've got it so that bash can run the script, and I've set the CLASSPATH to include the JDK bin folder, so it can now see JavaP, JavaC, etc. I now, however, get another error.

    Unable to find java.lang in CLASSPATH or BOOTCLASSPATH. I could try and find it and add it to CLASSPATH, but I'm thinking that it might be an indicator of something more fundamental. I expected the JDK would have set this up. Any ideas?
     
  8. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Anyone able to assist on this "Unable to find java.lang in CLASSPATH or BOOTCLASSPATH" error?
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    ANT is installed and configured?
     
  10. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    I only(!) have the NDK, the SDK, the JDK, Cygwin, numerous packages for the aforementioned.

    It says in the documentation that ANT is not required when building a static lib. It's only used to codesign and build the APK.
     
  11. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Looking through the build file, I found this line:

    javac JavaClass.java -bootclasspath $ANDROID_SDK_ROOT/platforms/android-8/android.jar -d .

    However, when I go to $ANDROID_SDK_ROOT/platforms/android-8/ ther eis no file called android.jar. Perhaps this is what I'm missing? How do I get that downloaded / installed? All I see in that folder is another folder called arch-arm.
     
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the whole folder there is managed by the android sdk manager, where you can update and install new platforms
     
  13. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    The Android SDK manager says I've installed every package which is compatible.
     
  14. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    then its not up to date or broken in some form.
    Because I have android.jar in all the platforms/android-x folders
     
  15. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Well I've attached a screenshot from my SDK manager. Am I missing any packages which you have?

     
  16. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I in addition have the Google APIs for all these API versions (they would in the list be after the 4 samples) as well as the Google USB Driver package (required if you want to debug on the device or push directly ... unless you have a HTC device, then its under HTC Syncs authority normally)
     
  17. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Yes, but none of those relate to the SDK platforms, do they? Anyway, I installed all of them and I still don't have Android.jar in the directory mentioned above.
     
  18. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    and ANDROID_SDK_ROOT points to the right place? the place where the SDK Manager you use is in too? if so you might have to make it delete all the platforms present and redownload them
     
  19. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    I'm not sure where that's supposed to be defined or how I can check it. It's not listed as an environment variable if that's where it's supposed to be.
     
  20. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Hang on, I might be confusing development kits.

    The Android NDK is in C:\Android-NDK-R5B\

    The Android SDK is in C:\program files\Android\Android-sdk-windows\

    Is that alright?

    There is an Android.jar in the SDK Platforms folder, but not in the NDK. So my earlier comment that the file referred to in the build file is missing is not true any more. But I still need to know how to check if the ANDROID_SDK_ROOT is correctly set.
     
    Last edited: Feb 3, 2011
  21. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Well I'm going to go back assuming that the Java CLASSPATH environment variable needs to be set correctly, because I can't find one, and it's the only error I'm actually getting. Is there a reference somewhere where I can find exactly what the CLASSPATH environment variable *should* have been set to when I installed the JDK. I'll settle for what it needs to include to find java.lang but all of them would be good. I'm assuming there are quite a few.
     
  22. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    I can't be the only person trying to compile a plugin, so someone must have a CLASSPATH which lets the JavaC compiler find java.lang. I've added the bin and lib folders of the JDK to a CLASSPATH environment variable I added manually, but they haven't made the error go away, so I can only assume they're wrong.
     
  23. BuzzJive_Legacy

    BuzzJive_Legacy

    Joined:
    Oct 11, 2010
    Posts:
    26
    Hey Phil. I've compiled plugins. My sdk manager looks similar to yours, other than me not having a couple of the latest ones. I do have the Google Usb Driver package in there too though.

    Some of my potentially useful Environment Variables:
    ANDROID_NDK_ROOT = C:\android-ndk-r4b
    ANDROID_SDK_ROOT = C:\android-sdk_r07-windows\android-sdk-windows
    CLASSPATH = .;C:\Program Files (x86)\QuickTime\QTSystem\QTJava.zip;C:\Program Files\Java\jdk1.6.0_21\jre\lib
    JAVA_HOME = C:\Program Files\Java\jdk1.6.0_21
    PATH = %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files (x86)\QuickTime\QTSystem\;%ANT_HOME%\bin;%JAVA_HOME%\bin;C:\android-sdk_r07-windows\android-sdk-windows\tools


    When I compile the JNI "bridge" - which is based on the Unity example bridge - my build_bridge.sh file has this command:

    javac src/com/blah/bridge/JavaClass.java -classpath "c:/Program Files (x86)/Unity/Editor/Data/PlaybackEngines/androidplayer/bin/classes.jar;./Blah.jar;./Etc.jar;./Suchnsuch.jar" -bootclasspath $ANDROID_SDK_ROOT/platforms/android-8/android.jar -d .
     
  24. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    Thanks for trying, but even after adding all of that, I still get "unable to find package java.lang in classpath or bootclasspath". I'm beginning to doubt whether the build script can even be used as it is.
    Is anyone actually able to compile the example plugin using the provided build_plugins.sh shell script?
     
  25. Phil™

    Phil™

    Joined:
    Jan 2, 2011
    Posts:
    73
    If I'm asking this in the wrong forum, or if I should be using email instead to get some answers from UT, I would appreciate a push in the right direction. Without actually being able to compile anything, Unity Android is something of a £1200 white elephant at present.
     
  26. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Edit: Was having same problem, found the solution.
    The problem (in my case, at least) was two-fold:
    1) $ANDROID_SDK_ROOT was not set correctly. And unhelpfully, javac doesn't give you a "missing file" message, but rather a "missing java.lang" message.
    2) javac will not include .jar files just from being in a folder that's in the path, you have to set the path to specifically include the .jar file.

    I have to say this is an area that could really use more documentation.
    Speaking of which, AFAICT, the jar file you need for com.unity3d.player is located at:
    Unity/Editor/Data/PlaybackEngines/androidplayer/bin/classes.jar
     
    Last edited: Aug 22, 2012
  27. Miyavi

    Miyavi

    Joined:
    Mar 10, 2012
    Posts:
    58
    I'm also feeling overwhelmed by the numerous steps you have to do.
    Two weeks trying to figure it out, but no luck yet.
     
  28. anthonyk2

    anthonyk2

    Joined:
    Sep 18, 2012
    Posts:
    47
    I had the same issue, my script was looking for

    C:\Program Files (x86)\Android\android-sdk\android.jar
    and not
    C:\Program Files (x86)\Android\android-sdk\platforms\android-13\android.jar

    Make some debug logs to be sure the script is pointing to the right jar location.