__ANDROID__ macro suddenly not defined - android

I'm working on an app which uses NDK (all I'm writing happened both on r6b and r8d)
Everything was working fine, and I wanted to start and try debugging my C code.
I followed this http://tools.android.com/recent/usingthendkplugin tutorial,
but NDK_DEBUG = 1 tag to my build command, suddenly I started getting errors in the code which didn't go away even after removing that tag, changing from Android 4.2.2 back to 2.2, changing the NDK I was using, or anything else I could think of.
The problems happens now inside statements like this
#ifdef __ANDROID__
some cool android code
#else
some pretty awesome iOS code
#endif
what happens it that the __ANDROID__ is for some reason not define, causing eclipse and ndk-build to try and compile the iOS code instead of the Android's
Reverting everything I did didn't seems to have any effect. Restarting eclipse didn't as well. Cleaning the project, completely delete libs and obj directories didn't work too..
Any suggestions?
Thanks!
EDIT:
Maybe it's worth adding that the build itself, using ndk-build completes successfully.
I think it might be an eclipse issue, but even if so, it still an error and I can't launch the app
Also, just in case, restarting the computer didn't work either.
EDIT 2:
The problem exists on another computer running the same workspace over network, my guess was something related to the workspace, so I tried deleting .metadata folder and adding the project again.
Deleting the .metedata folder fixed it the 1st time, but after a few minutes (in which I managed to build and run the app on my tablet) the same issue returned, and deleting the .metadata didn't work
EDIT 3:
Still no go.
However, I can confirm that it's not a project specific problem, as all the projects that has Native support in eclipse now do this.
Other things that doesn't work:
creating an empty project, adding Native support.
Completely changing to another unrelated workspace and perform the above tests
Downloading fresh version of eclipse (juno), CDT & ADT (was using the eclipse ADT bundle)

Just so you know:
when you work with Android.mk file(s) and ndk-build, the ANDROID macro is predefined (see -DANDROID extra C flag when building with verbose outputs),
but if you use the Android Standalone Toolchain, then __ANDROID__ is predefined instead.
So I suggest you to use:
#if defined(ANDROID) || defined(__ANDROID__)
/* ... */
#endif

Apparently __ANDROID__ is a specific GCC macro that it supposed to define internally whenever correct options are provided. However, since control over options is largely delegated to NDK, one should not rely on __ANDROID__ macro being ever defined. The compiler behind NDK might not be GCC for all we know (or care). When working with NDK, check for ANDROID.
Edit: clang now also defines __ANDROID__ macro

If anyone else encounters this issue:
I opened the workspace on another computer (Workspace is in a Dropbox folder), and the problem was still there on the other computer, which could only mean a workspace issue, so I delete the .metadata folder from the workspace.
I had to re-add the projects, but after doing so, everything seems to work now.

Related

ndk-build installs libraries even if no change. Can this be changed?

I'm using the Native Development Kit (NDK) in a project of mine, and I'm trying to automate the whole app build procedure with Python.
Whenever ndk-build is called, it copies the prebuilt shared libraries to libs/<abi>/, even if there's no changes in them or they already exist there. This causes problem when I call ant later on, as it detects changed files (the library timestamps are newer) and so rebuilds the apk without any need.
Is there a way to change the ndk-build behaviour so it checks for existing libraries in the libs/<abi>/ folder and if they need updating or some are missing, it will call ndk-build, otherwise, just proceed to the next build step?
I've tried using filecmp in Python, but as the timestamps are different between the prebuilt shared libraries and the installed ones, it doesn't work.
The OP probably doesn't need this any more, but I had the exact same problem, trying to set up a Makefile to build a project, so maybe this will be helpful to someone else in the future as well.
ndk-build is a wrapper around gnu make, that invokes a bunch of Makefiles in build/core directory of the ndk, so, while it's not universally applicable*, for your personal project you can modify those Makefiles to do whatever you want. I found a clean-installed-binaries target that a couple of build/install targets depended on, removing those dependencies fixed the issue with perpetual installs.
In whichever cases that clean target is necessary you can invoke it manually with:
ndk-build clean-installed-binaries.
*Given the time to come up with a clean opt-in solution you can submit a patch to ndk project, and if accepted it will eventually become universally applicable.

