0

I am writing an android SDK. I am using gradle and android studio.

Legend:

  • X.jar - a 3rd party jar file.
  • Y.jar - my final android SDK jar file, dependence on X.jar.
  • someApp - an Android app that uses/dependence on Y.jar.

The Problem:

someApp can not recognize classes in X.jar

I've searched (for almost a day) ways to generate Y.jar with also classes of X.jar (instead of just including X.jar file inside) but unfortunately I've failed. Many solutions I found were not clear enough or didn't fit my case.

I would very much appreciate your help and guidance.

Here is my relevant code snippet (based on this answer):

build.gradle code of my SDK project - creating Y.jar:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

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

    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
            }
            resources {
                srcDir 'src/../libs'
            }
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile files('libs/X.jar')
}

task clearJar(type: Delete) {
    delete 'build/outputs/Y.jar'
}

task makeJar(type: Copy) {

    from('build/intermediates/bundles/release/')
    into('build/outputs/')
    include('classes.jar')
    rename ('classes.jar', 'Y.jar')
}

makeJar.dependsOn(clearJar, build)

running 'gradlew makeJar' generates Y.jar in build/outputs directory.

Community
  • 1
  • 1
Gilu
  • 1
  • 1
  • As a tangent, you shouldn't need to make a separate `clearJar` task. Gradle will auto-create the task `cleanMakeJar`, which will delete the Outputs of the `makeJar` task. Also, having `makeJar` depend on `clearJar` is very inefficient, since it will negate Gradle's attempts to only execute a task if the inputs or outputs have changed (see http://gradle.org/docs/current/userguide/more_about_tasks.html#incrementalTask) – Jolta Mar 15 '15 at 22:29
  • And why do you use `'src/../libs'` instead of just using `libs`? – Jolta Mar 15 '15 at 22:34
  • @Jolta everything you say is correct. but still not answering my question :( – Gilu Mar 16 '15 at 08:41
  • Well, I don't have that answer, so I do what I can instead... – Jolta Mar 16 '15 at 09:44

1 Answers1

0

Well, just for those of you that will have a similar question I'm adding here a quick "work-around" that helped me manage my problem.

** this probably not the "official" way but it worked for me.

What I eventually did is:

  1. Extract all files from X.jar. After doing so I had this files hierarchy: com\X\sdk.
  2. Put 'com' folder in 'libs' folder.

note: make sure you have resources deceleration like that:

sourceSets {
    main.java.srcDir 'src/main/java'
    main.resources.srcDir 'libs'
}

and that is it. all I've left to do is call 'gradlew makeJar' and now my Y.jar includes X.jar classes as well.

In case someone has a better solution please share :)

Gilu
  • 1
  • 1