5

私はAndroid AARライブラリを持っています。 debuggableがtrueの場合、またはapkがdebug buildType.アンドロイドライブラリのデバッグ可能なビルドタイプまたはデバッグビルドタイプを確認する方法は?

を使用して作成されている場合に、ライブラリを使用できないようにする必要があるというセキュリティポリシーを、アンドロイドでプログラムで確認するにはどうすればよいですか?

+0

あなたのライブラリの 'build.gradle'をチェックしてデバッグかどうかを調べることができますが、どのようにして消費者の' build.gradle'をチェックするのですか? – matrix

+0

@ KostasDrakonakisそれはまさに問題です:) –

+0

@FarhadFaghihiしかし、何故ですか?重要な点は、ライブラリがデバッグされていないことです。誰もがデバッグモードでアプリを開発します。 –

答えて

5

反射してこの問題を回避するには、このようなプロジェクトの(ないライブラリの)BuildConfig値を取得するためにあります:DEBUGフィールドを取得するために

/** 
* Gets a field from the project's BuildConfig. This is useful when, for example, flavors 
* are used at the project level to set custom fields. 
* @param context  Used to find the correct file 
* @param fieldName  The name of the field-to-access 
* @return    The value of the field, or {@code null} if the field is not found. 
*/ 
public static Object getBuildConfigValue(Context context, String fieldName) { 
    try { 
     Class<?> clazz = Class.forName(context.getPackageName() + ".BuildConfig"); 
     Field field = clazz.getField(fieldName); 
     return field.get(null); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (NoSuchFieldException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

は、例えば、単にライブラリーActivityからこれを呼び出す:

boolean debug = (Boolean) getBuildConfigValue(this, "DEBUG"); 

私はまだこれを試していないし、それはすべての時間が動作することを保証することはできませんが、先に進むことができます!

+1

が正しいと思われます。私は試して結果を知らせます。 –

+0

私は解決策を試して、それが動作します。 –

+0

喜んで助けることができる! – matrix

関連する問題