Question

What i want: Use a jar file that contains all my model classes (for reuse in the webservice i separated the models) in my android project. I´m new to android so i guess i missed something? is it necessary to export my classes differently when using it in android?

What i get:

`11-15 12:26:14.082: E/Trace(1776): error opening trace file: No such file or directory (2)
11-15 12:26:14.152: E/dalvikvm(1776): Could not find class 'ww.entities.model.User', referenced from method com.example.weightwatchersapp.MainActivity.onCreate
11-15 12:26:14.172: W/dalvikvm(1776): VFY: unable to resolve new-instance 812 (Lww/entities/model/User;) in Lcom/example/weightwatchersapp/MainActivity;
11-15 12:26:14.172: D/dalvikvm(1776): VFY: replacing opcode 0x22 at 0x000a
11-15 12:26:14.182: D/dalvikvm(1776): DexOpt: unable to opt direct call 0x1499 at 0x0c in Lcom/example/weightwatchersapp/MainActivity;.onCreate
11-15 12:26:14.212: W/System.err(1776): TEST
11-15 12:26:14.262: D/AndroidRuntime(1776): Shutting down VM
11-15 12:26:14.262: W/dalvikvm(1776): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
11-15 12:26:14.292: E/AndroidRuntime(1776): FATAL EXCEPTION: main
11-15 12:26:14.292: E/AndroidRuntime(1776): java.lang.NoClassDefFoundError: ww.entities.model.User
11-15 12:26:14.292: E/AndroidRuntime(1776):     at com.example.weightwatchersapp.MainActivity.onCreate(MainActivity.java:17)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.app.Activity.performCreate(Activity.java:5104)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.os.Looper.loop(Looper.java:137)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at java.lang.reflect.Method.invoke(Method.java:511)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-15 12:26:14.292: E/AndroidRuntime(1776):     at dalvik.system.NativeStart.main(Native Method)`

What i did: For generating it i used a simple java project (not a library project or something like this and exported it as a jar. I can use the same library in my webservice java project without any problems.

What i tried: 1. Use automatically generated class MainActivity and add following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.err.println("TEST");// TODO: error here
    User user = new User();
    user.setDailyPoints(13);
}
  1. Add ww-entities to libs folder
  2. Right-click the lib and chose "Add to build path"
  3. Clean project
  4. Checked ww-entities.jar in "Order and Export"
  5. Checked Android Private Libraries in "Order and Export"

I tried to follow a lot of threads, that treated similar problems, but somehow it seems i am missing the point. (no need to say i´m new to android and need a comprehensive answer for noobies right? ^^)

Edit:

I´m using ormlite with my models so i.e. my class User looks like:
public class User implements Serializable {
    private static final long serialVersionUID = -2057474913370787753L;
    @DatabaseField(generatedId = true)
    private long id;
    @DatabaseField(canBeNull = false, unique = true)
    private String username;
    @DatabaseField
    private String password;
    @DatabaseField
    private String email;
    @DatabaseField
    private String forename;
    @DatabaseField
    private String surname;
    @DatabaseField
    private Date birthday;
    @DatabaseField
    private String sex;
    @DatabaseField
    private double height;
    @DatabaseField
    private int dailyPoints;
    @DatabaseField
    private int weeklyPoints;
    @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
    private OnlineUserFlags onlineUserFlags;
    @DatabaseField
    private boolean showProfileOnline;

    public User() {
    }

    public User(String username, String password, String email, String forename, String surname, Date birthday, String sex, double height, int dailyPoints, int weeklyPoints, OnlineUserFlags onlineUserFlags, boolean showProfileOnline) {
        super();
        this.username = username;
        this.password = password;
        this.email = email;
        this.forename = forename;
        this.surname = surname;
        this.birthday = birthday;
        this.sex = sex;
        this.height = height;
        this.dailyPoints = dailyPoints;
        this.weeklyPoints = weeklyPoints;
        this.onlineUserFlags = onlineUserFlags;
        this.showProfileOnline = showProfileOnline;
    }

    @Override
    public int hashCode() {
        //generated hascode method
    }

    @Override
    public boolean equals(Object obj) {
    //generated equals method
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    //...getters/setters  
}

No correct solution

OTHER TIPS

The easiest way of doing this (in SDK tools 0.22.2.1, at least) is to not export the jar, but to instead add your ww-entities project as a dependency of your android project (Build Path > Projects).

You then need to make sure you check ww-entities in the Order and Export tab.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top