Question

I have an Android library project which is used by several Android application projects. A library jar (having .class files) is attached to the project and located in the lib folder.

1

I am trying to set the variable com.utility.Commons.outgoingSIP (string) to the variable located inside the jar.

Declared inside Commons.java: public static String outgoingSIP = "";

2

Here is a code snipplet:

public void sendMessage(SIPMessage sipMessage, InetAddress receiverAddress, int receiverPort)
        throws IOException {
    long time = System.currentTimeMillis();
    byte[] bytes = sipMessage.encodeAsBytes(this.getTransport());
    sendMessage(bytes, receiverAddress, receiverPort, sipMessage instanceof SIPRequest);

    Commons.outgoingSIP = receiverAddress.toString();

But the variable never changes. I imported it at the top too (import com.utility.Commons).

The library and source are also attached otherwise the program wouldn't compile:

3

From inside the jar I can also CTRL + CLICK the Commons.outgoingSIP variable and it will take me to commons.java. So eclipse KNOWS which variable I'm talking about.

Am I missing something? Can someone please give me some advice.

Was it helpful?

Solution

I'm not sure if you can do that - maybe with some reflection. I am certain, however, that you do not want to do that.

Having a library rely on a global variable is not a good library interface. Instead of relying of a global variable that may or not exist, have your library clients specifically tell you what it is you need to know.

In your case, add something like this to your library:

public class Config {
    private static String _outgoingSIP;
    public static void setOutgoingSIP(string sip) { _outgoingSIP = sip; }
    public static String getOutgoingSIP() { return _outgoingSIP; }
}

Your clients wlil call Config.setOutgoingSIP, and your library will use Config.getOutgoingSIP.

Note - you can make _outgoingSIP a public static variable, and have your library code access it directly. I'm not too fond of this approach, but maybe it's my C# background talking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top