简体   繁体   English

在 kotlin 类中调用 Java 单例类时出现 NoClassDefFound 错误

[英]NoClassDefFound Error while calling Java singleton class in kotlin Class

how I'll be able to get data from Singleton class of java in kotlin class.?我如何能够从 kotlin 类中的 Java 的 Singleton 类中获取数据。? since my project is an old project and whole app is in java now I'm implementing its new module in kotlin where I'm using Java's Singleton (CurrentUser.java) in kotlin's Service Class (UpdateWidgetService.kt) .while I'm using this CurrentUser.getInstance().userType code for getting type of user in My service class its throwing below exception因为我的项目是一个旧项目,整个应用程序现在都在 Java 中,我正在 kotlin 中实现它的新模块,我在 kotlin 的服务类(UpdateWidgetService.kt)使用 Java 的单例(CurrentUser.java) 。而我正在使用这个CurrentUser.getInstance().userType代码用于在我的服务类中获取用户类型,它抛出以下异常

Process: com.example.myapp, PID: 27066
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/myapp/main/CurrentUser

Java爪哇

   public class CurrentUser {

          String userType = "";

     private static CurrentUser instance;

      public static CurrentUser getInstance() {
            if (instance == null) {
                instance = new CurrentUser();
            }
            return instance;
        }

        public void setUserType(String userType) {
            this.userType = userType;
        }

        public String getUserType() {
            return userType ;
        }



    }

Kotlin科特林

CurrentUser.getInstance().userType

Rebuild and invalidate cache your project and then restart your android studio.重建和无效缓存您的项目,然后重新启动您的 android studio。 Also Sync file system.也同步文件系统。 I was having the same issue and i did this trick and it worked for me.我遇到了同样的问题,我做了这个技巧,它对我有用。 Hope this may helps you.希望这可以帮助你。

在 kotlin 中像这样获取 userType

 val userType=CurrentUser.getInstance().userType

Would you mind to provide information if you are using ProGuard for minifying the Code?如果您使用 ProGuard 来缩小代码,您介意提供信息吗?

If Yes Make sure u add the model class in your proguard-rules.pro file如果是,请确保您在 proguard-rules.pro 文件中添加模型类

exmp: -keep class packagename.models.* { *; exmp: -keep class packagename.models.* { *; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM