7

我正在設置一個Android庫與不同的productFlavors。 圖書館有味道。該文件被正確地設置好的了:未能在庫中使用productFlavors導入android支持v4或v7

src/main/java/com/example/...主類
src/full/java/com/example/...全類
src/light/java/com/example/...光類

Android Studio中正確地理解這一點,增加了(full)到韻味十足。

問題:okhttp這樣的依賴項按預期工作,但不是appcompat-v7。使用ViewPagerFragmentActivityRecyclerView一切。我試着加入依賴於fullCompile,但它也不能工作。的依賴是不是由Android工作室解決,進口不工作,除了確定okhttpexoplayer等。

我試過Invalidate Cache/Restart,clean Project,Resync gradle,沒有工作。

圖書館build.gradle

apply plugin: 'com.android.library' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.1" 

    ... 

    buildTypes { 
     release { 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
      minifyEnabled false 
     } 
    } 
    lintOptions { 
     abortOnError false 
    } 

    publishNonDefault true 

    productFlavors { 
     full { 
     } 
     light { 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:support-v4:23.1.1' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.squareup.okhttp:okhttp:2.3.0' 
    fullCompile 'com.android.support:support-v4:23.1.1' 
    fullCompile 'com.android.support:appcompat-v7:23.1.1' 
    fullCompile 'com.android.support:recyclerview-v7:23.1.1' 
    fullCompile 'com.squareup.picasso:picasso:2.5.0' 
} 

應用build.gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.1" 

    buildTypes { 
     release { 
     } 
    } 

    productFlavors { 
     full { 
     } 
     light { 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.+' 
    compile 'com.android.support:support-v4:23.+' 
    compile 'com.google.android.gms:play-services-base:7.5.0' 
    compile 'com.google.android.gms:play-services-ads:7.5.0' 
    compile 'com.google.android.gms:play-services-location:7.5.0' 
    compile 'com.android.support:recyclerview-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    fullCompile project(path: ':library', configuration: 'fullRelease') 
    lightCompile project(path: ':library', configuration: 'lightRelease') 
} 
+1

你能提供在gradle中產生的錯誤嗎? – droidpl

+0

生成應用程序時,它會顯示錯誤:(1073,64)錯誤:軟件包ViewPager不存在。如果我瀏覽這些類,則「ViewPager」導入不會被解析並導致該類中的錯誤。 @droidpl –

+0

如果你在某個地方使用viewPager,並且這個類不在相應的src/light或者src/full文件夾的完整/光照版本中,那麼它將不會被導入,因爲你在這個版本中使用了fullCompile圖書館可用。你能重新檢查這個文件崩潰的地方嗎? – droidpl

回答

3

您必須聲明中的Gradle應用程序的配置。當它與庫相關時,配置沒有正確聲明。嘗試:

configurations { 
    fullDebugCompile 
    fullReleaseCompile 
    lightDebugCompile 
    lightReleaseCompile 
} 

dependencies { 
... 
    fullDebugCompile project(path:":library", configuration:"fullDebug") 
    fullReleaseCompile project(path:":library", configuration:"fullRelease") 
    lightDebugCompile project(path:":library", configuration:"lightDebug") 
    lightReleaseCompile project(path:":library", configuration:"lightRelease") 
} 

龍解釋

的gradle這個Android插件使用的應用程序和庫,分別稱爲AppVariantLibraryVariant不同的實現。有時候,變體和構建類型的工作方式在這兩類項目中都有所不同。在這種情況下,前段時間庫始終是在一個給定的變型中的發佈版本類型,一件讓庫項目不是那麼靈活,因爲應用程序被編譯。

這就是爲什麼他們決定啓用publishNonDefault選項並在Android Studio中爲這種行爲提供支持,因此您可以在不同版本的應用程序中使用庫的不同版本,但必須指定使用哪種版本哪個庫。這是使你明確聲明configurations的原因。

由Android組建團隊工具使用的約定名稱爲{buildType}{flavor}TaskName,因此對於類路徑配置,你必須使用相同的名稱。

所有這個過程都有一個缺點,就是如果你發佈非默認依賴關係,android插件將確保所有可能的庫配置都是在應用程序構建之前編譯的,所以構建時間可以增加一點在庫大小上)