J2Objc, Protobuf nano and Swift - android

I have Java classes being translated to Objective C (J2Objc using the cradle plugin). The simple Android app and Unit tests work fine. The same simple Objective C iOS app also works perfectly. However, when I try to write similar code in a Swift project I cannot access instance variables, find methods, etc. This is relatively new to me so I may be missing something basic.
Here's the code in Objective C. Works perfectly:
SHRRefreshAppointments *mTest = [[SHRRefreshAppointments alloc] initWithId:nil withBoolean:true];
[mTest run];
SHREventListResp *mResp = [SHREventListResp parseFromWithByteArray:mTest->mResponseData_];
NSLog(#"%#", mResp.description);
Same code in Swift - I cannot find the instance variable mResponseData which is readily available in Objective C:
let mTest : SHRRefreshAppointments = SHRRefreshAppointments(id: nil, withBoolean: true)
mTest.run()
var mResp : SHREventListResp = SHREventListResp.parseFromWithByteArray(mTest.)
Here are the contents of the bridging header:
#include "JreEmulation.h"
#include "J2ObjC_header.h"
#import "com/gcatconsult/shared/remote/AppConstants.h"
#import "com/gcatconsult/shared/remote/AppUtils.h"
#import "com/gcatconsult/shared/messages/nano/Resp.h"
#import "com/gcatconsult/shared/messages/nano//Req.h"
#import "com/gcatconsult/shared/messages/nano/EventListResp.h"
#import "com/gcatconsult/shared/messages/nano/EventListReq.h"
#import "com/gcatconsult/shared/messages/nano/Event.h"
#import "com/gcatconsult/shared/remote/RefreshAppointments.h"
#import "com/gcatconsult/shared/remote/NetworkBase.h"
SHRRefreshAppointments header:
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: /Users/gabrielchoza/AndroidStudioProjects/GcatMobile/shared/src/main/java/com/gcatconsult/shared/remote/RefreshAppointments.java
//
#include "J2ObjC_header.h"
#pragma push_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments")
#ifdef RESTRICT_ComGcatconsultSharedRemoteRefreshAppointments
#define INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments 0
#else
#define INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments 1
#endif
#undef RESTRICT_ComGcatconsultSharedRemoteRefreshAppointments
#if !defined (SHRRefreshAppointments_) && (INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments || defined(INCLUDE_SHRRefreshAppointments))
#define SHRRefreshAppointments_
#define RESTRICT_ComGcatconsultSharedRemoteNetworkBase 1
#define INCLUDE_SHRNetworkBase 1
#include "com/gcatconsult/shared/remote/NetworkBase.h"
#define RESTRICT_JavaLangRunnable 1
#define INCLUDE_JavaLangRunnable 1
#include "java/lang/Runnable.h"
#interface SHRRefreshAppointments : SHRNetworkBase < JavaLangRunnable >
#pragma mark Public
- (instancetype)initWithId:(id)requestData
withBoolean:(jboolean)asyncCall;
#pragma mark Protected
- (void)postProcessExcecute;
#end
J2OBJC_EMPTY_STATIC_INIT(SHRRefreshAppointments)
FOUNDATION_EXPORT void SHRRefreshAppointments_initWithId_withBoolean_(SHRRefreshAppointments *self, id requestData, jboolean asyncCall);
FOUNDATION_EXPORT SHRRefreshAppointments *new_SHRRefreshAppointments_initWithId_withBoolean_(id requestData, jboolean asyncCall) NS_RETURNS_RETAINED;
FOUNDATION_EXPORT SHRRefreshAppointments *create_SHRRefreshAppointments_initWithId_withBoolean_(id requestData, jboolean asyncCall);
J2OBJC_TYPE_LITERAL_HEADER(SHRRefreshAppointments)
#compatibility_alias ComGcatconsultSharedRemoteRefreshAppointments SHRRefreshAppointments;
#endif
#pragma pop_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments")
The SHRNetworkBase superclass:
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: /Users/gabrielchoza/AndroidStudioProjects/GcatMobile/shared/src/main/java/com/gcatconsult/shared/remote/NetworkBase.java
//
#include "J2ObjC_header.h"
#pragma push_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase")
#ifdef RESTRICT_ComGcatconsultSharedRemoteNetworkBase
#define INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase 0
#else
#define INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase 1
#endif
#undef RESTRICT_ComGcatconsultSharedRemoteNetworkBase
#if !defined (SHRNetworkBase_) && (INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase || defined(INCLUDE_SHRNetworkBase))
#define SHRNetworkBase_
#define RESTRICT_JavaLangRunnable 1
#define INCLUDE_JavaLangRunnable 1
#include "java/lang/Runnable.h"
#class IOSByteArray;
#class JavaLangInteger;
#interface SHRNetworkBase : NSObject < JavaLangRunnable > {
#public
NSString *mGetPatientPath_;
NSString *mStringURL_;
IOSByteArray *mRequestData_;
IOSByteArray *mResponseData_;
jboolean mShouldCompress_;
jint mCurrentCall_;
jboolean mAsyncCall_;
}
#pragma mark Public
- (instancetype)initWithNSString:(NSString *)callPath
withBoolean:(jboolean)shouldCompress
withJavaLangInteger:(JavaLangInteger *)currentCall
withBoolean:(jboolean)async;
- (void)run;
#pragma mark Protected
- (void)postProcessExcecute;
- (id)sendAsyncServerRequestWithNSString:(NSString *)stringURL
withId:(id)requestData
withBoolean:(jboolean)shouldCompress
withJavaLangInteger:(JavaLangInteger *)currentCall;
#end
J2OBJC_EMPTY_STATIC_INIT(SHRNetworkBase)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mGetPatientPath_, NSString *)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mStringURL_, NSString *)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mRequestData_, IOSByteArray *)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mResponseData_, IOSByteArray *)
FOUNDATION_EXPORT void SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(SHRNetworkBase *self, NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async);
FOUNDATION_EXPORT SHRNetworkBase *new_SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async) NS_RETURNS_RETAINED;
FOUNDATION_EXPORT SHRNetworkBase *create_SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async);
J2OBJC_TYPE_LITERAL_HEADER(SHRNetworkBase)
#compatibility_alias ComGcatconsultSharedRemoteNetworkBase SHRNetworkBase;
#endif
#pragma pop_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase")
Any help will be appreciated.

