2017-12-19 18 views
1

私はを使用して特定のクラスの方法を持っています。Reflectionを使用しています。ここ
は私Activity onCreateの私のコードの例です:ProGuardの難読化されたクラスでReflectionを使用できますか?

try { 
    aMethod = getClass().getDeclaredMethod("myMethodName", SomeParameter.class); 
} catch (NoSuchMethodException e) { 
    e.printStackTrace(); 
} 

私は、Android Studioから直接それを実行すると、それは動作しますが、私はリリースバージョンを作成するときに、メソッド名は自動的にProGuardのでは変更されません。私に何ができる?

答えて

1

ProGuard documentationは、次のように言及している:

クラス名は、例えば、コンフィギュレーションファイルから 読むかもしれないので、(元の名前で)保存 する必要がどのクラスを計算するために、一般的には不可能です。したがって、同じ簡単な -keepオプションを使用して、 をProGuard構成で指定する必要があります。

しかし、ProGuardのは自動的にリフレクションを処理するにSOME例があります。それらは以下の通りです:(最新のリストについては、マニュアルを参照してください)

  • Class.forName("SomeClass") SomeClass.class
  • SomeClass.class.getField("someField")
  • SomeClass.class.getDeclaredField("someField")
  • SomeClass.class.getMethod("someMethod", new Class[] {})
  • SomeClass.class.getMethod("someMethod", new Class[] { A.class })
  • SomeClass.class.getMethod("someMethod", new Class[] { A.class, B.class })
  • SomeClass.class.getDeclaredMethod("someMethod", new Class[] {})
  • SomeClass.class.getDeclaredMethod("someMethod", new Class[] { A.class })
  • SomeClass.class.getDeclaredMethod("someMethod", new Class[] { A.class, B.class })
  • AtomicIntegerFieldUpdater.newUpdater(SomeClass.class, "someField")
  • AtomicLongFieldUpdater.newUpdater(SomeClass.class, "someField")
  • AtomicReferenceFieldUpdater.newUpdater(SomeClass.class, SomeType.class, "someField")

だから、私の場合には、私のエラーは、私がgetClass()を使用してクラスを得ていたということでした。
次の行がうまく機能しました。

aMethod = MainActivity.class.getDeclaredMethod("myMethodName", SomeParameter.class); 
関連する問題