2015-05-11 117 views
0

我創建了一個包含庫項目且運行良好的應用程序。但是,當我添加.jar到我的庫項目應用程序沒有發現這個.jar和構建過程崩潰。將外部.jar添加到Android庫項目

這些都是我.gradle文件:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 21 
    buildToolsVersion "22.0.1" 

    defaultConfig { 
     applicationId "org.gradiant.apps" 
     minSdkVersion 14 
     targetSdkVersion 21 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:22.1.1' 
    compile project(':a') 
} 

apply plugin: 'com.android.library' 

android { 
    compileSdkVersion 21 
    buildToolsVersion "22.0.1" 

    defaultConfig { 
     minSdkVersion 14 
     targetSdkVersion 21 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:22.1.1' 
    compile(name: 'b', ext: 'jar') 

} 

repositories { 
    flatDir { 
     dirs 'libs' 
    } 
} 

當一個是我的庫項目和b的新的.jar,我需要。並在logcat出現此消息:

Error:A problem occurred configuring project ':app'. 
> Could not resolve all dependencies for configuration ':app:_debugCompile'. 
    > Could not find :b:. 
    Searched in the following locations: 
     https://jcenter.bintray.com//b//b-.pom 
     https://jcenter.bintray.com//b//b-.jar 
    Required by: 
     NewApps:app:unspecified > NewApps:a:unspecified 

我該如何解決它?

+2

你有看看這個http://stackoverflow.com/a/16639227/1503155 –

回答

0

首先,在你的圖書館項目中,確保它是以Android的apks可以支持的二進制格式編譯的。例如,如果庫使用java 1.8編譯,而不是java 1.7(Android似乎需要),問題就會出現(目前對我來說)。

對於這一點,在你的庫項目build.gradle文件,你應該添加

allprojects { 
sourceCompatibility = 1.7 
targetCompatibility = 1.7 
} 

現在,在Android項目,編譯依賴必須添加到app/build.gradle(不是項目的根build.gradle),這樣:

dependencies { 
. . . 
compile project (":aProject") 
. . . 
} 

最後,你應該添加到根項目settings.gradle

include ":aProject" 
project(":aProject").projectDir = new File (settingsDir, "../../someFolderPath/aProject"); 

希望它有幫助。

+0

我有相同的結構,問題是我在我的庫項目中添加的.jar,如果我刪除jar構建成功。 – user3086708

+0

使用'JavaVersion.VERSION_1_7',而不是'1.7'。 –

相關問題