1

I have a number of static constants that I am using for mathetmatical reasons in my app (these are parameters that should be tweaked only be developers and not by the users).

On the NDK side I have a header file (.h) containing the values of all these constants. Similarly, on the Java side I have a class that keeps track of the same variables (copied).

Is there one place (one file) where I can store these constants for use by both Java and C++? Maybe something like an XML file so a developer only has to tweak the constants there?

If so, how should I properly implement it (get).

Rohan
  • 515
  • 10
  • 30
  • You could put them into resource xml like http://stackoverflow.com/a/20120240 but constants defined on both sides with a note to keep the other side in sync isn't uncommon. Especially since resources requires that you have a `Context` object so you can't use them as easily. Manual asset text property file would also work but requires manual parsing on both sides. – zapl Dec 04 '15 at 20:58

1 Answers1

0

Please use the workaround below with extra caution, but it may save you some dev effort.

start with a simple Java class:

package com.example.hellojni;

public class Settings {
    public final static int ANSWER = 42;
}

this is UseSettings.h:

#pragma once
#define package struct { struct { bool hellojni; } example; } com = {{true}}; static bool com_example_hellojni_Settings =
#define public
#define final const
#define class struct
#include "../java/com/example/hellojni/Settings.java"
;
#undef class
#undef final
#undef public
#undef package

now you can write in your C++ file something like

#include "UseSettings.h"
int answer = Settings::ANSWER;

This solution may probably be tweaked to work with C99, but I didn't put enough effort into this.

Update:

Similar approach can be applied to simple enum:

package com.example.hellojni;

public enum Test {
    test1,
    test2,
    test3
}

#define public
#define package static struct { struct { bool hellojni; } example;} com {true}; static bool com_example_hellojni_Test =

#include "…/Test.java"

#undef public
#undef package
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307