Skip to content Skip to sidebar Skip to footer

Nosuchmethodexception Loading Build.getradioversion() Using Reflection

I'm trying to load the radio version of the Android device using reflection. I need to do this because my SDK supports back to API 7, but Build.RADIO was added in API 8, and Build.

Solution 1:

Try this:

try {
    Method getRadioVersion = Build.class.getMethod("getRadioVersion");
    if (getRadioVersion != null) {
        try {
            String version = (String) getRadioVersion.invoke(Build.class);
            // Add your implementation here
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        Log.wtf(TAG, "getMethod returned null");
    }
} catch (NoSuchMethodException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Solution 2:

What Build.getRadioVersion() actually does is return the value of gsm.version.baseband system property. Check Build and TelephonyProperties sources:

static final StringPROPERTY_BASEBAND_VERSION = "gsm.version.baseband";

publicstaticStringgetRadioVersion() {
    returnSystemProperties.get(TelephonyProperties.PROPERTY_BASEBAND_VERSION, null);
}

According to AndroidXref this property is available even in API 4. Thus you may get it on any version of Android through SystemProperties using the reflection:

publicstaticStringgetRadioVersion() {
  returngetSystemProperty("gsm.version.baseband");
}


// reflection helper methodsstaticStringgetSystemProperty(String propName) {
  Class<?> clsSystemProperties = tryClassForName("android.os.SystemProperties");
  Method mtdGet = tryGetMethod(clsSystemProperties, "get", String.class);
  returntryInvoke(mtdGet, null, propName);
}

staticClass<?> tryClassForName(String className) {
  try {
    returnClass.forName(className);
  } catch (ClassNotFoundException e) {
    returnnull;
  }
}

staticMethodtryGetMethod(Class<?> cls, String name, Class<?>... parameterTypes) {
  try {
    return cls.getDeclaredMethod(name, parameterTypes);
  } catch (Exception e) {
    returnnull;
  }
}

static <T> T tryInvoke(Method m, Objectobject, Object... args) {
  try {
    return (T) m.invoke(object, args);
  } catch (InvocationTargetException e) {
    thrownewRuntimeException(e.getTargetException());
  } catch (Exception e) {
    returnnull;
  }
}

Post a Comment for "Nosuchmethodexception Loading Build.getradioversion() Using Reflection"