Using ProGuard with reflection for startBluetoothSCO () in an Android app

I use reflection to call the AudioManager.startBluetoothSco () method. This should be done through reflection, as it exists only in version 2.2 or higher of the SDK, but my application is built for min version 2.0. If I just compile my application without using ProGuard, everything will work fine - the method will be called correctly in 2.2, and not at all in lower versions. But when I use ProGuard, it does not work at all. I suppose there is a KEEP add-on in the ProGuard configuration that will fix this, but I cannot figure out what it should be. Here are 3 lines from my code:

AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
Method startBT = AudioManager.class.getMethod("startBluetoothSco");   
startBT.invoke(am);
+3
source share
1 answer

- , , ProGuard : http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html

, StrictMode ( ):

public abstract class StrictModeUtil {

    private static class GingerbreadAndBeyond extends StrictModeUtil {

        private static class Holder {
            private static final GingerbreadAndBeyond INSTANCE = new GingerbreadAndBeyond();
        }

        public GingerbreadAndBeyond() {
            // we just enable StrictMode here, but only in developer mode
            if (C.D) {
                android.os.StrictMode.enableDefaults();
            }
        }
    }

    public static StrictModeUtil getInstance() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            return GingerbreadAndBeyond.Holder.INSTANCE;
        } else {
            // we don't need an implementation before Android 2.3
            return null;
        }
    }

}
+1

Source: https://habr.com/ru/post/1793884/


All Articles