My limited understanding is that Swift imports Objective C properties as fields, not the fields themselves. You'll therefore need an accessor method for mResponseData.
The good news is that j2objc has a Property annotation, which you can add to the mResponseDate field so an equivalent Objective C property is generated during translation. That property should be imported later in Swift.
The Property annotation will also generate default accessors (like #synthesize does in Objective C), so if your Java class already has accessors, specify them using the getter= and setter= #property attributes. The translator's testProperties() test demonstrates a complex property annotation example.

mResponseData_ is an ivar and Swift can only access objective-c properties or functions. Alternative you can use mTest.valueForKey("mResponseData_") and mTest.setValue(someValue, forKey: "mResponseData_") or edit the generated class and transform the ivar in a property or method.
Not that you can create extension files with computed get and set to encapsulate the valueForKey implementation.
You can refer to this question for more details.

Related

Can't use typedef enum

I have a header file with definition:
typedef enum acamera_metadata_enum_android_lens_facing {
// enumeration
} acamera_metadata_enum_android_lens_facing_t;
The problem is when I am trying to declare this enum as my class'es member the compiler can't find definition (header is found).
../../../../src/main/cpp/include/camera_manager.h:41:9: error: unknown type name 'acamera_metadata_enum_android_lens_facing_t'
acamera_metadata_enum_android_lens_facing_t facing;
This is my class header:
#ifndef DAVINCI_CAMERA_MANAGER_H
#define DAVINCI_CAMERA_MANAGER_H
#include <map>
#include <string>
#include <camera/NdkCameraManager.h>
#include <camera/NdkCameraError.h>
#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraMetadataTags.h> // The enumeration is defined here
#include <media/NdkImageReader.h>
namespace DaVinci {
class CameraId;
class CameraManager {
struct ACameraManager *_manager;
std::map<std::string, CameraId> _cameras;
std::string _activeCameraId;
int32_t _cameraFacing;
int32_t _cameraOrientation;
bool _valid;
public:
CameraManager();
~CameraManager();
};
// helper classes to hold enumerated camera
class CameraId {
public:
struct ACameraDevice *device;
std::string id;
acamera_metadata_enum_android_lens_facing_t facing;
bool available; // free to use ( no other apps are using
bool owner; // we are the owner of the camera
explicit CameraId(const char *id);
explicit CameraId();
};
};
#endif //DAVINCI_CAMERA_MANAGER_H
Where can the problem be?
P.S. I am using c++ 14 if it's important.
UPDATED
I created a repository with my project: https://bitbucket.org/ghostman2013/davinci_test
In your project's app/build.gradle you have minSdkVersion set to 21.
The native camera APIs were added in API level 24.
So you can either A) increase your minSdkVersion to 24 or higher, or B) Not use the native camera APIs in your library.

How to turn on/off native log cat in ndk at runtime