Eclipse not updating "built-in" include paths for Android NDK Project after switching to GCC 4.8

I have an Android/Eclipse project that uses the NDK and I've recently enabled c++11 support by adding the following to my Application.mk file:
NDK_TOOLCHAIN_VERSION := 4.8 #same result here with clang
APP_CPPFLAGS += -std=c++11
This basically works fine, c++11 features are available and compile as expected. This goes for ndk-build and Eclipse builds (which just invoke nkd-build), but the eclipse code parser becomes confused now. When I open a file that uses types added to the STL in c++11 (like for example std::unique_ptr), I get red squiggles and an error entry in the Problems tab saying Symbol 'unique_ptr' could not be resolved. This wouldn't be so bad, but if errors are present in that list Eclipse refuses to launch (or debug) the application. Right-clicking on the #include <memory> line and selecting Open Declaration also opens the wrong (4.6) file.
When checking the project properties under C/C++ General --> Paths and Symbols --> Includes it still lists the old (4.6) includes when Show Built-in values is ticked: Screenshot
Are those just cached from somewhere and I can get it to re-generate those entries? I've obviously tried to Clean and Rebuild the project, with no effect. I'd rather not add them manually if that can be avoided, upgrading the NDK to new versions already requires quite a few changes until it compiles again. Where are these entries generated from and how do I trigger an update?
I wish I could say I have a definitive answer for you, as I've been battling this problem for quite a while myself, but I do have a decent workaround. For starters, the "built-in" entries under "CDT Managed Build Setting Entries", are generated based on the contents of your Application.mk file which is located in the jni directory. There is no other way of changing them, and these are what populate the "Includes" section in the C++ perspective's Project Explorer. But I see that must have read the documentation.html in the NDK installation to see what to put in that file already. The issue then becomes that as soon as you change something or breathe too hard, your includes suddenly and irreversably become unresolved. The workaround for this is to recall the exact path of each include entry, right-click on the project, go to New->Folder, then in the dialog click Advanced, and select Link to Alternate Location (Linked Folder). Then navigate to the path (or extend a variable if you are fancy) of the include folder, rename it something descriptive (you can't have 4 linked folders named "include"), and voilà! You suddenly have a usable workspace again. I guess the "Includes" section functionality is a bit flimsy still, at least for ADT, and doing this puts the includes on the same footing as other files that are actually in your project. I'd be thrilled if the officially sanctioned mechanism started to work reliably, and I eagerly await news of such. HTH!

Mac Eclipse not resolving C++ lib functions in NDK

I'm developing a native application using Eclipse for Mac and I can't get std::string or std::map to resolve.
I've already had to add a number of include directories to the path to handle the NDK not being set up that well out of the box; currently I have
.../Development/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include
.../Development/android-ndk-r9/platforms/android-18/arch-arm/usr/include
.../vuforia-sdk-android-2-6-10/build/include
.../android-ndk-r9/sources/cxx-stl/system/include
set as C++ symbol paths, and #include <string> seems to have worked fine. However, all the references to std::string are not resolving, and Eclipse says
Type 'std::string' could not be resolved.
I know this code builds because I successfully built it on a Windows PC; it's an eclipse problem of some description. What can I do to make these symbols resolve?
EDIT 1: I've looked inside <string> and it seems that it mainly contains a bunch of other includes, for example #include <bits/c++config.h>. All of these seem to resolve, but I am going to see if I can find them and determine where std::string actually lives.
EDIT 2: I can find std::basic_string, but not std::string (yet).
EDIT 3: std::basic_string is not apparently visible to my source code either.
Im not sure if this will be an answer for you. My experiences are different, I'm on Windows, and I was only trying to get C (not C++) code working. But ran into exactly the same issue. And my solution could seemly be used to work for C++ as well. As far as I can tell, Eclipse/ADT on Mac is much the same as on Windows. So that shouldn't be a problem here. Who knows, I could be talking out of my a--.
However, I think the problem is solved two fold. Here is how I solved it. I followed the tutorial: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/
Things to note:
1) When developing in Eclipse/ADT for Android. It always assumes that your writing in Java. You need to make the project "Mixed Java & C/C++" (not that I've seen it called this outside the tutorial, lol)
2) Your ndk-build should probably work fine from the command line (even tho it might not compile in Eclipse).
3) In the tutorial, the two things that seemed to get it to recognize C/C++ are "Step 5: Convert to a C/C++ Project" followed by Steps 6-8 (which seem like one big step).
4) In Step 7, under "Build command". You might have to use a the complete path (no spaces) to your ndk-build. (*Windows users enter "ndk-build.cmd" here)
5) In Step 8, also include a full path.. no spaces. However, this is where you would put a path in for "GNC C++" instead. (This is my guess for your solution)
6) If all else fails... It should work in C. Try Step 8 exactly, and use only C syntax. Because, at the top of this older tutorial (http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/) it talks about how limited NDK is. It says C++ functionality is limited. Otherwise, this second tutorial is mostly obsolete it seems. Cygwin and command line compilation are not needed anymore, and my Eclipse installation had the C/C++ support out-of-box. However, its all still usable/valid.
PT
(and on a similar note, and for those seeking help with NDK. Also make sure a space-less full path to your NDK folder is entered in at Eclipse > Window > Preferences > Android > NDK > NDK Location)

