I've been running the Android SDK for a while now in Eclipse (MAC OSX). I've downloaded the NDK and installed the C/C++ tools in Eclipse, but could anyone guide me on using the NDK? For example, do I just create an Android project like normal and build it with the NDK instead?
Really could do with a decent tutorial if anyone know of any.
EDIT: OK so I have the NDK installed now (I think) but does anyone have any idea how to use it? I got as far as this (taken from here):
Run Terminal
cd ~/android-ndk-1.5_r1
make APP=hello-jni
In order to run the hello-jni sample application, but I get an error in terminal saying:
Android NDK: APP variable defined to
unknown applications: hellojni
Android NDK: You might want to use
one of the following:
build/core/main.mk:81: *** Android
NDK: Aborting . Stop.
Any ideas why?
As simply as I can describe it, building an Android app from within Eclipse that uses the NDK requires two steps.
First, inside your terminal you need to run the NDK build script on your project. cd into the root of your project directory and then execute the ndk-build script within that directory.
For example:
cd ~/workspace/hello-jni
./~/android-ndk-1.5_r1/ndk-build
After doing this, you should see some output that results in the creation of a *.SO file within the obj directory within your project directory.
Once you have the *.SO file, the final step to building an application with the Android NDK through Eclipse is to build it with Eclipse like you would any other application and then deploy it for testing.
If you make any changes to the C/C++ code you'll need to repeat step one and regenerate your *.SO file before building and deploying your application from within Eclipse again.
I would like to note that by using the Android NDK your android apps are still based upon Java. They're just communicating with code written in C/C++ by way of the Java Native Interface.
Finally, I am not aware of any Eclipse plugins that will aid with NDK development. Everything I know about the NDK I have learned the official Android NDK documentation. Please feel free to comment and let me know if there anything I can clear up in my response.
Native development and debugging support came into Eclipse environment as of ADT version 20. http://tools.android.com/recent/usingthendkplugin
Set path to NDK from Eclipse Preferences -> Android -> NDK
Right-click on your project and choose Android Tools -> Add Native Support
developer.android.com states you also need Cygwin.
http://developer.android.com/tools/sdk/ndk/index.html#Contents
Required development tools
For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
A recent version of awk (either GNU Awk or Nawk) is also required.
For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.
The docs directory in the NDK has some pretty good information on how to use the NDK itself. Read the overview, Application.mk, and Android.mk HTML docs. You'll want to google for the Sun JNI PDF, download it, and learn what JNI is all about before you go any further. This is because simply compiling a bunch of C/C++ code into libraries with the NDK is only part of the process. You have to write native Java code that calls your C/C++, and you have to create wrapper functions in C/C++ that adhere to JNI conventions that the native Java code can invoke. JNI has been around a long time, it's not Android specific by any means. So, you can, to learn about it, go quite far following tutorials geared towards JNI, using command line tools like javah and javac, and then return to integrating with the NDK after you know the basics. (For an example of what these C shims look like, take a look at the hello-jni sample in the NDK; the C source file there shows you typically what the shims look like. Using javah to generate these shims is the way to go, you create Java classes that have native methods, process them with javah, and it generates the C headers for you, then you code up C functions that adhere to the generated function prototypes).
Note: while the NDK docs would have you manually building from command line and then going into Eclipse to build your app (a laborious sequence of steps, to be sure, especially if you are changing the C/C++ code), it turns out you can integrate easily with Eclipse so that the NDK is run each time you build from Eclipse. To see how, read here.
This is for benefit of others who want to create the project from scratch from within eclipse: I followed steps mentioned here in this blog here and it works fine: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/
To attempt to answer the question directly - you need to run ndk-build in the folder with the native code within your project folder. This creates the .so files found in the file explorer/resources tree under jni in Eclipse. These functions, if the syntax in the code is correct, can now be called from your java code.
I found many sources of help when install and getting to grips with Android Developer Tools and the NDK. I wrote a blog post to share my experiences and hopefully give back to the community that helped me get there which may help understand my answer: http://workingmatt.blogspot.co.uk/2013/03/set-up-android-sdk-and-ndk.html
Related
Is there a way to build AOSP's /platform/frameworks/base package source or support library source separately in Android Studio.
I wanted to read the framework (base and support) code in Android Studio and since I didn't want to be bothered about the rest of the packages in AOSP, I only imported the mirrored repo of these two packages as two separate projects in Android Studio. I want to browse through the code by using "jump to source" shortcut (cmd/ctrl+click) of the IDE. But this handy feature won't simply work properly in Android Studio. When I try to jump to the method definition which belongs to a different java source file (It says - "Cannot find declaration to go to"). This limitation of source editor is due to gradle build failure. The project just won't build and throws the following error.
Error:You need a symlink in prebuilts/sdk/99 that points to
prebuilts/sdk/current.Without it, studio cannot understand current
SDK. ln -s ../../prebuilts/sdk/current ../../prebuilts/sdk/99
Is there a way to fix this. I know I can simply browse the source code on http://androidxref.com/ too, but it's not as convenient and powerful as your IDE.
This is possible (having Android Studio be able to show you the framework code and let you use the usual IDE tools), though compiling won't work.
But the detail in your question seems to indicate you don't actually care about compiling, just about viewing/editing.
The first steps listed here:
http://ronubo.blogspot.com/2016/01/debugging-aosp-platform-code-with.html
might work for you. Basically:
Build your AOSP platform
( . build/envsetup.sh && lunch - && make ...)
Create the Android Studio project for importing to
( mmm development/tools/idegen && development/tools/idegen/idegen.sh )
Import the project
( open android.ipr from Android Studio)
Not without a huge effort.
For very many reasons.Ex:
1.the framework depends on different HALs and libraries from system/bionic/external
2.Compiling the framework uses specific tools from linux(flex,bison,libgl etc..)
3.Framework is built using the Android.mk system and needs a lot of flags and compilation macros from build/
I also can't imagine a motivation to do what you are trying to do. If it's going to run on any device, bare in mind device manufacturers modify the framework heavily, even the small ones. Actually the chipset manufacturers modify it and give it to manufacturers that modify it even more.
Error:You need a symlink in prebuilts/sdk/99 that points to prebuilts/sdk/current.Without it, studio cannot understand current SDK. ln -s ../../prebuilts/sdk/current ../../prebuilts/sdk/99
This notice already tell you should run the command:
ln -s ../../prebuilts/sdk/current ../../prebuilts/sdk/99
To tell build system use 'current version sdk' in prebuilts/sdk/current directory replace the temp sdk version 99.
could you try it?
I tried to build this project using JetBrains IntelliJ IDEA but it was unsuccessful (there were a lot of errors). The project has an Android.mk file, but I didn't see the C++ code.... How can I build this project? I've tried to use another IDE, then I tried building from the console but it was not useful. Perhaps, does Launcher3 need NDK to build it?
You'll need to isolate the launcher's code and then identify all of it's dependencies. If the code has any C++ code then it definitely requires the NDK and currently IntelliJ/Android Studio does not have any stable support for NDK based app development (though it is a work in progress) else if it is pure Java (most probably) then you'll have to manually add the classes and build the app from Android Studio! Either way you will have to dig deeper into the source and know for yourself. Unfortunately there seems to be no easier option, for now. I wouldn't mind being proved wrong for the matter.
EDIT :
I glanced through the code and I noticed there are no C++ source files or library (*.so) files. This isn't a gradle project and hence cannot be built as one. You'll need to setup a gradle based Android project and then begin adding the sources (src, tests etc.) and their dependencies (res etc.). You can start of here : Getting Started with Android development if you're new, else you can move to this : Gradle and Android
Sorry it's my first time with cross-platform programming and I spent 4 hours for naught this afternoon.
I currently have a cocos2dx win32 project. I can run it on Visual Studio. My mentor told me to find a way to convert it to an android project. I have followed the instructions to set up the android environment, so I have: the recommended ADT (modified eclipse), NDK, and cygwin. Honestly I do not really know what NDK and cygwin is for. I have never played with linux either. Is there an informative page for how they work? All the pages I found are how-to-set-up-environment.
I think I set up the environment just ok, because I built a default helloworld project and ran it on the ADK emulator. However, I find no way to convert the win32 project I have to an android project.
I also found that in the folder of my project there is a .bat file called create-android-project.bat. And I followed some instructions from online to set up the three parameters for this batch file: the package path, project name, and target id. However the batch failed with errors like "No such file or folder...". I tested with other win32 projects I have. Ended up with similar errors.
Interestingly, if I ran this batch in the original cocos2dx project (the one with samples, templates, and built multi-platform projects), the batch ran just fine.
I really don't know what to do now. Can any one give me a solid guide page on this? Thanks a lot!
You need the classes and resources folder in the project created. Once done, you need to add all the CPP files and the includes folder in Android.mk file and build it using a script available build_native.sh
For cocos2dx v3, you need to run cocos compile -p android from within the project's directory. You need to make sure to have your JAVA_HOME path set correctly in your environment variables.
I can't say for sure that it mattered, but I had run proj.android/build_native.py as admin through cmd.exe before hand.
I have successfully converted my Cocos2d-x 2.1.5 project to Android project with the reference with this link. And I conclude some problems I met in my Blogger. I also found an official tutorial
To conclude:
Setup Java Build Path
Setup Builder Settings, I didn't use cygwin but using ndk_build
Setup NDK_MODULE_PATH
Copy the Resources
Modify Android.mk
I am building C libraries that will be used by an android app(through either JNI or the NDK, I've never used the NDK though) and I am wondering what IDE/development environment is best to use? I've basically only used Visual Studio for native development and I'm not sure how to adapt to building .so libraries for use on android.
Thanks
EDIT: Update -- I want to avoid makefiles if possible (never used them) and rather use a complete IDE such as the case with visual stuido to compile
Generally, cygwin (i.e. command line) is used for building Android native code. But you can also try to use Eclipse for this purpose. It doesn't help very much but it highlights syntax and you don't have to switch to other apps to write code and build your app. You can read more about using Eclipse for C/C++ Android development here.
I would strongly recommend that you use the NDK's compiler.
Normally this is done by writing an Android.mk in accordance with the instructions for the NDK. It is not complicated. The easiest thing to do is to grab the hello-jni sample app and just make the necessary changes, for example substituting your source files into its android.mk.
You can stay in visual studio if you like that as an editing environment, simply using the ability to bind an external command to a keystroke to launch the ndk build script. Ages ago I had figured out how to reformat GCC error messages with sed into something that VS could parse to make them click-able, though I soon moved development of that project under linux.
I think a lot of people use Eclipse as their IDE for Android development. I use it and its pretty good. You shouldn't have any problems if you're used to VS.
The Android SDK comes with a plugin, the Android Development Tools (ADT), to develop applications for Android inside the Eclipse IDE, which is same kind of beast as Visual Studio. And within that IDE, it is possible to use JavaCPP (disclaimer: I am the author) to have it compile all the native C/C++ stuff we need through the Android NDK, but without needing Makefiles and such. More details here:
http://code.google.com/p/javacpp/#Instructions_for_Android
I'd like to build some native libraries for android using the NDK (native development kit), but i'd like to use Visual Studio 2010 to do it. I've googled quite a bit but haven't found any information on it. Does anyone have any experience with this and know the steps necessary to make this happen? I have CYGWin installed, made sure i get Make (per the NDK instructions), but i'm not really sure of the next steps in terms of setting up the project, compiler in visual studio, etc.
If anyone knows of any write-ups, tutorials, or links to sample projects, that would be awesome, as there isn't much on google yet.
thanks!
Here's another solution, which integrates the NDK fully within Visual Studio. No makefiles. It behaves like a proper Win32 project:
Here's an excellent blog post about how you can configure your environment to debug android NDK code using Visual Studio.
Visual Studio is officially not supported.
Some problems (but not limited to):
MSVS cannot create the proper ARM binaries
Android makefiles (.mk) are not supported by MSVS
There are however, third party solutions:
vs-android
VisualGDB
You might want to check out DS-5 as well, though it's not Visual Studio.
The answer depends on what kind of integration you require.
To just build the native Android code from Visual Studio you can create a new Makefile project, and make it run ndk-build.cmd when you press "build". If you would like to get the error messages mapped as well, you will need to parse the output of ndk-build.cmd and convert it to a format that Visual Studio can udnerstand.
If you want to debug your native Android code from Visual Studio, you will need a third party tool that will control ndk-gdb on behalf of Visual Studio and provide workarounds for several bugs (e.g. rebind breakpoints when libraries are loaded).
You can try our VisualGDB for Android tool that does exactly that - creates projects that wrap ndk-build and controls NDK debugger on behalf of Visual Studio. If you need more information, there is a step-by-step tutorial available.
I have not found a direct clean solution, here is my workaround.
I develop my native code on VS as a static library, and use some test project to try it as a console aplication.
When it is ok, from cygwin I use a little bash script that copies all needed files to the jni folder and launch the standard android ndk make command. (also copy some file to assets folder when needed), producing the executable in the right folder.
To use the pthreads I have linked my projects to pthreads-win32.
The only files I do not compile in VS are the jni code.
I hope this can help you.
There's also a couple of other third party alternatives for developing ANdroid apps with VS in addition to those above. There is the open source dot42 located at http://www.dot42.com and Remobjects, http://www.remobjects.com , which allows you to program in c#, Pascal, and Swift. I have played with the trial of Remobjects and am now starting to play around with dot42.
My use of the remobjects trial allowed me to recreate in c# the first five chapter projects in the textbook of the Android course I took. I haven't had the same success with dot42 so far but I need to find some time to really give it a chance.