Gradle로 라이브러리 프로젝트를 빌드 할 때 BuildConfig.DEBUG는 항상 false입니다.
디버그 모드에서 앱을 사용할 때 BuildConfig.DEBUG가 작동하지 않습니다. Gradle을 사용하여 빌드합니다. 이 검사를 수행하는 도서관 프로젝트가 있습니다. BuildConfig.java는 빌드 디버그 폴더에서 다음과 달라집니다.
/** Automatically generated the file. DO NOT MODIFY */
package common.myProject;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
}
릴리스 폴더에서 :
public static final boolean DEBUG = false;
라이브러리 프로젝트와 애플리케이션 프로젝트 모두에서.
내 프로젝트의 클래스를 설정하는 변수를 확인하여 해결했습니다. 이 클래스는 라이브러리에서 상속 시작시 시작됩니다.
<application
android:name=".MyPrj" ...
이로 인해 또 다른 문제가 발생합니다. 응용 프로그램 클래스 이전에 실행되는 DataBaseProvider에서 DEBUG 변수를 사용하고 제대로 실행되지 않습니다.
이것은 예상되는 동작입니다.
라이브러리 프로젝트는 다른 프로젝트 또는 모듈에서 사용할 수 릴리스 릴리스 변형 만 게시합니다.
우리는이 문제를 해결하기 위해 노력하고이를 사소한 업무가 필요합니다.
https://code.google.com/p/android/issues/detail?id=52962 에서 문제를 추적 할 수 있습니다.
Android Studio 1.1과 1.1 버전의 gradle 버전을 사용하면 다음이 가능합니다.
도서관
android {
publishNonDefault true
}
앱
dependencies {
releaseCompile project(path: ':library', configuration: 'release')
debugCompile project(path: ':library', configuration: 'debug')
}
완전한 문서는 http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication 에서 완전한 문서를 사용할 수 있습니다.
수정 :
이 문제 는 Android Studio Gradle 버전 3.0에서 수정 된 표시되었습니다. 거기에서 사용할 수있는 implementation project(path: ':library')
올바른 구성을 자동으로 선택합니다.
imports
여기서 BuildConfig 가 의도하지 않게 라이브러리의 모든 클래스에서 가져 오기를 확인하십시오 . 예를 들면 :
import io.fabric.sdk.android.BuildConfig;
이 경우 BuildConfig.DEBUG 는 항상 false를 반환합니다 .
import com.yourpackagename.BuildConfig;
이 경우 BuildConfig.DEBUG 는 실제 빌드 변형을 반환합니다 .
이것은 점을 제외하면 필요하지 않습니다.
private static Boolean sDebug;
/**
* Is {@link BuildConfig#DEBUG} still broken for library projects? If so, use this.</p>
*
* See: https://code.google.com/p/android/issues/detail?id=52962</p>
*
* @return {@code true} if this is a debug build, {@code false} if it is a production build.
*/
public static boolean isDebugBuild() {
if (sDebug == null) {
try {
final Class<?> activityThread = Class.forName("android.app.ActivityThread");
final Method currentPackage = activityThread.getMethod("currentPackageName");
final String packageName = (String) currentPackage.invoke(null, (Object[]) null);
final Class<?> buildConfig = Class.forName(packageName + ".BuildConfig");
final Field DEBUG = buildConfig.getField("DEBUG");
DEBUG.setAccessible(true);
sDebug = DEBUG.getBoolean(null);
} catch (final Throwable t) {
final String message = t.getMessage();
if (message != null && message.contains("BuildConfig")) {
// Proguard obfuscated build. Most likely a production build.
sDebug = false;
} else {
sDebug = BuildConfig.DEBUG;
}
}
}
return sDebug;
}
해결 방법으로 리플렉션을 사용하여 라이브러리가 아닌 앱에서 필드 값을 가져 오는 방법을 사용할 수 있습니다.
/**
* 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;
}
DEBUG
예를 들어 필드 를 가져 오려면 다음에서 호출하십시오 Activity
.
boolean debug = (Boolean) getBuildConfigValue(this, "DEBUG");
또한 AOSP Issue Tracker 에서이 솔루션을 공유했습니다 .
디버그 버전인지 확인하는 올바른 방법은 어느 쪽을 통해 앱 자체가 가능한지 확인할 수 있습니다.
private static Boolean sIsDebuggable;
public static boolean isDebuggable(Context context) {
if (sIsDebuggable == null)
sIsDebuggable = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
return sIsDebuggable;
}
앱과 라이브러리의 기본 동작은 완벽하게 일치합니다.
더 나은 해결 방법이 필요한 경우 다음을 대신 사용할 수 있습니다.
public static boolean isInDebugFlavour(Context context) {
if (sDebugFlavour == null) {
try {
final String packageName = context.getPackageName();
final Class<?> buildConfig = Class.forName(packageName + ".BuildConfig");
final Field DEBUG = buildConfig.getField("DEBUG");
DEBUG.setAccessible(true);
sDebugFlavour = DEBUG.getBoolean(null);
} catch (final Throwable t) {
sDebugFlavour = false;
}
}
return sDebugFlavour;
}
gradle을 사용하여 각 빌드 유형에 대해 고유 한 BuildConfig 클래스를 만들 수 있습니다.
public class MyBuildConfig
{
public static final boolean DEBUG = true;
}
대한 /src/debug/.../MyBuildConfig.java 및 ...
public class MyBuildConfig
{
public static final boolean DEBUG = false;
}
대한 /src/release/.../MyBuildConfig.java
그런 다음 다음을 사용하십시오.
if (MyBuildConfig.DEBUG)
Log.d(TAG, "Hey! This is debug version!");
여기 또 다른 해결책이 있습니다.
1) 인터페이스 생성
public interface BuildVariantDetector {
boolean isDebugVariant();
}
2) Application 클래스 (Appplication 모듈)에서이 인터페이스 사용
public class MyApplication extends Application implements BuildVariantDetector {
@Override
public boolean isDebugVariant() {
return BuildConfig.DEBUG; //application (main module) Buildonfig
}
}
3) 그리고 라이브러리 모듈에서 :
boolean debugVariant = ((BuildVariantDetector)getApplication()).isDebugVariant();
우리는 같은 문제가있었습니다. 나는 다음과 같은 것을 생각 해냈다.
SDK (라이브러리)와 데모 프로젝트가 있으며 계층 구조는 다음과 같습니다.
Parent
|
+ SDK (:SDK)
|
+ DemoApp (:DemoApp)
우리가 가지고있는 데모 애플리케이션을 위해이었다 :SDK:jarjarDebug
및 :SDK:jarjarRelease
일부 특정 작업입니다 :SDK
그 생산 일부 사후 처리 항아리 :
dependencies {
debugCompile tasks.getByPath(":SDK:jarjarDebug").outputs.files
releaseCompile tasks.getByPath(":SDK:jarjarRelease").outputs.files
... more dependencies ...
}
이것은 buildTypes
한 번에 빌드 된 여러 개에서도 작동합니다 . 하지만 디버깅은 조금 어렵습니다. 의견을주세요.
이것은 내 해결 방법입니다. 앱 모듈의 BuildConfig를 반영합니다.
`public static boolean debug = isDebug ();
private static boolean isDebug() {
boolean result = false;
try {
Class c = Class.forName("com.example.app.BuildConfig");
Field f = c.getField("DEBUG");
f.setAccessible(true);
result = f.getBoolean(c);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return result;
}`
각 프로젝트 buildTypes에서 이것을 시도 할 수 있습니다.
parent.allprojects.each{ project -> android.defaultConfig.debuggable = true}
제 경우에는 BuildConfig
프로젝트에 라이브러리 모듈이 많기 때문에 잘못 가져 왔습니다. 수정 사항은 BuildConfig
내 app
모듈에 대한 올바른 정보를 가져 오는 것 입니다.
gradle 파일에서 debuggable true로 작업합니다.
buildTypes {
demo{
debuggable true
}
live{
debuggable true
}
}
BuildConfig.DEBUG는 전혀 신뢰할 수 없으며 Android는 빌드가 디버그 모드인지 비디 버그 모드인지를 나타내는 전역 적으로 사용 가능한 내부 플래그를 제공했습니다.
(getContext().getApplicationInfo().flags &ApplicationInfo.FLAG_DEBUGGABLE) != 0)
디버그 상태이면 true가됩니다.
크레딧 : https://medium.com/@elye.project/checking-debug-build-the-right-way-d12da1098120
'ProgramingTip' 카테고리의 다른 글
저장 후 몽구스 채우기 (0) | 2020.10.04 |
---|---|
숫자 만 허용되는 입력을 제한 귀하가 허용합니까? (0) | 2020.10.04 |
redux에서 특정 배열 항목 내부의 단일 값을 업데이트하는 방법 (0) | 2020.10.04 |
elisp에서 운영을 확인하는 방법은 무엇입니까? (0) | 2020.10.04 |
Jquery는 요소가 뷰포트에 보이는지 확인합니다 (0) | 2020.10.04 |