I’m not sure if this is backendless issue or I am missing something. When I dynamically invoke BackendlessUser’s methods such as “setPassword”, “setProperty”, it works:
backendlessUserClass = (Class<?>) dexClassLoader.loadClass(“com.backendless.BackendlessUser”);
instance = backendlessUserClass.newInstance();
final Method setPasswordMethod = backendlessUserClass.getMethod(“setPassword”, String.class);
setPasswordMethod.invoke(instance, “mypassword”);
final Method setUserMethod = backendlessUserClass.getMethod(“setProperty”, String.class, Object.class);
setUserMethod.invoke(instance, “name”, “John”);
However, when i invoke Backendless.UserService’s “login”, the application fails:
userServiceClass = dexClassLoader.loadClass(“com.backendless.UserService”);
//final Method getInstanceMethod = userServiceClass.getDeclaredMethod(“getInstance”, null);instance = userServiceClass.newInstance(); //failed here//I looked at the UserService and there is no public constructor in UserServicefinal Method loginMethod = userServiceClass.getDeclaredMethod(“login”, String.class, String.class, boolean.class);getInstanceMethod.invoke(instance, “John”, “mypassword”, true);Am I missing something or UserService has issue?Thank you
Thank you Mark. I plan to use Reflection for several APIs so I can target devices running different versions of Android with a simple application package. Also this is a convenient way to execute code not installed as part of an application package to leverage new APIs not only with Backendless.
Thanks, Key. Rather than calling “newInstance()” on com.backendless.UserService, try loading the “com.backendless.Backendless” class first, and from there getting the “UserService” field. The reason is you do not want to skip the static initializer block in the Backendless class.
I try to use com.backendless.Persistence to save() and find() with the similar approach that you suggested. The “save” method works great without invoke the “of” but the “find” method I got exception method not found whether I invoke the “of” or not. Any suggestions, please see my snippet code for both below. Thank you.
//save works great
… (some other code here to load)
Field persistenceField = backendlessClass.getDeclaredField(“Persistence”);
persistenceField.setAccessible(true);
persistenceObject = persistenceField.get(backendlessClass);
persistenceClass = persistenceObject.getClass();
Method saveMethod = persistenceClass.getDeclaredMethod(“save”, Object.class);
saveMethod.invoke(persistenceObject, backendlessUserObject);
//find throws exception
… (some other code here to load)
Field persistenceField = backendlessClass.getDeclaredField(“Persistence”);
persistenceField.setAccessible(true);
persistenceObject = persistenceField.get(backendlessClass);
persistenceClass = persistenceObject.getClass();
Class<?> dataQueryClass = classLoader.loadClass(“com.backendless.persistence.BackendlessDataQuery”);
Object dataQueryObject = dataQueryClass.newInstance();
Method whereMethod = dataQueryClass.getMethod(“setWhereClause”, String.class);
String whereClause = “objectId = '” + id + “’” ;
setWhereClauseMethod.invoke(belDataQueryObject, whereClause);