android-sdk icon indicating copy to clipboard operation
android-sdk copied to clipboard

InvoiceActivity class using reflections to get resources class

Open c3ph3us opened this issue 9 years ago • 1 comments

first its bad idea and you doing it wrong way :)

why u not using provided context which starts activity to obtain resources class name ?

why not use ? context.getApplicationContext() and use in

public static int getResIdByName(Context context, String name, String type) {
    return context.getResources().getIdentifier(name, type, context.getPackageName());
}

you should address also some other issues:

  1. multidex / used classloader could not be aware of multiple dex files

if you need use reflection try:

  // use space name not android application id  :) - see my last words 
  Class<?>  aClass = classLoader.loadClass(javaPackageName +".R$id");  
  1. differences between package/applicationId and java package for class name R as

    getPackageName()

returns the flavor-specific "package" (applicationId) instead of what you defined in the app's manifest (package field).- be aware of that that

now my app can't use your api as packageName.R doesn't exist in my app :)

you have two choices to correct this:

  1. iterate over all classes until you find expected resource or
  2. get proper package name / space name

c3ph3us avatar Jan 23 '17 10:01 c3ph3us

you should mention also that lib requires

     <uses-permission android:name="android.permission.NFC"/>

and for api>23 that should be gathered at run-time !

c3ph3us avatar Jan 25 '17 05:01 c3ph3us