APK가 서명되었거나 "디버그 빌드"인지 확인하는 방법
내가 아는 한, 안드로이드 "릴리스 빌드"는 APK에 서명되어 있습니다. 코드에서 확인하는 방법 또는 Eclipse에 비밀이 정의되어 있습니까?
웹 서비스 데이터에서 ListView 항목 채우기를 디버깅하려면 옵션이 필요하지 않습니다.
내 생각:
- 응용 프로그램
android:debuggable
이지만 어떤 이유로 든 신뢰할 수 없습니다. - 서명 된 APK를 테스트하기 위해 동일한 장치를 사용하고 있으므로 하드 코딩 장치 ID는 좋지 않습니다.
- 코드 어딘가에 수동 플래그를 사용합니까? 그럴듯하지만 언젠가는 변화하는 것을 잊어 버리고 모든 프로그래머는 게으르다.
디버그 또는 릴리스 인증서를 사용하여 응용 프로그램이 빌드되었는지 확인하는 다른 방법이 있지만 다음과 같은 방법이 가장 좋습니다.
Android 문서 Signing Your Application 의 정보에 따르면 디버그 키에는 " CN = Android Debug, O = Android, C = US " 와 같은 제목 고유 이름이 포함되어 있습니다 . 이 정보를 사용하여 디버그 키 서명을 코드에 하드 코딩하지 않고 패키지가 디버그 키로 서명되었는지 테스트 할 수 있습니다.
주어진:
import android.content.pm.Signature;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
다음과 같이 isDebuggable 메소드를 구현할 수 있습니다.
private static final X500Principal DEBUG_DN = new X500Principal("CN=Android Debug,O=Android,C=US");
private boolean isDebuggable(Context ctx)
{
boolean debuggable = false;
try
{
PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),PackageManager.GET_SIGNATURES);
Signature signatures[] = pinfo.signatures;
CertificateFactory cf = CertificateFactory.getInstance("X.509");
for ( int i = 0; i < signatures.length;i++)
{
ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);
debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
if (debuggable)
break;
}
}
catch (NameNotFoundException e)
{
//debuggable variable will remain false
}
catch (CertificateException e)
{
//debuggable variable will remain false
}
return debuggable;
}
디버깅 가능한 플래그를 확인하려면 다음 코드를 사용하십시오.
boolean isDebuggable = ( 0 != ( getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
코 틀린 :
val isDebuggable = 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
자세한 정보는 Android LVL 애플리케이션 보안을 참조하십시오 .
또는 Gradle을 올바르게 사용하는 경우 BuildConfig.DEBUG
true 또는 false 인지 확인할 수 있습니다 .
의해 답변을 Mark Murphy
The simplest, and best long-term solution, is to use BuildConfig.DEBUG
. This is a boolean
value that will be true
for a debug build, false
otherwise:
if (BuildConfig.DEBUG) {
// do something for a debug build
}
Maybe late, but iosched uses BuildConfig.DEBUG
If you want to check an APK
statically, you could use
aapt dump badging /path/to/apk | grep -c application-debuggable
This outputs 0
if the APK
isn't debuggable and 1
if it is.
First add this to your build.gradle file, this will also allow side by side running of debug and release builds:
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
Add this method:
public static boolean isDebug(Context context) {
String pName = context.getPackageName();
if (pName != null && pName.endsWith(".debug")) {
return true;
} else {
return false;
}
}
A debug build is signed as well, just with a different key. It's generated automatically by Eclipse, and its certificate is valid for one year only. What's the problem with android:debuggable
? You can get this value from code using PackageManager
.
Another option, worth mentioning. If you need to execute some code only when debugger is attached, use this code:
if (Debug.isDebuggerConnected() || Debug.waitingForDebugger()) {
//code to be executed
}
Solved with android:debuggable
. It was bug in reading item where in some cases debug flag on item was not being stored in record getting if (m.debug && !App.isDebuggable(getContext()))
always evaluated to false
. My bad.
Solution in Kotlin that I'm using at the moment:
@SuppressLint("PackageManagerGetSignatures")
@Suppress("DEPRECATION")
fun isSigned(context: Context?): Boolean {
return (context?.packageManager?.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES)?.signatures?.firstOrNull()?.toByteArray()
?.let {
return@let CertificateFactory.getInstance("X.509").generateCertificate(ByteArrayInputStream(it))
} as? X509Certificate)
?.issuerDN
?.name
?.contains("O=Android", ignoreCase = false) ?: true
}
that way I can still SIGN in debug and those will be reported to Crashlytics (example, for the QA process)
참고URL : https://stackoverflow.com/questions/7085644/how-to-check-if-apk-is-signed-or-debug-build
'IT박스' 카테고리의 다른 글
왜 SQL을 좋아하지 않습니까? (0) | 2020.07.22 |
---|---|
Xcode 8 Beta-현재 Swift 구문으로 변환 실패 : 테스트 호스트를 찾을 수 없습니다 (0) | 2020.07.22 |
div를 세로로 가운데에 배치하는 방법은 무엇입니까? (0) | 2020.07.22 |
사용자 지정 MSBuild 작업을 만들 때 C # 코드에서 현재 프로젝트 디렉토리를 얻는 방법은 무엇입니까? (0) | 2020.07.22 |
플레이스 홀더 Mixin SCSS / CSS (0) | 2020.07.22 |