Search code examples
androidormliteandroid-annotations

How to adjust AndroidAnnotations 3.x compatible ORMLite code for AndroidAnnotations 4.0.0


I'm trying to upgrade from AndroidAnnotations 3.3.2 to version 4.0.0 and have a problem with getting ORMLite to work after the upgrade. After I upgrade to 4.0.0 and build the project I get the error message: "error: cannot find symbol class OrmLiteDao" And this code part is marked in red:

@OrmLiteDao(helper = DatabaseHelper.class)
ContactDao mContactDao;

My old class(es) that works perfectly with AndroidAnnotations 3.3.2 looks something like this:

public class ContactService {

    private final String TAG = getClass().getSimpleName();

    @RootContext
    Context ctx;

    @OrmLiteDao(helper = DatabaseHelper.class)
    ContactDao mContactDao;

    public Contact getContact(Integer contactId) throws ItemNotFoundException, SQLException {
        Contact contact = mContactDao.queryForId(contactId);
        if (contact == null) {
            Log.e(TAG, "Contact not found in database");
            throw new ItemNotFoundException();
        }

        return contact;
    }

    public List<Contact> getContacts() throws ItemNotFoundException, SQLException {
        List<Contact> contact = mContactDao.queryForAll();
        if (contact == null) {
            Log.e(TAG, "Contacts not found in database");
            throw new ItemNotFoundException();
        }

        return contact;
    }

    public Contact createContact(Contact contact) throws SQLException {
        try {
            mContactDao.create(contact);
        } catch (SQLException e) {
            Log.e(TAG, e.getLocalizedMessage());
        }

        Log.d(TAG, "New Contact ID: " + contact.getId());

        return contact;
    }   

}

And my ContactDao class is relatively empty since I have the methods all in the ContactService class:

public class ContactDao extends BaseDaoImpl<Contact, Integer> {

    public ContactDao(Class<Contact> dataClass) throws SQLException {
        super(dataClass);
    }

    public ContactDao(ConnectionSource connectionSource, Class<Contact> dataClass) throws SQLException {
        super(connectionSource, dataClass);
    }

    public ContactDao(ConnectionSource connectionSource, DatabaseTableConfig<Contact> tableConfig) throws SQLException {
        super(connectionSource, tableConfig);
    }

    public ContactDao(ConnectionSource connectionSource) throws SQLException {
        super(connectionSource, Contact.class);
    }
}

According to the AndroidAnnotations Wiki (https://github.com/excilys/androidannotations/wiki/Ormlite) I have to change the code to this:

@EActivity
public class MyActivity extends Activity {

    @OrmLiteDao(helper = DatabaseHelper.class)
    void setUserDao(UserDao userDao){
        // do something with userDao
    }

}

It's not clear to me how I have to adjust my code above to the new format (below) in order to make it work with AndroidAnnotations 4.0.0. Could anyone show me how I have to adjust my code to have it compatible with AndroidAnnotations 4.0.0?

Thank you.


Solution

  • You do not have to change the code in they way you wrote in the last example. In that, you are using method injection, but that is an alternative, field injection still works in AA 4.0.0.

    However you have to add more dependencies, and adjust your imports for the new package name.

    build.gradle:

    compile "org.androidannotations:androidannotations-api:4.0.0"
    apt "org.androidannotations:androidannotations:4.0.0"
    compile "org.androidannotations:ormlite-api:4.0.0"
    apt "org.androidannotations:ormlite:4.0.0"
    

    .java files where @OrmLiteDao is used:

    import org.androidannotations.ormlite.annotations.OrmLiteDao;
    

    You could read these instructions here.