java - Firebase 中的 dataSnapshot.getValue(MyClass.class) 在开发中工作正常,但在 Play 商店上发布后获取空值

标签 java android firebase firebase-realtime-database google-play

我正在从 firebase(实时数据库)获取数据并在 recyclerview 中正确显示它。这是我的代码:

我的类(class),StickerPack.class

public class StickerPack implements Parcelable {
    //Vienen de la bd
        public String identifier;
        public String name;
        public String publisher;
        public String trayImageFile;
        public String publisherEmail;
        public String publisherWebsite;
        public String privacyPolicyWebsite;
        public String licenseAgreementWebsite;
        public String imageDataVersion;
        public boolean avoidCache;
    //Se generan dinamicamente
        public String iosAppStoreLink;
        public List<Sticker> stickers;
        public long totalSize;
        public String androidPlayStoreLink;
        public boolean isWhitelisted;

    //Constructores
        public StickerPack(){}
        public StickerPack(String identifier, String name, String publisher, String trayImageFile, String publisherEmail, String publisherWebsite, String privacyPolicyWebsite, String licenseAgreementWebsite, String imageDataVersion, boolean avoidCache) {
            this.identifier = identifier;
            this.name = name;
            this.publisher = publisher;
            this.trayImageFile = trayImageFile;
            this.publisherEmail = publisherEmail;
            this.publisherWebsite = publisherWebsite;
            this.privacyPolicyWebsite = privacyPolicyWebsite;
            this.licenseAgreementWebsite = licenseAgreementWebsite;
            this.imageDataVersion = imageDataVersion;
            this.avoidCache = avoidCache;
        }
        protected StickerPack(Parcel in) {
            identifier = in.readString();
            name = in.readString();
            publisher = in.readString();
            trayImageFile = in.readString();
            publisherEmail = in.readString();
            publisherWebsite = in.readString();
            privacyPolicyWebsite = in.readString();
            licenseAgreementWebsite = in.readString();
            iosAppStoreLink = in.readString();
            stickers = in.createTypedArrayList(Sticker.CREATOR);
            totalSize = in.readLong();
            androidPlayStoreLink = in.readString();
            isWhitelisted = in.readByte() != 0;
            imageDataVersion = in.readString();
            avoidCache = in.readByte() != 0;
        }

    //Getter and Setters are generated by default but i will not show them to not make the post so long
}

我使用像这样的引用和监听器,我使用两种不同的方式只是为了测试,并且它们都工作正常

        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference databaseReference = firebaseDatabase.getReference("sticker_packs");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                stickerPackList = new ArrayList<>();
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    StickerPack pack = ds.getValue(StickerPack.class) ;
                    StickerPackFirebase pack2 = ds.getValue(StickerPackFirebase.class) ;

                    GenericTypeIndicator<ArrayList<StickerPack>> t = new GenericTypeIndicator<ArrayList<StickerPack>>() {};
                    ArrayList<StickerPack> spl = dataSnapshot.getValue(t);

                    GenericTypeIndicator<StickerPack> t2 = new GenericTypeIndicator<StickerPack>() {};
                    StickerPack sp = ds.getValue(t2);


                    Log.i("CARTOONLOG", "value1: " + Objects.requireNonNull(ds.getValue()).toString());
                    Log.i("CARTOONLOG", "value2: " + Objects.requireNonNull(pack).name );
                    Log.i("CARTOONLOG", "value2: " + Objects.requireNonNull(pack2).name );


                    if (sp!= null){
                        stickerPackList.add(sp);
                        Log.i("CARTOONLOG", "pack: " + sp.name);
                    }
                }
                setUpRecyclerView(stickerPackList);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.i("ERROR FIREBASE", "Error trayendo los datos, firebase snapshot in stickerpacklist");
            }
        });

        DatabaseReference databaseReference2 = firebaseDatabase.getReference("sticker_packs");
        databaseReference2.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {



                StickerPack pack = dataSnapshot.getValue(StickerPack.class) ;
                StickerPackFirebase pack2 = dataSnapshot.getValue(StickerPackFirebase.class) ;
                Log.i("CARTOONLOGCHILD", "value0: " + Objects.requireNonNull(dataSnapshot.getValue()).toString() + " string: " + s);
                Log.i("CARTOONLOGCHILD", "value1: " + Objects.requireNonNull(pack).name);
                Log.i("CARTOONLOGCHILD", "value2: " + Objects.requireNonNull(pack2).name);


            }
                //The other methods are below this
        });

我在 Firebase 中的数据如下所示: enter image description here

这是来自开发环境的日志:

enter image description here

这是我在内部测试中发布后从 Google Play 商店下载的应用程序的日志:

enter image description here

这是我在 gradle 中的配置

buildTypes {
    customDebugType {
        debuggable true
    }
    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

编辑:

也许这是一个有用的信息

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'

    implementation 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
    implementation 'androidx.browser:browser:1.2.0'
    implementation 'androidx.media:media:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    implementation 'com.google.android.material:material:1.1.0'

    implementation 'com.google.firebase:firebase-messaging:20.1.5'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.facebook.fresco:fresco:1.10.0'
    implementation 'com.facebook.fresco:webpsupport:1.10.0'
    implementation 'com.facebook.fresco:animated-webp:1.10.0'
    implementation 'com.facebook.fresco:webpsupport:1.10.0'
    implementation 'com.facebook.fresco:animated-base:1.13.0'
    implementation 'com.facebook.fresco:nativeimagetranscoder:1.12.0'
    implementation 'com.google.android.gms:play-services-ads:19.1.0'
    implementation 'com.android.billingclient:billing:2.2.0'

    implementation 'com.google.firebase:firebase-analytics:17.3.0'
    implementation 'com.google.firebase:firebase-ads:19.1.0'


    //implementation 'com.firebaseui:firebase-ui-database:6.2.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.google.firebase:firebase-database:19.2.1'
    implementation 'com.google.firebase:firebase-storage:19.1.1'
    implementation 'com.firebaseui:firebase-ui-storage:6.2.1'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'


}

编辑:图像链接替换为图像文件

最佳答案

很可能 ProGuard 正在缩小您的模型类,这意味着应用程序无法从数据库中读回它们。

参见:

关于java - Firebase 中的 dataSnapshot.getValue(MyClass.class) 在开发中工作正常,但在 Play 商店上发布后获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61435721/

相关文章:

java - 如何在 Spring Boot 中使用 Controller /服务上的缓存?

java - 不确定根据用户输入计算佣金

安卓工具栏 : Centre align a TextView in a toolbar having a menu

Android,为什么一个 Fragment 不能直接调用另一个?

java - 如何使用相同的语句和结果集运行多个选择查询?

java - Selenium 优化 getVisibleElement(当元素不存在时)

java - 在 android 应用程序项目的 adt 20 中添加外部 jar

android - iOS 和 Android 中的 Firebase OTP 身份验证消息不同

ios - 错误 'GoogleDataTransport/GDTClock.h' 找不到文件

json - 如何将JSON数据保存到Google Firebase的Firestore数据库?