如何在gradle 3或更高版本中使用jar文件?

时间:2019-07-18 06:09:29

标签: java android gradle

在我的项目中,“ app”模块还使用放置在其依赖项模块之一中的jar库。当我使用Gradle 2和Gradle Plugin 2.3时,它工作正常。然后,我将Gradle版本更新为5.1.1,并将Android Gradle插件版本更新为3.4.2,并遇到以下问题。

如果将jar放在两个模块的libs文件夹中,则会出现错误,如第一个屏幕截图所示。但是,如果我在“ app”模块中删除了jar,则它无法识别出jar中的类,如第二个屏幕截图所示。当我使用旧的Gradle时,它能够使用那些类。


Error when putting the jar in both modules


Error when removing the jar from app module


dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13-beta-3'
    implementation 'me.zhanghai.android.materialratingbar:library:1.3.2'

    implementation 'com.flurry.android:analytics:11.6.0@aar'

    implementation project(':dayWeekLib')
    implementation (project(':zappasoft-android-support-library'))
//    {
//        exclude module: 'libs/YouTubeAndroidPlayerApi'
//        exclude group: 'com.google.android.youtube.player'
//    }

    implementation ('androidx.appcompat:appcompat:1.0.2')

    implementation ('com.google.android.material:material:1.0.0')

    implementation ('androidx.recyclerview:recyclerview:1.0.0')

    implementation ('androidx.legacy:legacy-support-v13:1.0.0')

    implementation 'org.apache.httpcomponents:httpcore:4.4.11'
    implementation 'org.apache:apache:21'
    implementation 'com.google.android.gms:play-services-plus:17.0.0'
    implementation 'com.google.apis:google-api-services-youtube:v3-rev112-1.19.0'
    implementation 'com.google.http-client:google-http-client-android:1.30.2'
    implementation 'com.google.api-client:google-api-client-android:1.30.2'
    implementation 'com.google.api-client:google-api-client-gson:1.30.2'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.okhttp3:okhttp:4.0.1'

    implementation 'com.daimajia.swipelayout:library:1.2.0@aar'
    implementation 'com.github.barteksc:android-pdf-viewer:+'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31'
    implementation 'se.emilsjolander:stickylistheaders:2.7.0'
    implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

    implementation ('androidx.core:core:1.0.0')

    implementation 'org.greenrobot:greendao:3.2.2'

    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.github.chrisbanes:PhotoView:2.1.0'
    implementation 'com.opencsv:opencsv:4.4'
    implementation 'xmlwise:xmlwise:1.2.11'

    implementation 'com.google.firebase:firebase-core:17.0.1'
    implementation 'com.google.firebase:firebase-messaging:19.0.1'
}

2 个答案:

答案 0 :(得分:0)

  

我认为您的实现是错误的。首先你要放罐子   文件在您的libs文件夹中。然后在您的gradle中添加implementation files('libs/your_jar.jar')

答案 1 :(得分:0)

在项目的lib文件夹中添加aar之后,必须在项目根目录中添加以下行build.gradle

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

并将下面的行添加到gradle.build模块(例如app,...。)

implementation files('lib-name-x.x.x.jar') 

否则使用自爆代码

 implementation fileTree(dir: 'libs', include: '*.jar'))