2017-02-14 1 views
2

회사 프로젝트 용 단위 테스트 (robolectric 3.1 사용)를 쓰고 있습니다. 최근 커밋에서 android gradle plugin 버전이 2.1.0에서 2.2.3으로 업데이트되었습니다. 이 변경으로 인해 테스트에서 사용자 지정 글꼴을로드하는 코드가 호출 될 때마다 테스트가 실패하게됩니다.android gradle plugin upgrade 후 유닛 테스트 중에 사용자 정의 글꼴을로드 할 수 없습니다.

플러그인 버전을 2.1.0으로 변경하면 문제가 해결되지만 버전을 변경해야하며 계속 있어야한다고 들었습니다. 어쨌든 CI 빌드 환경은 테스트에 아무런 문제가 없으므로 문제는 내 로컬 빌드에서만 발생합니다.

JRE와 SDK 둘 다 (1.8 JAJ_HOME을 업데이트 된 JDK 디렉토리로 설정) 및 Android Studio를 아무런 문제없이 업데이트했습니다. 자산 디렉토리가 'res'디렉토리가 아닌 'main'에 올바르게 위치합니다 (글꼴은 app/src/main/assets/fonts /에 있습니다). 프로젝트를 정리하고 캐시 무효화로 IDE를 다시 시작하는 것도 도움이되지 않았습니다.

문제는 즉시 해제 실행 켠 후 지속, 그래서 여기에 설명 된 https://code.google.com/p/android/issues/detail?id=213454

build.gradle 아니다 :

dependencies { 
     (...) 
     classpath 'com.android.tools.build:gradle:2.2.3' 
} 
android { 
    compileSdkVersion 23 
    buildToolsVersion '23.0.2' 

    defaultConfig { 
     applicationId "com.example.app" 
     minSdkVersion 16 
     targetSdkVersion 22 
     multiDexEnabled true 
    } 

    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_8 
     targetCompatibility JavaVersion.VERSION_1_8 
    } 
    (...) 
} 

예제 코드를 사용하여 사용자 정의 글꼴 (레이아웃) :

<com.example.CustomFontTextView 
       (...) 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       custom:customFont="@string/custom_font_path"/> 

맞춤 f ONT 텍스트 뷰 :

public class CustomFontTextView extends TextView { 
    public CustomFontTextView(final Context context){ 
     super(context); 
    } 

    public CustomFontTextView (final Context context, final AttributeSet attrs){ 
     super(context, attrs); 

     if(!isInEditMode()){ 
      initTextView(context, attrs); 
     } 
    } 

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     if(!isInEditMode()){ 
      initTextView(context, attrs); 
     } 
    } 

    private void initTextView(final Context context, final AttributeSet attrs){ 

     TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomFontView); 
     String customFont = attributes.getString(R.styleable.CustomFontView_customFont); 
     setText(getText()); 
     if(customFont!=null){ 
      Typeface tf = getTypeface(context, customFont); 
      setTypeface(tf); 
     } 
     attributes.recycle(); 
    } 

    private Typeface getTypeface(final Context context, final String customFontPath) { 
     return Typeface.createFromAsset(context.getAssets(), customFontPath); 
    } 

    public void setTypeFace(Context con, String font){ 
     if(font != null && con != null){ 
      Typeface tf = getTypeface(con, font); 
      setTypeface(tf); 
     } 
    } 
} 

attrs.xml이 :

<declare-styleable name="CustomFontView"> 
    <attr name="customFont" format="string" /> 
</declare-styleable> 

폰트 경로가 문자열로 유지되고있다 :

예외

<string name="custom_font_path">fonts/CustomFont.ttf</string> 
은 (레이아웃 팽창시에 일어나는)

android.view.InflateException: XML file build\intermediates\res\merged\mock\debug\layout\layout.xml line #-1 (sorry, not yet implemented): Error inflating class <unknown> 
(...) 
Caused by: java.lang.RuntimeException: Font not found at [build\intermediates\bundles\mock\debug\assets\fonts\CustomFont.ttf] 

글꼴로드를 유발하는 원인 실패 할까? Robolectric은 책임이 있습니까?

+0

글꼴에서 찾을 수 없습니다 [\ 중간 \ 번들 \ 모의 \ 디버그 \ 자산을 구축 \ 글꼴 \ CustomFont.ttf] – petey

답변

3

그것은 Robolectric에게있어 참으로 내부 구현을 플러그인

https://github.com/robolectric/robolectric/issues/2647

안드로이드 Gradle을 2.2으로 변경되었지만 Robolectric 3.1은 여전히 ​​오래된 구현을 기반으로합니다. Robolectric 3.2에서 수정되었습니다. robolectric 업그레이드 외에도 솔루션은 otbinary에 의해 GitHub의에 제안도 마법처럼 작동합니다

android.applicationVariants.all { variant -> 
    def productFlavor = "${variant.productFlavors[0].name.capitalize()}" 
    def buildType = "${variant.buildType.name.capitalize()}" 
    tasks["compile${productFlavor}${buildType}UnitTestSources"].dependsOn(tasks["merge${productFlavor}${buildType}Assets"]) 
} 
This creates a dependency for all compile*UnitTestSources Gradle tasks on the corresponding merge*Assets tasks. The merge tasks copy all assets to app/build/intermediates/assets, where Robolectric finds them. 
관련 문제