Eclipse compiles successfully but still gives semantic errors

NOTE: it apparently is a recurrent question on StackOverflow, but - for what I have seen - either people never find a way or their solution does not work for me
The problem:
I am using Eclipse Juno ADT. Everything was working fine until I tried to update the NDK. I replaced my ndk folder (that was the ndk-r8d) by the new version (i.e. ndk-r8e) and, in my Paths and Symbols configuration, I changed the includes to go from g++ 4.6 to 4.7.
It seemed to break my index: I could compile my code, but Eclipse was giving semantic errors, exactly like in [1] and [2]. The errors mainly come from symbol used by OpenCV4Android, such as distance, pt, queryIdx and trainIdx.
When I tried to backup to my old configuration, the index was still broken! I cannot find a way to change this.
What I have tried
Clean up the project
Rebuild, refresh, and all the other options in the "Index" submenu (when "right-clicking" on the project)
Disable / enable the indexer in the preferences
Verify that symbols such as trainIdx only appear in my OpenCV4Android include in the Paths and Symbols section.
Change the order of my includes in the Paths and Symbols section. I basically tried to put the OpenCV include in the beginning and in the end.
Some observations
What is not working
I assume that it is the CDT index because of the following:
In command line, I can build my project using ndk-build clean and ndk-build.
When I start Eclipse, I have no error until I open a C++ file (from the jni folder).
I can always build the project, but as long as I have opened a C++ file, I can't run the application anymore because of a lot of Field '<name>' could not be resolved.
If I don't open the C++ files, Eclipse doesn't report any error and can build and deploy the Android application successfully.
Interesting fact
The following code reports errors on line, queryIdx, pt:
cv::line(mRgb, keypointsA[matches[i].queryIdx].pt, keypointsB[matches[i].trainIdx].pt, cv::Scalar(255, 0, 0, 255), 1, 8, 0);
If I write it as follows, it works:
cv::DMatch tmpMatch = matches[i];
cv::KeyPoint queryKp = keypointsA[tmpMatch.queryIdx];
cv::KeyPoint trainKp = keypointsB[tmpMatch.trainIdx];
cv::line(mRgb, queryKp.pt, trainKp.pt, cv::Scalar(255, 0, 0, 255), 1, 8, 0);
It is not that all of the OpenCV functions are unresolved: only pt, queryIdx and trainIdx are.
Any comment will be really appreciated.
In your selected project preferences within the Eclipse environment, go to C/C++ General -> Code Analysis -> Launching. Make sure that both check boxes are unchecked. Close and reopen the project or restart eclipse and rebuild the project.
Since indexing for Android native code on Eclipse is incomplete, I managed to enable indexing in my NDK projects the following unintuitive way, it should work whether you use ndk-build or plain make or even cmake. I'm using Kepler but it should work on older versions too.
Get your toolchain right
Right click on project -> Properties -> C/C++ Build -> Tool Chain Editor -> Uncheck Display compatible toolchains only.
In the same window, set Current toolchain to Linux GCC.
In the same window, set Current builder to Android Builder if you're using ndk-build, set it to Gnu Make Builder otherwise (this step may be wrong, sorry in advance if it is).
Right click on project -> Properties -> C/C++ Build -> Build Variables -> Make sure Build command reads the correct command for your project; if it's not, uncheck Use default build command and correct it (it may be ndk-build or make -j5 that you want). If you build the native code in a separate terminal, you can skip this step.
Make a standalone toolchain, it's probably the cleanest way to get STL sources in one place
Go to the NDK root directory.
Run the following (tweak the settings according to your liking). Add sudo if you don't have write permissions to the --install-dir because the script fails silently.
./build/tools/make-standalone-toolchain.sh \
--platform=android-14 \
--install-dir=/opt/android-toolchain \
--toolchain=arm-linux-androideabi-4.8
This is assuming that you use GNU-STL. If you use another C/C++ library, you will need to tweak the above command, and probably also the include paths in the next command.
Add the necessary include paths to your project
Right click on project -> Properties -> C/C++ General -> Paths and Symbols -> Go to the Includes tab -> Select GNU C++ from Languages -> Click Add and add the following paths (assuming you installed the standalone toolchain to /opt/android-toolchain):
/opt/android-toolchain/include/
/opt/android-toolchain/include/c++/4.8/
/opt/android-toolchain/include/c++/4.8/arm-linux-androideabi/
/opt/android-toolchain/lib/gcc/arm-linux-androideabi/4.8/include/
/opt/android-toolchain/include/c++/4.8/backward/
/opt/android-toolchain/lib/gcc/arm-linux-androideabi/4.8/include-fixed/
/opt/android-toolchain/sysroot/usr/include/
Here, you can add every include path you want. In fact, I have my OpenCV built for Android and installed in the standalone toolchain, so I have the following include there:
/opt/android-toolchain/sysroot/usr/share/opencv/sdk/native/jni/include/
Now, the indexing should work. You should also be able to run ndk-build (or make if that's your build method) and then deploy your project to your device inside Eclipse.
Why?
Android native development on Eclipse is incomplete since the indexing doesn't work out of the box. This is due to having to support multiple architectures (ARMv7, Intel etc.), multiple STL options, multiple Android versions etc. This is why you have the bare make based ndk-build and the whole NDK structure, and this is also why NDK development is very unclean and few large volume native Android projects exist.
A big Android project is OpenCV where they had to develop a 1500 odd line CMake script to get it to compile for Android properly. At some point, they tried to export that script as a CMake based build system for Android but it couldn't keep up with the changes in the NDK system and was abandoned. This support should have been inside NDK itself.
The default NDK build system should have been standalone toolchain only, with all different architectures/C++ libraries having their own toolchains at the cost of storage space but with the advantage of cleanness, intuitiveness and good practice. Then you can incorporate any standard cross-compilation system that is also used elsewhere, is tested and is well-known, e.g CMake. You can, and in my opinion you should, do that with the NDK's make-standalone-toolchain command as shown above. But in the end, this is only my opinion. If you feel comfortable enough with ndk-build then go ahead.
It's actually quite hard to say what is the problem. Here are some advices:
Try to import and build hello-jni (it is located in jni's samples folder). If it runs without problems than problem is with linking OpenCV to your project.
It seems that you forgot to update android-ndk location in project properties -> c/c++ build -> environment. Here's link to problem Issue with build Android NDK project.
Build from console your project (ndk-build -B), delete all errors in Eclipse manually (in Problems view select all errors and just click delete) and try to run project now. Sometimes this "hack" helps me to run project.
Close Eclipse and delete folder path-to-your-workspace/.metadata/.plugins/org.eclipse.cdt.core (backup it first).
Go to Preferences > C/C++ > Language Mapping > ADD (Source C File and select GNU C) Do the same for C++
I had the same issue. I had all the proper include paths setup but after opening the .c/.cpp or .h file and it would start marking everything as "Unresolved."
This worked for me...
Go to:
PREFERENCES -> C/C++ -> INDEXER
Check Index Source And Header Files Open in Editor.
I had the same issue, like many people.
I followed the steps in Ayberk Özgür post, which make good sense. Although I also had to make sure to put includes under all three languages: GNU C, GNU C++, and Assembly. Probably because I'm not using a stand alone tool chain.
I at first had my includes only under GNU C and GNU C++ languages. Which left me still with the unresolved includes error. Not until I assigned my includes under the Assembler language as well did my errors go away.
I do not know why eclipse is only searching through the Assembler includes in my project. I also do not know how this part of the solution will work for bigger more complicated projects.
Hope this helps.
I had the similar situation with Eclipse CDT working with the OpenCV library. I got several error messages while the program compiled correctly. I changed the indexer setting in "window->preferences->Indexer" "build configuration for indexer" box to "Use Active Configuration" which solved my issue.
I just spent about 3h banging my head against this Eclipse NDK indexing issue!..
What made it work: make sure that you have only ONE cpu architecture specified in Your Application.mk file.
Otherwise the .metadata/.plugins/com.android.ide.eclipse.ndk/*.pathInfo file will not be generated by the NDK build. This file contains built-in values from Project -> Properties -> C/C++ General -> Paths and Symbols -> Includes (just making .pathInfo file does not fix the problem)

C files get an error using the NDK

I have been given a program written in C, and have been asked to use the functions from the program in a new Android application. I have been following the tutorials of the NDK, but have hit a wall. I think all that is left is compiling or linking my C files, but I'm not sure. I have installed the SDK, NDK, Eclipse, Cygwin, and Sequoyah ( I haven't used Sequoyah yet and don't really know where it comes into play ).
I have used the ndk-build command, but am not sure what I am should be expecting. This is a screenshot of my Cygwin terminal.
This is a screen shot of my NWPTest.c file. All of the fields defined in the .h file come up as errors. I feel like this is an error that should go away once I compile the code.
BYTE and WORD etc. declarations here:
This is a screen shot of the NWPTest.h file. BYTE and WORD declarations are also coming up as errors, which is something I do not understand.
This screen shot basically shows the last thing as the last one, but I'm confused how some of the BYTEs are errors and some are not:
EDIT
Here is my Android.mk file. Just to clarify, I was given the entire C project, and was told to make it an Android application. I have no experience with C.
Probably your problem is that Eclipse doesn't know about the include path for the ndk C header files; however that should not really matter since ndk-build appears to have built your native library. Though if your C-aware eclipse setup is being obstinate and refusing to build the rest of your project with "errors" in the C files, then you will have to either fix the include path or tell eclipse to ignore the C files as it does when not aware of C.
Of more concern would be whatever is causing an error in Operations.java - that will prevent you from building an apk.
I guess this is eclipse error. It simply does not understand your c and h files. Try to install CDT plugin, maybe after that the problems will eliminate.

Categories

Resources