0

i have a module written in kotlin, now i have to make this module into a jar. I have to include kotlin-stlib-1.3.50.jar into my jar, so that it can run successfully in android. But there is a question is that the app which calls my jar is also written in kotlin, and it has a different version of kotlin-stdlib. So how can I solve it?

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
plugins {
id('com.android.library')
id('kotlin-android')
id('kotlin-android-extensions')
}


android {
compileSdkVersion 28
defaultConfig {
    minSdkVersion 27
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

sourceSets {
    main {
        aidl.srcDirs = ['src/main/aidl']
    }
}

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}

}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//    implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41'
implementation 'com.squareup.okio:okio:2.2.2'
implementation 'androidx.fragment:fragment:1.0.0'
implementation 'com.nex3z:notification-badge:1.0.2'
implementation files('libs/framework.jar')
implementation 'com.nex3z:notification-badge:1.0.2'
implementation 'com.android.support:support-annotations:28.0.0'
}
repositories {
mavenCentral()
}



task createJar(type: Jar){
println(configurations.compile.asPath)
archiveName 'wifisdkn_7_4_2.jar'
from('build/intermediates/javac/release/compileReleaseJavaWithJavac/classes')
from('build/tmp/kotlin-classes/release')

from {
    (zipTree('/home/mi/.gradle/caches/transforms-2/files-2.1/d6bc0306f3ac2741d6328b688ed7b145/jars/classes.jar'))
}

from {
    (zipTree('/home/mi/.gradle/caches/modules-2/files-2.1/com.squareup.okio/okio/2.2.2/36f483536153f15339a8b48d508e22be7c9c531a/okio-2.2.2.jar'))
}


from{
    (zipTree('/home/mi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.3.41/e24bd38de28a326cce8b1f0d61e809e9a92dad6a/kotlin-stdlib-1.3.41.jar'))
}

destinationDir = file('build/libs')
}

createJar.dependsOn(build)
huiwen hu
  • 11
  • 2
  • Hi and welcome to StackOverflow. Do you need to add your dependencies to your jar file so that they are resolved automatically? Are you familiar with the POM files? – Saeed Entezari Oct 09 '19 at 07:58

1 Answers1

0

You don't have to write any code for that. just follow the below steps:

  1. open the Gradle projects pane on the right side

    Gradle projects pane

  2. Goto the module for which you want to build the jar. Expand the Tasks node.

    Task node

  3. Expand the Build folder and double click on the Build action.

    build action

  4. The build process will be started. Once the process is completed, JAR/AAr will be generated.

  5. Goto the module directory, then follow the path build -> libs -> .jar

Jaymin
  • 2,879
  • 3
  • 19
  • 35
  • hello Jaymin thanks for your answer, i did what you said. But it doesn't work, there is no such path build -> libs -> .jar in the module directory. And i also want to know is there any difference between in this way and build in gradle task? – huiwen hu Oct 10 '19 at 01:40
  • There is no difference. You do not have to code if you approach my way. what kind of module are you trying to build the JAR? it should be **Java library**. You can use the **.aar** file as well. the same process will generate the **.aar** file as well. – Jaymin Oct 10 '19 at 04:27
  • My module is android library, it doesn't generate either .jar or .aar. Build directory doesn't contain lib directory. – huiwen hu Oct 10 '19 at 06:01
  • It should work. The same thing is working for me. I've done this multiple times to generate the jar/aar with the same steps. – Jaymin Oct 10 '19 at 06:10
  • ok thanks I'll try again, but I think it doesn't solve my question. Maybe you know how to solve it? – huiwen hu Oct 10 '19 at 06:27
  • I think this question can help you. https://stackoverflow.com/questions/21712714/how-to-make-a-jar-out-from-an-android-studio-project – Jaymin Oct 10 '19 at 06:28
  • I can build a jar through gradle task, and the jar contains kotlin-stdlib.jar. When apps written in Kotlin calls my jar, there will be a confliction, because the version of kotlin-stdlib.jar between my jar and the apps is not the same – huiwen hu Oct 10 '19 at 06:30