3

The app works fine in debug mode, but when i compile in release and upload to play store, the encapsulated getters return null. The snapshot has data, but getter are returning null.

Anyone has any idea why this happens ?

Firebase database :

Firebase database

Encapsulated getter:

import com.google.firebase.database.Exclude;
import com.google.firebase.database.IgnoreExtraProperties;

import java.util.HashMap;
import java.util.Map;

@IgnoreExtraProperties
public class Posto {

    public String uid;
    public String nome;
    public String publico;
    public String status;
    public double latitude;
    public double longitude;
    public int bandeira;
    public String cep;
    public String logradouro;
    public String numero;
    public String bairro;
    public String cidade;
    public String estado;
    public String telefone;
    public double valor = 0.000;
    public int starCount = 0;
    public Map<String, Boolean> stars = new HashMap<>();

    public Posto() {
        // Default constructor required for calls to DataSnapshot.getValue(Posto.class)
    }

    public Posto(String uid, String nome, String publico, String status, Double latitude,
                 Double longitude, int bandeira, String cep, String logradouro, String numero,
                 String bairro, String cidade, String estado, String telefone, Double valor) {
        this.uid = uid;
        this.nome = nome;
        this.publico = publico;
        this.status = status;
        this.latitude = latitude;
        this.longitude = longitude;
        this.bandeira = bandeira;
        this.cep = cep;
        this.logradouro = logradouro;
        this.numero = numero;
        this.bairro = bairro;
        this.cidade = cidade;
        this.estado = estado;
        this.telefone = telefone;
        this.valor = valor;
    }

    // [START post_to_map]
    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("uid", uid);
        result.put("nome", nome);
        result.put("publico", publico);
        result.put("status", status);
        result.put("latitude", latitude);
        result.put("longitude", longitude);
        result.put("bandeira", bandeira);
        result.put("cep", cep);
        result.put("logradouro", logradouro);
        result.put("numero", numero);
        result.put("bairro", bairro);
        result.put("cidade", cidade);
        result.put("estado", estado);
        result.put("telefone", telefone);
        result.put("valor", valor);
        result.put("starCount", starCount);
        result.put("stars", stars);

        return result;
    }
    // [END post_to_map]

}
// [END post_class]

and there:

    public void CarregaMarcNuvem() {
        showProgressDialog();

        mPostosDBRef.addListenerForSingleValueEvent(new ValueEventListener() {
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Limpa os marcadores do mapa e zera a matriz identificadora de marcadores
                mMap.clear();
                mHashMap.clear();

                Log.d(TAG,"dataSnapshot: "+dataSnapshot);

                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                    mPostoKey = postSnapshot.getKey();
                    Posto posto = postSnapshot.getValue(Posto.class);

                    Log.d(TAG,"postSnapshot: "+postSnapshot);

                    mLatitude = posto.latitude;
                    mLongitude = posto.longitude;
                    mValor = posto.valor;
                    mNome = posto.nome;
                    mLogradouro = posto.logradouro;
                    mNumero = posto.numero;
                    mCidade = posto.cidade;

// .... SOME TREATMENT OF DATA ....

                    mClusterManager.addItem(offsetItem);
                    mHashMap.put(offsetItem, postSnapshot.getKey());
                }
                mClusterManager.cluster();
                hideProgressDialog();
                try {
                    GravaHashMap();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("****** The read failed: " + databaseError.getCode());
            }
        });
    }

dataSnapshot and postSnapshot has data from database OK, but in getter posto not.

posto.latitude and all return null only in release mode. In debug mode, works fine.

I really dont know what to do. Please, any idea ?

=== SOLVED ===

Include forward line in dependencies of build.graddle :

" compile 'com.android.support:support-annotations:25.2.0' "

Add " @Keep " after public class Posto {, like this :

...
@IgnoreExtraProperties
@Keep
public class Posto {
    public String uid;
    public String nome;
...

Thanks for Frank Van Puffelen

Itapox
  • 579
  • 1
  • 7
  • 25
  • 2
    Most likely Proguard is hiding/stripping those classes in release mode, making it impossible for Firebase to serialize/deserialize them. See http://stackoverflow.com/questions/37743661/firebase-no-properties-to-serialize-found-on-class/41141406#41141406 and http://stackoverflow.com/questions/35104739/failure-to-write-a-simple-pojo-to-firebase-android – Frank van Puffelen Mar 08 '17 at 14:39
  • 2
    Thats it! Problem solved! I´d include compile **'com.android.support:support-annotations:25.2.0'** in build.graddle dependencies and **@Keep** after **public class Posto {**. Now its working in release too. Thanks Frank! – Itapox Mar 08 '17 at 16:52
  • 1
    Another way to solve this problem is removing the line **minifyEnabled true** from **build.graddle**, but i think is better the solution with sinalize **@Keep** to Proguard. – Itapox Mar 08 '17 at 17:16
  • 1
    Also have a look at the documentation about it in the "Optional: Configure ProGuard" section on this page: https://firebase.google.com/docs/database/android/start/ – Frank van Puffelen Mar 08 '17 at 17:17

0 Answers0