How to exclude a jar-file from my Android library build target

落爺英雄遲暮 提交于 2019-12-09 13:30:28

问题


I am using Android Studio and have a several apps that rely on the same code. I moved shared code to a separate library in order to include it in my apps.

The library project (MyLib) that I created for this purpose requires a jar-file to compile, so I added it to project's libs directory.

My build.gradle of MyLib main module looks like this:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example.android"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {

        }
    }
}

dependencies {
    compile files('./libs/external-java-lib.jar')
}

When I build the project, gradle generates a jar-file that contains external-java-lib.jar.

I want my Android application projects to provide "external-java-lib.jar", not the MyLib. Because in my apps I may use different versions of external-java-lib.jar.

How do I configure Gradle to exclude external-java-lib.jar from the build of my library project?

I couldn't find the answer to my question on the net, so I have a feeling that the thing I want is a piece of bad design. What else can I do?

Thank you in advance.


回答1:


I finally solved my problem.

In my library project I added another empty libraries module and moved external-java-lib.jar to its libs folder.

In the build.gradle of the main module, I added dependency:

dependencies {
    compile project(':libraries')     
}

My directory structure is now:

MyLibrary
├── build.gradle
├── gradle.properties
├── main
│   ├── build.gradle
│   └── src
│       └── main
│           ├── AndroidManifest.xml
│           ├── java ...
│           └── res  ...
├── libraries
│   ├── build.gradle
│   ├── libs
│   │   ├── external-lib-1.jar
│   │   └── external-lib-2.jar
│   └── src
│       └── main
│           ├── AndroidManifest.xml
│           ├── java ...
│           └── res  ...
└── settings.gradle

After I build the library, the output aar-package had no external-java-lib.jar in it. So, this is exactly what I want.




回答2:


If you are not using proguard then you simply use provided tag

dependencies {
    provided files('./libs/external-java-lib.jar')
}



回答3:


i don not know about android studio but its easy in eclipse, in eclipse right click on the project-> properties->java build path there you have libraries and java build path. There you can exclude or include files.



来源:https://stackoverflow.com/questions/26576635/how-to-exclude-a-jar-file-from-my-android-library-build-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!