Java – how to include jar dependencies in the AAR Library
Summary:
I have an AAR file that depends on the jar file. When I build an AAR project, it does not contain jar code
Details:
I have a Java SDK library project that contains our code for Java Web projects, and this library is created using gradle and resides in an internal nexus server (as a jar)
The goal is to provide the "Android configuration" version of the jar library through the AAR library that can be used by multiple Android applications, and minimize the workload (and template code) of implementing it The AAR file is also uploaded to the nexus server for Android application projects
My AAR project includes gradient dependencies on my java SDK Library (jar), but at build time, AAR does not include any classes
Code:
This is the graduation file of the Java SDK project:
apply plugin: 'java' //noinspection GroovyUnusedAssignment sourceCompatibility = 1.7 version = '1.1.1' repositories { mavenCentral() } jar { from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } } dependencies { testCompile group: 'junit',name: 'junit',version: '4.11' testCompile 'org.apache.directory.studio:org.apache.commons.io:2.4' compile 'org.springframework:spring-web:3.1.1.RELEASE' compile 'com.google.code.gson:gson:2.3' }
This is the graduation document of my AAR project. Please note that I deleted the Maven repository declaration from it to my nexus server I don't think this question is important
apply plugin: 'com.android.library' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "2.2.2" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' } } lintOptions { abortOnError false } } dependencies { compile fileTree(dir: 'libs',include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' compile ('com.mycompany:javasdk:1.1.1') }
This is the graduation document of my android project. I deleted the nexus server reference again:
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.mycompany.application1" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs',include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' compile ('com.mycompany:androidsdk:2.2.2@aar') }
Note: I initially solved the problem by adding jars to the Lib directory of the AAR project, but this is not required It makes a nexus server useless This will be a good thing. We can find the jar version number in the gradle file of the AAR project, and it will be updated automatically at compile time
Note 2: I tried to add transitional = true for my AAR dependency in the Android project, but did not solve any problems. The real problem is that the jar project code will not be bound when building the AAR project
Thank you for your time
Solution
You can add this task:
task copyLibs(type: Copy) { from configurations.compile into 'libs' }
Dependencies will be downloaded from your nexus, but when you need to package the library, perform this task first and copy the jar file and include it in final AAR