java - 应用程序在测试设备上调试但在发布 apk 时运行良好,它没有从 firebase 获取 post.class 变量

标签 java android firebase-realtime-database

我的应用程序在 Activity 中有帖子,帖子有图像和喜欢按钮以及星数和分享按钮。调试到测试设备时一切正常,但在发布的版本中却没有。 在谷歌商店发布应用程序后,post.class 下的所有变量都不会被接收/显示(所有使用 post.get 的东西,例如 post.getimage() 都是没有从 firebase 数据库获取值。另一方面,存储在帖子外的值被接收(例如,我有一个单独的 starcount 节点,我正在获取 childcount 的值,它是工作正常)。 我已经尝试了所有可能的解决方案,但没有任何效果: 使用 SHA 发布 key (上传 key )更新应用程序,然后生成签名的 apk(aab),所有三个 SHA(调试、发布和应用程序签名 key )都保存在我的 firebase 项目指纹下。 我还检查了 firebase 数据库规则。 我将 post.class 中的一些变量从 public 更改为 private

我直接在设备上下载了 APK,但它也无法运行。它在测试设备上运行完美。感谢是否有人可以提供帮助,非常感谢。

这是post.class

// [START post_class]

@IgnoreExtraProperties

public class Post {

private String image;
public String uid;
public String author;
public String title;
public String body;
public String videoLink;
private int starCount = 0;
private int starCount2 = 0;
private Map<String, Boolean> stars = new HashMap<>();

//shares
private String ntDeeplink;
private int ntNumShares = 0 ;
private String postID;

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

public Post(String image, String uid, String author, String title, String body, String videoLink) {
    this.image = image;
    this.uid = uid;
    this.author = author;
    this.title = title;
    this.body = body;
    this.videoLink = videoLink;


}

//note 2 constructor here were icrease instead of one , and the above one in connectedto some methods already


public Post(int starCount, Map<String, Boolean> stars, String ntDeeplink, int ntNumShares, String ntPostID) {
    this.starCount = starCount;
    this.stars = stars;
    this.ntDeeplink = ntDeeplink;
    this.ntNumShares = ntNumShares;
    this.postID = postID;
}

public Post(int starCount2) {
    this.starCount2 = starCount2;
}

// [START post_to_map]   ....this and bindToPost(if there is a view) replace the method of populateViewHolder
// but only for non Storage Items like Images
@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();

    result.put("image",image);
    result.put("uid", uid);
    result.put("author", author);
    result.put("title", title);
    result.put("body", body);
    result.put("videoLink", videoLink);
    result.put("starCount", starCount);
    result.put("starCount2", starCount2);
    result.put("stars", stars);
    //numShares also created int the contants
    result.put("ntNumShares", ntNumShares);
    result.put("ntDeeplink", ntDeeplink);
    result.put("postID", postID);

    return result;
}

//shares

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getBody() {
    return body;
}

public void setBody(String body) {
    this.body = body;
}

public String getVideoLink() {
    return videoLink;
}

public void setVideoLink(String videoLink) {
    this.videoLink = videoLink;
}

public int getStarCount() {
    return starCount;
}

public void setStarCount(int starCount) {
    this.starCount = starCount;
}

public int getStarCount2() {
    return starCount2;
}

public void setStarCount2(int starCount2) {
    this.starCount2 = starCount2;
}

public Map<String, Boolean> getStars() {
    return stars;
}

public void setStars(Map<String, Boolean> stars) {
    this.stars = stars;
}

public String getNtDeeplink() {
    return ntDeeplink;
}

public void setNtDeeplink(String ntDeeplink) {
    this.ntDeeplink = ntDeeplink;
}

public int getNtNumShares() {
    return ntNumShares;
}

public void setNtNumShares(int ntNumShares) {
    this.ntNumShares = ntNumShares;
}

public String getPostID() {
    return postID;
}

public void setPostID(String postID) {
    this.postID = postID;
}


// [END post_to_map]

} //[结束课后]

例如,这就是我在 Activity 中获取图像的方式

        ValueEventListener postListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            final Post post = dataSnapshot.getValue(Post.class);
            final DatabaseReference postRef = mPostReference;
            final DatabaseReference postRefLikes = mPostReferenceLikes;


            // [START_EXCLUDE]

            //Image
            String image = null;
            if (post != null) {
                image = post.getImage();
            }
            Picasso.with(HbDetailActivity.this).load(image).placeholder(R.drawable.likes_hl_gr).into(hbDActivityPostImage);

最佳答案

经过两天的搜索,我找到了问题的解决方案: 造成此问题的是 Proguard。创建签名的 APK 时,Proguard 会清除所有未使用的代码或类。在我的例子中,post.class 已被 Proguard 错误删除:

解决方法:

  • 我把 post.class 和其他我想确保 Proguard 不会删除它们的类放在一个包(模型)下,你可以把它们放在不同的包下,但你需要列出所有这些根据 Proguard 规则如下:

  • 我在 5 月的 android studio 项目中打开了 proguard-rules.pro

  • 当 Proguard 创建已签名的 APK(或 aab)时,我将以下行列出并防止类被删除

    -keepclassmembers class com.mydomain.app.models.** {*;} 
    

其中com.domain.app是包名

希望这会节省一些人的时间。

关于java - 应用程序在测试设备上调试但在发布 apk 时运行良好,它没有从 firebase 获取 post.class 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55776463/

相关文章:

android - 我尝试将我的 Android 项目与 Firebase 连接,但出现 JSON 文件错误

java - 为什么不在 opencv java 中使用 createFisherFaceRecognizer()?

源代码中的java.lang.nullpointerException

java - 我的计时器正在执行的方法一直失败

android - 使用 OpenCV 检测账单

android - event.params 评估为未定义;无法使用云功能访问 Firebase 实时数据库中的 event.params.post

java - 从调制解调器 (AT) 获取清晰易读的答案

java - 调用 repaint 不会导致在线程中调用 Paint 方法

android - 异步更新 Android 上下文菜单

ios - 从 Firebase 解析数据时无法成功重新加载 UITableView 元素