将外部.jar添加到Android库项目中

时间:2023-01-27 10:34:50

I create an app that include a library project and runs fine. But when I add .jar to my library project app not finds this .jar and build process crashes.

我创建了一个包含库项目的应用程序,运行正常。但是,当我将.jar添加到我的库项目应用程序时,找不到这个.jar并且构建进程崩溃了。

These are my .gradle files:

这些是我的.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'
    }
}

Where a is my library project and b the new .jar that I need. And in logcat appears this message:

其中a是我的图书馆项目,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

How can I fix it?

我该如何解决?

1 个解决方案

#1


First, in your library project, make it sure that it is compiled in a binary format that Android apks can support. For instance, problems arise (currently for me) if the library is compiled with java 1.8, instead of java 1.7 (what Android seems to need).

首先,在您的库项目中,确保它是以Android apks可以支持的二进制格式编译的。例如,如果库是用java 1.8编译的,而不是java 1.7(Android似乎需要),则会出现问题(目前对我而言)。

For this, in your library project build.gradle file, you should add

为此,在库项目build.gradle文件中,您应该添加

allprojects {
 sourceCompatibility = 1.7
 targetCompatibility = 1.7
}

Now, in the Android project, the compile dependency must be added to app/build.gradle (not to the project root build.gradle), like this:

现在,在Android项目中,必须将编译依赖项添加到app / build.gradle(而不是项目根build.gradle),如下所示:

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

And finally, you should add to the root project settings.gradle

最后,您应该添加到根项目settings.gradle

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

Hope it helps.

希望能帮助到你。

#1


First, in your library project, make it sure that it is compiled in a binary format that Android apks can support. For instance, problems arise (currently for me) if the library is compiled with java 1.8, instead of java 1.7 (what Android seems to need).

首先,在您的库项目中,确保它是以Android apks可以支持的二进制格式编译的。例如,如果库是用java 1.8编译的,而不是java 1.7(Android似乎需要),则会出现问题(目前对我而言)。

For this, in your library project build.gradle file, you should add

为此,在库项目build.gradle文件中,您应该添加

allprojects {
 sourceCompatibility = 1.7
 targetCompatibility = 1.7
}

Now, in the Android project, the compile dependency must be added to app/build.gradle (not to the project root build.gradle), like this:

现在,在Android项目中,必须将编译依赖项添加到app / build.gradle(而不是项目根build.gradle),如下所示:

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

And finally, you should add to the root project settings.gradle

最后,您应该添加到根项目settings.gradle

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

Hope it helps.

希望能帮助到你。