GreenDao custom type converter and code obfuscation configuration in the Android ORM series, ormgreendao
Sometimes, our entity classes have some attributes, but there is no corresponding type in the database. In this case, we need to customize the converter for type conversion. The most common processing is the Date type. Generally, if the precision requirement is not high, the data will be converted to the Long type for storage. During reading, the data will be converted to the Date type. If we want to store the time string directly, GreenDao provides us with support for custom type conversion.
First, we need to implementPropertyConverterInterface to implement the two methodsConvertToEntityPropertyAndConvertToDatabaseValueThis interface requires two generic parameters. The first parameter is the type in the object class, and the second parameter is the type stored in the database. Now, if we need to convert Date to String for storage, the interface implementation should be like this.
public class DateStringConverter implements PropertyConverter<Date,String> { @Override public Date convertToEntityProperty(String databaseValue) { return null; } @Override public String convertToDatabaseValue(Date entityProperty) { return bull; }}
Next we will convert
public class DateStringConverter implements PropertyConverter<Date,String> { private static final String DEFAULT_FORMAT="yyyy-MM-dd HH:mm:ss"; @Override public Date convertToEntityProperty(String databaseValue) { return convert2Date(databaseValue,DEFAULT_FORMAT); } @Override public String convertToDatabaseValue(Date entityProperty) { return convert2String(entityProperty,DEFAULT_FORMAT); } public static String convert2String(Date date,String format){ String currentDate=null; try { SimpleDateFormat formatter=new SimpleDateFormat(format); currentDate=formatter.format(date); }catch (Exception e){ e.printStackTrace(); } return currentDate; } public static Date convert2Date(String day, String format) { if (day == null || format == null) return null; SimpleDateFormat formatter = new SimpleDateFormat(format); try { Date dt = formatter.parse(day); return dt; } catch (ParseException e) { e.printStackTrace(); } return null; }}
Generate entity classes later
Entity type = schema.addEntity("Demo");type.addStringProperty("test").customType("java.util.Date","cn.edu.zafu.greendao.db.converter.DateStringConverter");
AddProperty is the type in the corresponding database. Here we store it as a string, so it isAddStringPropertyAnd thenCustomTypeThe function specifies the type in the object class. Here isJava. util. DateAnd then the full class name of our type converter. Next, try to insert a piece of data into the database. We will find that it is stored directly as a string,
Add the following statements to the project to be obfuscated:
-keepclassmembers class * extends de.greenrobot.dao.AbstractDao { public static java.lang.String TABLENAME;}-keep class **$Properties
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.