Android Question App works when compiled in Debug(rapid) mode but not in release mode

Rory Mapstone

Member
Licensed User
Longtime User
Good day,

I have wrapped the Java smack library to be used in B4A. When I compile the app in Debug(rapid) mode I can establish and communicate with my xmpp server over the LAN. When I compile the exact same code using Release mode I am unable to connect to the server.

I have also checked to make sure that internet and network permissions are present.

Debug mode
upload_2015-5-7_9-22-40.png


Release mode
upload_2015-5-7_9-22-54.png


Java Lib:
B4X:
package smack.b4a.lib;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;


@Version(1.0f)
@Permissions(values={"android.permission.INTERNET"})
@ShortName("SmackLib")
@Events(values={"receivedmessage (mMsg As String, , fromName As String)"})


public class SmackLib {
    private ConnectionConfiguration connConfig;
    private XMPPConnection connection;
    private String mMsg="";
  
    public String autoConnect(String host, String port, String service, String u,String p)
    {
        ConnectionConfiguration _connConfig = new ConnectionConfiguration(host, Integer.parseInt(port), service);
        XMPPConnection _connection = new XMPPConnection(_connConfig);
        try {
            _connection.connect();
            _connection.login(u, p);
            Presence presence = new Presence(Presence.Type.available);
            _connection.sendPacket(presence);
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
        return "login OK";
    }
  
    public String AndroidConnectionConfiguration(String host, String port, String service)
    {

        try
        {
            connConfig = new ConnectionConfiguration(host, Integer.parseInt(port), service);
        }
        catch(Exception e)
        {
            return e.getMessage();
        }
        return host;

    }
  
    public String MakeConnection()
    {
        try
        {
            connection = new XMPPConnection(connConfig);
        }
        catch (Exception e)
        {
            return "Error:" + e.getMessage();
        }
        return "Config set";

    }
  
    public String ConnectServer()
    {
        try
        {

            connection.connect();
        }
        catch(Exception e)
        {
            return "Error ConnectServer:" + e.getMessage();
        }

            return connection.getHost();
    }
  
    public String Disconnect()
    {
        try
        {

            connection.disconnect();
        }
        catch(Exception e)
        {
            return "Error:" + e.getMessage();
        }

            return "Disconnected";
    }
  
    public String loginUser(String u,String p)
    {

        try
        {

            connection.login(u, p);
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);

        }
        catch(Exception e)
        {
            return "Error:" + e.getMessage();
        }

        return "login OK";
    }
  
  
    public String sendMessage(String to, String text)
    {

        try
        {
      
             Message msg = new Message(to, Message.Type.chat);
             msg.setBody(text);
             connection.sendPacket(msg);

        }
        catch(Exception e) {
            return "Error:" + e.getMessage();
        }
      
        return "send!";

    }
  
    public String ReceiveMessageEvent(final BA ba)
    {
        //mBA = new BA(null, null, null, mMsg, mMsg);
         PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
         connection.addPacketListener(new PacketListener()
         {
             public void processPacket(Packet packet)
             {
                 Message message = (Message) packet;
                 if (message.getBody() != null)
                 {
                     String fromName = StringUtils.parseBareAddress(message.getFrom());
                     mMsg = message.getBody();
                     //BA.raiseEvent2(this, false, "ReceivedMessage", true, new Object[] {mMsg});
                     ba.raiseEventFromDifferentThread(this, null, 0, "receivedmessage", false, new Object[] {mMsg, fromName});
                     //mBA.raiseEvent2(this, false, "receivedMessage", true, new Object[] {mMsg});
                     //BA.raiseEvent(this, "ReceivedMessage");
                 }
             }
         }, filter);
         //mBA.raiseEventFromDifferentThread(this, null, 0, "receivedmessage", false, new Object[] {"Hi"});
        //mBA.raiseEvent2(this, false, "receivedmessage", true, new Object[] {"hI"});
         return "Listening";
    }
  
}
Any ideas?

Regards
 
Last edited:

Rory Mapstone

Member
Licensed User
Longtime User
My guess:
1. This library makes the network calls on the main thread (not good).
2. It hides the error by printing it instead of throwing it (same). Check the unfiltered logs and you will see it.
3. Solution: Workaround the NetworkOnMainThread exception

Thanks Erel,

That solution worked.

I am creating a service which runs new thread to make the connection so I was under the impression it would be safe.

I think this could be the cause. RunOnGuiThread causes the thread to run on main thread.
B4X:
If th.Running =False Then th.RunOnGuiThread("thread1",Params)

I have a direction to focus on now, thanks again.
 
Upvote 0
Top