program story

디버그 빌드 변형에만 Stetho 포함

inputbox 2020. 12. 27. 10:50
반응형

디버그 빌드 변형에만 Stetho 포함


내가 사용할 수 있다는 것을 알고 debugCompileA의 전용 풀에 dependency을 위해 debug build. code initialization필요한 작업을 수행 할 수있는 효율적이고 효율적인 방법이 있습니까? 종속성이 없으면 다른 변형은 컴파일되지 않습니다.


몇 가지 옵션이 있습니다.

옵션 1 : 모든 빌드에 Stetho를 포함하고 ( compile대신 사용 debugCompile) Application디버그 빌드를 위해 클래스 에서만 초기화 합니다.

이것은 매우 쉽습니다. 당신의에 Application클래스, 체크 BuildConfig.DEBUG같은 :

if (BuildConfig.DEBUG) {
    Stetho.initialize(
            Stetho.newInitializerBuilder(this)
                    .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                    .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                    .build()
    );
}

옵션 2 : 디버그 빌드 용으로 Stetho 만 포함 Application하고 디버그 및 릴리스 빌드 용으로 다른 클래스를 만듭니다.

Gradle 덕분에 애플리케이션은 다양한 빌드 변형에 대해 서로 다른 소스 세트를 가질 수 있습니다. 기본적으로 릴리스 및 디버그 빌드 유형이 있으므로 세 가지 소스 세트를 가질 수 있습니다.

  • 디버그 빌드에서만 원하는 코드에 대해 디버그
  • 릴리스 빌드에서만 원하는 코드 릴리스
  • 모든 빌드에서 원하는 코드의 기본

애플리케이션 코드는 현재 모두 main소스 세트에 있습니다. 애플리케이션 debugmain폴더 옆에 라는 새 폴더를 만들고 main디버그 빌드에 추가하려는 모든 항목에 대해 폴더 구조를 미러링 할 수 있습니다.

이 경우 Stetho를 전혀 참조하지 않는 소스 세트 Application클래스 를 원합니다 main.

그런 다음 평소처럼 Stetho를 초기화하는 소스 세트 Application클래스 를 원합니다 debug.

Stetho 샘플 에서이 설정의 예를 볼 수 있습니다 . 특히 여기에 기본 Application 클래스 가 있고 여기에 디버그 Application 클래스가 있습니다. 또한 사용할 Application 클래스를 선택하는 각 소스 세트에 매니페스트를 설정합니다.


@Tanis 답변을 확인하십시오.

또한 다음과 같이 사용할 수 있습니다.

디버그 버전 에서만 라이브러리를 추가하십시오 .

dependencies {
   debugCompile 'com.facebook.stetho:stetho:1.1.1      
 }

응용 프로그램에서 다음을 수행 할 수 있습니다.

public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    StethoUtils.install(this);
  }
}

그런 다음 StethoUtils디버그 / 릴리스 버전에서 다른 클래스를 만들 수 있습니다 .

src/debug/java/

public class StethoUtils{

   public static void install(Application application){
       Stetho.initialize(
          Stetho.newInitializerBuilder(application)
            .enableDumpapp(Stetho.defaultDumperPluginsProvider(application))
            .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(application))
            .build());

   }
}

src/release/java/

public class StethoUtils{

   public static void install(Application application){
      // do nothing
   }
}

자바 리플렉션을 사용하는 것은 당연한 아이디어 일 수 있습니다.

private void initStetho() {
            if (BuildConfig.DEBUG) {
                try {
                   Class<?> stethoClazz = Class.forName("com.facebook.stetho.Stetho");
                    Method method = stethoClazz.getMethod("initializeWithDefaults",Context.class);
                    method.invoke(null, this);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }

그런 다음 컴파일 stetho를 디버그 할 수 있습니다.

debugCompile 'com.facebook.stetho:stetho:1.5.0'

ReferenceURL : https://stackoverflow.com/questions/30172308/include-stetho-only-in-the-debug-build-variant

반응형