2017-03-16 25 views
0

我有一個Android庫項目(RealCoolLib)。 RealCoolLib的用戶有時需要打包在aar中的依賴關係,有時他們希望以正常的gradle方式使用它。我的構建gradle看起來像這樣:如何在不同的android構建版本之間共享依賴關係?

apply plugin: 'com.android.library' 
android { 
    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_COMPILE_SDK_VERSION) 
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION 
    defaultConfig { 
     minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION) 
     targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION) 
     versionCode 1 
     versionName 1.0.0 
    } 

    buildTypes { 
     debug { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
     release { 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
      consumerProguardFiles 'client-proguard.txt' 
     } 
     releaseNoPro { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
      consumerProguardFiles 'client-proguard.txt' 
     } 
     releaseWithJars { 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
      consumerProguardFiles 'client-proguard.txt' 
     } 
     releaseWithJarsNoPro { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
      consumerProguardFiles 'client-proguard.txt' 
     } 
    } 
} 

dependencies { 
     compile 'com.googlelib.everyone.needs.this:1.0.0' 

     debugCompile 'com.tools.debugversion:0.0.5' 
     debugCompile 'org.awesome.stuff.debug:2.0.0' 
     debugCompile 'com.another.tool.needed:3.0.0'   

     releaseCompile 'com.tools.releaseversion:1.0.0' 
     releaseCompile 'org.awesome.stuff:2.0.0' 
     releaseCompile 'com.another.tool.needed:3.0.0' 

     releaseNoProCompile 'com.tools.releaseversion:1.0.0' 
     releaseNoProCompile 'org.awesome.stuff:2.0.0' 
     releaseNoProCompile 'com.another.tool.needed:3.0.0' 

     releaseWithJarsProvided file('stuff/com_tools_releaseversion.jar') 
     releaseWithJarsCompile file('stuff/org_awesome_stuff.jar') 
     releaseWithJarsCompile file('stuff/com_another_tool_needed.jar') 

     releaseWithJarsNoProProvided file('stuff/com_tools_releaseversion.jar') 
     releaseWithJarsNoProCompile file('stuff/org_awesome_stuff.jar') 
     releaseWithJarsNoProCompile file('stuff/com_another_tool_needed.jar') 
} 

這導致了很多重複的依賴聲明。有沒有辦法創建一個單一的「單位」,它保存在jar 依賴關係的列表中,而另一個具有正常表示法?基本上我正在尋找一些方法來減少重複。運行​​當

configurations { 
    baseConfiguration 
    configurationA { 
     extendsFrom(baseConfiguration) 
    } 
    configurationB { 
     extendsFrom(baseConfiguration) 
    } 
} 

dependencies { 
    baseConfiguration('junit:junit:4.12') 
} 

然後你就可以看到configurationAconfigurationB包含相同的依賴關係baseConfiguration

回答

0

你可以做這樣的事情

baseConfiguration 
\--- junit:junit:4.12 
    \--- org.hamcrest:hamcrest-core:1.3 

configurationA 
\--- junit:junit:4.12 
    \--- org.hamcrest:hamcrest-core:1.3 

configurationB 
\--- junit:junit:4.12 
    \--- org.hamcrest:hamcrest-core:1.3 

文檔:extendsFrom

+0

我會給你一個鏡頭。 – agent8261

+0

所以試過了,但似乎是因爲android做了一些特殊的風味和變體,所以我只能做一個分組。 – agent8261

相關問題