I use this snippet code to turn log on or off
#define DEBUG 1
#if DEBUG
#include <android/log.h>
#define LOG_TAG "native_log"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
# define LOGD(...) do {} while (0) // do nothing
#endif
// use it
LOGD("%s : %d","value", val);
It worked fine by turn DEBUG flag on/off. The problem is I want to do it at run time in java side. What I want like this:
// java
private native void nativeSetDebug(boolean flag);
// jni
JNIEXPORT void JNICALL Java_com_my_package_Native_nativeSetDebug(JNIEnv *env, jobject thiz, jboolean flag){
// what should I do in this method?
}
Since the macros in c++ are replaced by the preprocessor by their value before source file even compiles, so I'm looking for another approach. Is there any ideas?
macro file
extern bool useDebug;
#include <android/log.h>
#define LOG_TAG "native_log"
#define LOGD(...) if(useDebug){__android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)}
C file
bool useDebug = true;
JNIEXPORT void JNICALL Java_com_my_package_Native_nativeSetDebug(JNIEnv *env, jobject thiz, jboolean flag){
useDebug = flag;
}
The extern is important, otherwise each file including the header will define its own variable and they won't be set correctly.

__android_log_print equivalent for printf

I am trying to port this logging statement to work, so it will run on linux and android my #define'ing it:
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
I have cross-compiled my app to run on both Linux and Android. However, as linux doesn't have the equivalent I have tried to do my own.
/** ANDROID */
#if defined(__ANDROID__)
#include <android/log.h>
#define LOG_ERROR ANDROID_LOG_ERROR
#define LOG(PRIORITY, fmt, ...) __android_log_print(ANDROID_LOG_UNKNOWN, LOG_TAG, fmt, ##__VA_ARGS__)
/** LINUX */
#elif defined(linux) || defined(__linux) || defined(__linux__)
#define LOG_ERROR LINUX_LOG_ERROR
#define LOG(PRIORITY, fmt, ...) printf(PRIORITY fmt, ##__VA_ARGS__)
#endif
And then using it like this when running under linux
LOG(LOG_ERROR, "Testing loggging [ %d ]", test);
Is there a better way to do this?
Many thanks for any suggestions,
I managed to solve it this way. Here is the complete solution.
This would be in a header file
typedef enum levels_tag levels_e;
enum levels_tag {
LOG_UNKNOWN = 0,
LOG_DEFAULT,
LOG_VERBOSE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
LOG_SILENT,
LOG_LAST
};
/* ANDRIOD IMPLEMENTATION */
#if defined( __ARM_EABI__)
#include <android/log.h>
levels_e levels[LOG_LAST] = {LOG_UNKNOWN,
LOG_DEFAULT,
LOG_VERBOSE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
LOG_SILENT};
#define LOG_TAG "MODULE_LOG_SIP"
#define LOGGING(PRIORITY, fmt, ...) __android_log_print(levels[PRIORITY], LOG_TAG, fmt, ##__VA_ARGS__)
/* LINUX IMPLEMENTATION */
#elif defined(linux) || defined(__linux) || defined(__linux__)
char *priority_levels[] = {"UNKNOWN",
"DEFAULT",
"VERBOSE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL",
"SILENT",
NULL };
#define LOGGING(PRIORITY, fmt, ...) \
do { \
char *priority = priority_levels[PRIORITY]; \
printf("%s/%s:%d [%s] " fmt " \n", __FILE__, __func__, __LINE__, priority, ##__VA_ARGS__); \
} while(0)
#endif
#define LOG(PRIORITY, fmt, ...) LOGGING(PRIORITY, fmt, ##__VA_ARGS__)
And in your source files you would just call the LOG macro like this:
LOG(LOG_FATAL, "Failed to create and initialize application instance [ %d ]", errno);
Now if you were to compile on linux it would use the printf statement. And if you were to compile on Android it would use the __android_log_print statement. Both would produce the same formatted output.
Hope this helps someone else.
You can also use family of syslog() functions to make output to system log instead of stdout/stderr (see syslog.h):
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
Logs can be viewed in /var/log (file depends on options of syslog, by default in many systems logs are in the file "messages"). Sometimes this way is a better than stdout/stderr, but you have to use openlog/closelog. Unfortunately, syslog's priorities are different comparing to android priorities and thus it doesn't allow simple macro definition with transparent priorities.Syslog priorities:
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */
#define LOG_ERR 3 /* error conditions */
#define LOG_WARNING 4 /* warning conditions */
#define LOG_NOTICE 5 /* normal but significant condition */
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */
Priorities of android log:
/*
* Android log priority values, in ascending priority order.
*/
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority;

JNI, C++ problems

I did an Opencv's application en windows and now I am using JNI to convert this code to Android but I am having some problems.
In concrete my native code not do nothing.
This is my Java class where I define my native methods:
package com.example.telo3;
import org.opencv.core.Mat;
public class Process {
static {
System.loadLibrary("nativo");
}
public Process(){
dir=inicializar_nativo();
}
public void Procesar(Mat framedetect, Mat framedraw){
procesar_nativo(dir,framedetect.getNativeObjAddr(),framedraw.getNativeObjAddr());
}
private long dir;
private static native long inicializar_nativo();
private static native void procesar_nativo(long thiz, long framedetect, long framedraw);
}
This is my JNI code:
#include "nativo.h"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/video/tracking.hpp"
#include <iostream>
#include <stdio.h>
#include "FaceDetector.h"
#include "Draw.h"
#include "Almacena.h"
#include "Runnable.h"
using namespace std;
using namespace cv;
#include <android/log.h>
#define LOG_TAG "NATIVO"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
struct variables {
Almacena almacena;
Draw draw;
FaceDetector face_detector;
};
JNIEXPORT jlong JNICALL Java_com_example_telo3_Process_inicializar_1nativo(
JNIEnv *, jobject) {
long dir = (long) new variables();
return (dir);
}
JNIEXPORT void JNICALL Java_com_example_telo3_Process_procesar_1nativo(JNIEnv *,
jobject, jlong dir, jlong framedetect, jlong framedraw) {
Mat* telo =(Mat*)framedetect;
Mat* telo2= (Mat*)framedraw;
((variables*)dir)->almacena = ((variables*)dir)->face_detector.Detect(*telo);
//almacena = face_detector.Detect(frame_gray);
((variables*)dir)->draw.Dibujar(*telo2,((variables*)dir)->almacena);
//frame_capturado = draw.Dibujar(frame_capturado, almacena);
if( (((variables*)dir)->almacena.get_faces()).size() ==0){
LOGD("no detecto caras");
}
}
I think that I use the Jni correctly but the function Detect not works correctly because when I uses this if return 0.
Is framedetect 0? I made a test app based on this and it worked fine (that is, the jlong was converted to and from just fine, so that shouldn't be the issue).
Try catching the error with ndk-gdb to better understand the problem. Attaching it to the process might a problem though if it crashes straight away, in that case I like to put breakpoint on the java side before the crash, make the execution pause there using the debugger, attach ndk-gdb and continue let the java process continue.
Also I recommend using reinterpret_cast<jlong> and reinterpret_cast<variables*> instead of the C style casts, and save that cast to a separate variable to avoid casting all the time! cleaner code!

Android NDK: Function va_start / va_end could not be resolved

I trying to use va_start and va_end functions in my project, but eclipse don't want to resolve it as functions. gcc compiles whole project without errors...
[myfile.cpp]
#include <stdio.h>
#include <stdarg.h>
[...]
inline void ShowDbgMsg( const char* str, ... )
{
va_list argptr;
va_start(argptr, str);
vprintf(str, argptr);
va_end(argptr);
}
[...]
[Android.mk]
[...]
LOCAL_C_INCLUDES := jni/pvrTools/ jni/igel/ $(STLPORT_BASE)/stlport
[...]
Eclipse says:
[...]
Description Resource Path Location Type
Function 'va_start' could not be resolved igel.comdef.h /NativeProject/jni/igel line 195 Semantic Error
Function 'va_end' could not be resolved igel.comdef.h /NativeProject/jni/igel line 203 Semantic Error
Function 'va_start' could not be resolved igel.string.h /NativeProject/jni/igel line 341 Semantic Error
Function 'va_end' could not be resolved igel.string.h /NativeProject/jni/igel line 351 Semantic Error
[...]
So, it looks like Eclipse unable to locate something... How to solve this issue?
Thanks in advance!
P.S.> Project->Index->Rebuild didn't help. :(
My solution is also not pretty. But after you've deleted the error markers 100 times, try putting this code somewhere before you include stdlib.h. Then define ECLIPSEBUILD=1 in Project::Properties::C++ General::Paths and Symbols.
#if ECLIPSEBUILD // this part is just to fix spurious Eclipse errors
typedef __builtin_va_list va_list;
#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#define va_copy(d,s) __builtin_va_copy(d,s)
#endif
#define __va_copy(d,s) __builtin_va_copy(d,s)
typedef __builtin_va_list __gnuc_va_list;
typedef __gnuc_va_list va_list;
typedef va_list __va_list;
#endif
I solve this issue by replacing my va_* code with embedded compiler features:
#ifdef IGEL_PLATFORM_ANDROID
# define ShowDbgMsg(...) ((void)__android_log_print(ANDROID_LOG_INFO, "igel-debug", __VA_ARGS__))
#else
inline void ShowDbgMsg( const char* str, ... )
{
va_list argptr;
va_start(argptr, str);
vprintf(str, argptr);
va_end(argptr);
}
#endif // IGEL_PLATFORM_ANDROID
I know it's not a good solution, but it works.
#include <stdarg.h> // includes va_list
inline void ShowDbgMsg(char* format, ...)
{
va_list ap;
va_start( ap, format );
// replace vfprintf( stderr, format, ap ) with:
__android_log_vprint(0, __FILE__, format, ap);
va_end( ap );
}

Categories

Resources