Programing

ADT는 언제 BuildConfig.DEBUG를 false로 설정합니까?

crosscheck 2020. 8. 8. 10:40
반응형

ADT는 언제 BuildConfig.DEBUG를 false로 설정합니까?


최신 버전의 ADT (r17) BuildConfig.DEBUG에서는 빌드 유형에 따라 설정 되는 생성 된 상수가 추가되었습니다 . 내가 가진 문제는 그것이 결코 거짓으로 설정되지 않는다는 것입니다. "Android 도구-> 서명 된 응용 프로그램 패키지 내보내기"를 수행 할 때 변경 될 것으로 예상했지만 나에게는 그렇지 않았습니다.

그렇다면 빌드 유형을 어떻게 변경합니까?

디버그 모드에서만 일부 코드를 실행할 수있는 기능이 추가되었습니다. 이제 빌드는 빌드 유형에 따라 자동으로 설정되는 DEBUG 상수를 포함하는 BuildConfig라는 클래스를 생성합니다. 코드에서 (BuildConfig.DEBUG) 상수를 확인하여 디버그 전용 함수를 실행할 수 있습니다.


현재 "자동으로 빌드"를 비활성화하고 프로젝트를 정리 한 다음 "Android 도구-> 서명 된 애플리케이션 패키지 내보내기"를 통해 내 보내면 올바른 동작을 얻을 수 있습니다. 응용 프로그램을 실행할 때 BuildConfig.DEBUG거짓이어야합니다.


이클립스 , 릴리스에서 응용 프로그램을 내보내기 전에 항상 비활성화 "빌드 자동으로"옵션을 선택합니다. 그런 다음 프로젝트를 정리하고 내 보냅니다. 그렇지 않으면 디버그 모드에서 컴파일을 시작하고 BuildConfig.DEBUG의 값이 잘못되었을 수 있습니다.

Android Studio를 사용 하여 build.gradle에 고유 한 맞춤 변수를 추가하기 만하면됩니다.

buildTypes {
    debug {
        buildConfigField "Boolean", "DEBUG_MODE", "true"
    }
    release {
        buildConfigField "Boolean", "DEBUG_MODE", "false"
    }
}

프로젝트를 빌드 할 때 BuildConfig.java가 다음과 같이 생성됩니다.

public final class BuildConfig {
  // Fields from build type: debug
  public static final Boolean DEBUG_MODE = true;
}

그런 다음 내 코드에서 다음을 사용할 수 있습니다.

if (BuildConfig.DEBUG_MODE) {
    // do something
}

디버그 / 릴리스 빌드를 전환 한 후 청소하는 것이 좋습니다.


제대로 작동하지 않습니다.

문제 27940 : 내 보낸 응용 프로그램 패키지에 대해 BuildConfig.DEBUG가 "true"

때때로 버그가있는 기능을 릴리스하는 것은 실망 스럽습니다.


작동하지만 코드 파일은 서명 된 파일을 내보낼 때도 변경되지 않습니다. 내보내기 프로세스 는이 변수의 값을 false로 변경하여 작동하지 않는다는 잘못된 인상을 줄 수 있습니다. 나는 이것을 로깅 문으로 테스트했다.

if (com.mypackage.BuildConfig.DEBUG)
            Log.d(TAG, location.getProvider() + " location changed");

테스트 할 때 내 로그 문이 더 이상 출력을 생성하지 않습니다.


확인하십시오 imports. 때때로 BuildConfig 가 의도하지 않게 라이브러리의 모든 클래스에서 가져옵니다. 예를 들면 :

import io.fabric.sdk.android.BuildConfig;

이 경우 BuildConfig.DEBUG 는 항상 false를 반환합니다 .

import com.yourpackagename.BuildConfig;

이 경우 BuildConfig.DEBUG실제 빌드 변형을 반환합니다 .

추신 나는 여기 내 대답에서 이것을 복사합니다 : BuildConfig.DEBUG는 gradle로 라이브러리 프로젝트를 빌드 할 때 항상 false입니다.


에서 출시 준비 :

로깅 및 디버깅 끄기

릴리스 용 애플리케이션을 빌드하기 전에 로깅을 비활성화하고 디버깅 옵션을 비활성화해야합니다. 소스 파일에서 Log 메서드에 대한 호출을 제거하여 로깅을 비활성화 할 수 있습니다. 매니페스트 파일의 태그에서 android : debuggable 속성을 제거하거나 매니페스트 파일에서 android : debuggable 속성을 false로 설정하여 디버깅을 비활성화 할 수 있습니다. 또한 프로젝트에서 생성 된 모든 로그 파일 또는 정적 테스트 파일을 제거합니다.

Also, you should remove all Debug tracing calls that you added to your code, such as startMethodTracing() and stopMethodTracing() method calls.

More information is following the link.


The solution for me:

  1. Project -> Build Automatically
  2. Project -> Clean
  3. Project -> Build
  4. Project Export Android application

It's work in r20


I would want to propose a simple workaround if you use proguard during APK export.

Proguard provides a way to remove calls to specific functions in release mode. Any calls for debugging logs can be removed with following setting in proguard-project.txt.

# Remove debug logs
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

And optimization setting in project.properties.

proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt

With this, you don't need to concern any unnecessary String computation passing to debug log to which @Jeremyfa pointed. The computations are just removed in release build.

So the workaround for BuildConfig.DEBUG uses the same feature of proguard like following.

public class DebugConfig {

    private static boolean debug = false;

    static {
        setDebug(); // This line will be removed by proguard in release.
    }

    private static void setDebug() {
        debug = true;
    }

    public static boolean isDebug() {
        return debug;
    }
}

And following setting in proguard-project.txt.

-assumenosideeffects class com.neofect.rapael.client.DebugConfig {
    private static *** setDebug();
}

I would prefer using this to disabling the Build Automatically option, because this doesn't depend on the builder's individual IDE setting but is maintained as committed file which are shared among developers.


Does not work properly as far as I understood (Android issue 22241)

I had some trouble on a project (working with Eclipse), that constant was not set to true when exporting a signed APK of my project :(

Would love to hear it works though


a good way is creating your own class :

public class Log {

public static void d(String message) {
    if (BuildConfig.DEBUG)
        android.util.Log.d(
            "[" + (new Exception().getStackTrace()[1].getClassName()) + "]",
            "{" + (new Exception().getStackTrace()[1].getMethodName()) + "} "
            + message
        );
}

}

I've seen some strange behavior that has to do with when the values in BuildConfig are set to their final values. This may have something to do with your issue.

The simple explanation is that default values are set initially before Proguard is run, then after Proguard runs, the BuildConfig file is regenerated with the proper values. However, Proguard has already optimized your code by this point and you have issues.

Here is a bug I created against Gradle. https://code.google.com/p/android/issues/detail?id=182449

참고URL : https://stackoverflow.com/questions/9855834/when-does-adt-set-buildconfig-debug-to-false

반응형