Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 965
ntoskrnl Avatar asked Dec 08 '22 06:12

ntoskrnl


1 Answers

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.

like image 180
ntoskrnl Avatar answered Dec 28 '22 07:12

ntoskrnl