I like to build the Android NDK example "native-audio", supplied with the NDK.
The NDK-supplied compiler swallows it without complaint, but Eclipse is showing several errors in file native-audio-jni.c.
In the code excerpt below, all of the static variable declarations have red underlining, because Eclipse claims that the types cannot be resolved. They are defined right up there in the OpenSLES.h file, which Eclipse does find if I Ctrl + left click the file.
Note that I modified the lines where hello_clip and android_clip are included compared to the example source - I put the array declarations in those "header" files also (they contained just comma separated data), since Eclipse had a problem parsing the syntax, too:
static const char array[] = #include"blah.h";
I tried things suggested over the internet, like starting with eclipse.exe -clean, clean project / rebuild, refresh project, etc., none helps.
So, what could be the reason/solution for Eclipse not resolving those types?
#include <assert.h>
#include <jni.h>
#include <string.h>
// for __android_log_print(ANDROID_LOG_INFO, "YourApp", "formatted message");
// #include <android/log.h>
// for native audio
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
// for native asset manager
#include <sys/types.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
// pre-recorded sound clips, both are 8 kHz mono 16-bit signed little endian
// includes data arrays here
#include "hello_clip.h"
#include "android_clip.h"
// engine interfaces
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine;
// output mix interfaces
static SLObjectItf outputMixObject = NULL;
static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
(the file is full of many more of those problems)
Related
I searched online and on this site and found this link which asks the same question I am about to, but the reply does not seem address the question. Plus one of the referenced links are missing. Basically the question is how do you efficiently and intelligently decide when to use a jni-compatible function to what you have in your C source file. I am familiar with what javah command does, but that command converts JAVA methods into a C header file to be used. What about the methods that are already implemented in a C source file? How can you know if you have to convert them to a JNI version of the method? I am using Android Studio and put in the following code in the .c source file that is in my jni folder of the project:
#include "com_example_sansari_usetbt_MainActivity.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <android/log.h>
#define TAG "native-log-tag"
#define LOGI(LOG_TAG, ...) __android_log_print (ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define LOGV(LOG_TAG, ...) __android_log_print (ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define LOGE(LOG_TAG, ...) __android_log_print (ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
/*
* Class: com_example_sansari_usetbt_MainActivity
* Method: usetbt
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_sansari_useqsee_MainActivity_Usetbt
(JNIEnv *env, jobject obj)
{
int fd;
int rc = 0;
char *rd_buf[16];
//(*env)->printf("<1>%s: entered\n", argv[0]);
printf("<1>: entered\n");
fd = open("/dev/tbt", O_RDWR);
LOGI(LOG_TAG,"This is a log test");
LOGV(LOG_TAG,"This is a log test");
LOGE(LOG_TAG,"This is a log test");
//(*env)->fd;
//return;
if ( fd == -1 ) {
perror("<1>open failed");
rc = fd;
}
printf("<1>: open: successful\n");
/* Issue a read */
rc = read(fd, rd_buf, 0);
//I need to find what fd is and then use command completion to pick a jni finction. rd-buf seems to be jstring, and 0 seems to be int
rc = (*env)->GetString
if ( rc == -1 ) {
perror("<1>read failed");
close(fd);
}
printf("<1>: read: returning %d bytes!\n",rc);
close(fd);
(*env)->NewStringUTF(env,"Hi From Usetbt version 2");
}
And ndk-build compiles the project, but I do not get the result I am looking for. That is, the normal C version of the code which I compiled with NDK opens the driver and is able to interface with it, but this does not do the same. I do not see the result of the print statement in the kernel logs that is. I am not asking for someone to convert the above code; rather show me the way to know which of the above lines need to be converted, and what is the best way to do it please. I have read a number of items about this, and I am quickly coming up to speed, but if you can describe at a high level how
this conversion is done, please advise. I do have a copy of Java Native Interface and a number of Android texts and am going through them as fast as possible.
Thanks
I believe you need to call fflush(stdout); after your printf. As per this answer.
I have a C++ program that starts like this:
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
#include "linefinder.h"
#define PI 3.1415926
using namespace cv;
int main(int argc, char* argv[]) {
int houghVote = 200;
string arg = argv[1];
bool showSteps = argv[2];
string window_name = "Processed Video";
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
VideoCapture capture(arg);
I want to make an Android app from it.
I have NDK installed but I don't know what I have to do now.
Do I have to change anything in the C++ main program so that it will run on Android, or can I compile and run it unchanged?
Introduction to Android
Android provides a rich application framework that allows you to build innovative apps and games for mobile devices in a Java language environment. ... Android apps are built as a combination of distinct components that can be invoked individually.
You need to construct your app using Java, then call your C++ code via JNI.
Also, Android NDK has this to say
you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++
In particular, read Using the NDK to understand why you can't run your C++ main program without changing anything.
I'm writing an Adroid app with some C++ code behind the UI using Eclipse + NDK (r8d). I have some code that I thought was fool proof but the compiler just gives me weird errors like "Invalid arguments" without specifics. Here is what my C++ code looks like:
#include <jni.h>
#include <string>
using namespace std;
#include "../../Evaluator.Engine/Evaluator.Engine.h"
Evaluator evaluator;
extern "C" {
JNIEXPORT jstring JNICALL Java_haskellevaluator_android_MainActivity_evaluateNative(JNIEnv *env, jobject, jstring jInput)
{
...
string sInput(L"Hello world");
string sResult = evaluator.evaluate(sInput);
jstring jResult = env->NewStringUTF(sResult.data());
return jResult;
}
}
Evaluator.Engine.h is nothing fancy, but just a declaration of the class Evaluator.
#include <string>
using namespace std;
class Evaluator
{
public:
string evaluate(string input);
};
However, the compiler complains:
Invalid arguments '
Candidates are:
? evaluate(?)
'
as if string is not defined. But if I put a copy of the header file under the same folder, the error goes away. This is a Windows box. I have tried using \ and escaped \\ as path separators and it didn't work.
Does this sound like a NDK (or whatever the preprocessor it uses) bug? I don't want to move the header file because it'll be shared by other projects. I also hate to keep 2 copies of the same file.
Any ideas? Thanks.
Sorry I don't have windows OS, but I've tried you code on a MacOS, but it doesn't work because of:
string sInput(L"Hello world");
Saying that wchar_t cannot be put on std::string. Is it possible to be the same problem ?
I have a problem using cocos2d-x on android.
I am trying to load a .zip file using
CCFileUtils::sharedFileUtils()->getFileDataFromZip(...)
As this project is designed to be multiplatform, we also tried this code on an iPad3 and there it is working without problem.
The problem is that the app gets an Segmentation Fault when loading the zip file.
Here is the whole code with some extra comments
void Map::loadChunk(int index, CCPoint startPoint) {
chunks[index]->startPoint = startPoint;
std::stringstream filename;
filename << (int)startPoint.x << "_" << (int)startPoint.y << ".map";
unsigned long filesize = 0;
const char* path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("map1.zip");
CCLog(path);
CCLog(filename.str().c_str());
// In the following two lines the error occurs
const char* buffer =(const char*) CCFileUtils::sharedFileUtils()->getFileDataFromZip(path, filename.str().c_str(), &filesize);
std::istringstream fileBuffer(buffer);
CCLog("filesize %d", (int)filesize);
std::string line;
// Here some code follows but this code does not produce the problem so I left it out
}
The used includes are:
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include "cocos2d.h"
The values for path and filename are
map1.zip and -128_-128.map
map1.zip is in the assets folder and the .map file exists inside map1.zip
Thanks in advance
I've figured out why this is not working.
It seems to be the problem to pack another .zip into the .apk.
When playing the .zip in the folder received by
CCFileUtils::sharedFileUtils()->getWriteablePath()
and then open the zip with
CCFileUtils::sharedFileUtils()->getFileDataFromZip(...)
the zip loads properly and I am able to read certain files from inside the zip.
I have a directory mounted by fuse,and i am intented to use lstat() against this dirctory.But,when i run the following code,it just wait there and prompt nothing.
And by the way,i run the fuse in the android emulator.
the code is:
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!");
char *path="/data/pwrite/test_12/";
struct stat *stbuf;
int res=12;
res=lstat(path, stbuf);
printf("%d",res);
}
And,"/data/pwrite/test_12/" is the fuse-mounted directory.What's more,when i try another dicrtory that share the same parent directory but not mounted by fuse,like /data/pwrite/test_13/,it works!
so,i definely sure it is leaded by fuse.But,i'm even more confused that whether it is due to the conflict between fuse and android.
Any idea?thx
Try without the bugs and see if that works better.
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!\n");
const char *path="/data/pwrite/test_13/";
struct stat stbuf;
int res=12;
res=lstat(path, &stbuf);
printf("%d\n",res);
}
why it not work?because there runs several fuse-deamon currently.