简体   繁体   中英

Android Studio - add .jar library

This seems an old answered question, but I'm still having some issues. I created a Web Server that shares some classes as .jar. This is an example of a shared POJO class

@JsonInclude(Include.NON_NULL)
@Entity
@Table(name = "user", uniqueConstraints =
{ @UniqueConstraint(columnNames = "email"),
    @UniqueConstraint(columnNames = "nick") })
public class User implements java.io.Serializable, RecognizedServerEntities
{
  @Id
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "id", unique = true, nullable = false)
  private Integer userId;

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "threadAuthor",
      orphanRemoval = false)
  @Cascade({ CascadeType.SAVE_UPDATE })
  private Set<Thread> userThreads = new HashSet<Thread>(0);
}

Now, I'm trying to create an android app that simply use these shared classes: the app doesn't need to know anything about hibernate or javax.persistence (at least, is what I think). However, when I create a new project in Android Studio , it seems to require some additional libraries. Indeed, the gradle build process generates a lot of warnings like

warning: unknown enum constant CascadeType.REMOVE
  reason: class file for org.hibernate.annotations.CascadeType not found
warning: unknown enum constant FetchType.EAGER
  reason: class file for javax.persistence.FetchType not found

and at runtime the app crashes with this log message

java.lang.NoClassDefFoundError: org/hibernate/annotations/Cascade

So, do I have to import these .jar s as library and add a compile files() to build.gradle ? I've yet developed another client-side application using Java (Swing) and there is no need to import those libraries: the shared.jar was enough. Why do Android require them?

CascadeType and FetchType are part of Java EE. Try use findjar to download jar(s) which contains these classes.

Even if you will add downloaded jars to classpath, RuntimeException s still migt happen because these libs can use part of Java that isn't part of Android Java.

Just try and give us a feedback :)

Now, I'm trying to create an android app that simply use these shared classes: the app doesn't need to know anything about hibernate or javax.persistence (at least, is what I think).

Apparently, it does. Some of those annotations may be runtime annotations, in which case, you would need the JARs. Some of those that are compile-time annotations may expand into references to things in those JARs, and that is my interpretation of your error messages.

So, do I have to import these .jars as library and add a compile files() to build.gradle?

You are certainly welcome to try using Hibernate on Android, whether through JARs or artifacts up at someplace like Maven Central. Whether there is a port of Hibernate that works on Android, I cannot say. Last I checked, Hibernate was designed for server-side use with direct access to a database, whereas Android apps are designed for client-side use without direct access to a database (eg, using a Web service).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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