Create an AAR with multiple AARs/JARs - android-gradle-plugin

I have seen questions (Android Studio combine 2 .aar into one and others) posted by various developers but I haven't seen a definitive response that enables me to create an AAR that includes 1 or more AARs or JARs (I can do with JARs since I don't need to share any resources; only classes). Here is the app.gradle for my library project:
apply plugin: 'com.android.library'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.google.code.gson:gson:2.1'
compile ('libs/eventbus.jar')
compile project(':core-release')
compile project(':midware-release')
}
Again, this app is a library project which needs two other library projects ('core-release', 'midware-release') and while I was able to generate one AAR file that I can use in my application, the application was unable to find the dependent library projects' classes so, I had to add the two library projects' AARs into my application.
Here is the app.gradle application project (without adding the JARs manually) which is unable to find the dependent projects' classes:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.app.sample"
minSdkVersion 19
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.1'
compile 'com.android.support:design:23.1.1'
compile files('libs/eventbus.jar')
compile project(':sdk3-debug')
}
I don't think the library project's AAR file is pulling in the dependent projects (AAR or JAR) and hence the application is unable to find the classes.
I read about transitive dependency, but I was unable to find an example implementation which may help with my situation.

I haven't seen a definitive response that enables me to create an AAR that includes 1 or more AARs or JARs.
Yes, I think because this topic is not limited to AAR or JAR, but how Maven manage dependency.
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
while I was able to generate one AAR file that I can use in my application, the application was unable to find the dependent library projects' classes so, I had to add the two library projects' AARs into my application.
It's not your AAR responsibility to include your dependencies, your POM file should include information about dependencies.
https://maven.apache.org/pom.html
I don't think the library project's AAR file is pulling in the dependent projects (AAR or JAR) and hence the application is unable to find the classes.
Correct, you still need to include libraries dependency in your Application.
I assume you want your library can be used by Application, without specifying your library dependencies core-release and midware-release. I made a full explanation here android studio generate aar with dependency but here is what you need to do:
Upload core-release and midware-release to your Maven repository
Create a POM file for your library that include your dependencies
<project>
<modelVersion>4.0.0</modelVersion>
<parent>...</parent>
<artifactId>okhttp</artifactId>
<name>OkHttp</name>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>core-release</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>midware-release</artifactId>
</dependency>
</dependencies>
<build>...</build>
</project>
Publish your AAR with that POM file
mvn deploy:deploy-file \
-DgroupId=com.example \
-DartifactId=your-library \
-Dversion=1.0.1 \
-Dpackaging=aar \
-Dfile=your-library.aar \
-DpomFile=path-to-your-pom.xml \
-DgeneratePom=true \
-DupdateReleaseInfo=true \
-Durl="https://mavenUserName:mavenPassword#nexus.example.com/repository/maven-releases/"
And then your Application can use your library. Gradle will download your library transitive dependencies automatically.

I was able to address the issue by following Stan Kurdziel 's suggestion: stackoverflow.com/questions/30052058/multiple-aar-files and here are steps I took to arrive at a solution:
I created an AAR which I published to the local maven repository
Now this AAR was accessible in the application project
This AAR also had reference to the classes in the core and midware libraries. I followed Stan Kurdziel's approach #2:
Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.
Hope this helps others that might run into similar issue.

This should fix your issue:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile files('libs/eventbus.jar')
compile project(':sdk3-debug') { transitive = true }
}
Include the transitive flag at true.

Related

react-native link adds compile to gradle script, but compile support soon gone

Whenever I use react-native link, it adds the following to my app gradle file.
Image from Android Studio Gradle File
Then when I compile this, Android Studio tells me that "compile" is being deprecated and won't be supported after 2018. My question is, what would the correct replacement be for, for instance, react-native-ionicons?
The section of the gradle file in question is as follows.
dependencies {
compile project(':react-native-vector-icons')
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.facebook.react:react-native:+'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.squareup.okhttp3:okhttp:3.4.0-RC1'
implementation 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
testImplementation 'junit:junit:4.12'}
It was due to react-native link not having been updated but as of React-Native 0.58.3 the linking procedure has been updated so that compile and api have been replaced with implementation. Which you can see at this pull request
You could also just change compile to implementation in your grade file.
However you may find that some of the build.gradle files for the dependencies still use compile and api, unfortunately they only way to fix warnings from those is to make a pull request on the dependency changing compile and api to implementation

Error:Unable to resolve version for dependency 'org.jetbrains.kotlin:kotlin-stdlib-jre7:jar'

I have a project written in java I am integrating kotlin with using gradle. I am trying to follow https://kotlinlang.org/docs/reference/using-gradle.html
My build.gradle
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "kotlin"
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7"
}
according to Kotlin Error : Could not find org.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7 the library is only available in kotlin v1.1 and up. When I deploy this library and use it in my android project I get the error in the title.
you need to replace
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7"
with
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
Starting with Kotlin 1.1.2 and if you're targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library which contain additional extension functions for APIs added in new JDK versions.
Use one of the following dependencies:
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

IntelliJ root project showing gradle dependencies from modules as not resolvable

This might just be an IntelliJ bug, and it doesn't keep anything from working, but it is driving me crazy. The root project in IntelliJ shows the dependencies for the submodules are not resolvable, but they are resolving just fine. Is there something I am missing in the root project's gradle file?
Note: the catalog-core, common, and dal modules have no dependencies yet, however legacy-db and mongo-db do have dependencies. So literally every single dependency from the sub-projects resolves in its own project but not in the larger root project.
build.gradle for AdminTools
buildscript {
repositories {
mavenCentral()
}
dependencies {
}
}
group 'com.example'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
dependencies {
compile project( ":catalog-admin" )
}
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
}
task wrapper(type: Wrapper) {
gradleVersion = '4.0.1'
}
settings.gradle
rootProject.name = 'AdminTools'
include 'catalog-core'
include 'catalog-admin'
include 'catalog-common'
include 'catalog-dal'
include 'catalog-dal:legacy-db'
include 'catalog-dal:mongo-db'
Screenshot of Gradle Tool Window
Yup, adding the repositories block to the main project, with all of the repositories from all of the subprojects, did the trick. Thanks!
PS - It did build from command line at the root project level, although strangely 'gradle dependencies' didn't work. You'd think the root project would simply conglomerate all the repositories from subprojects but I guess not.

How to add Guava lib to runtime classpath in Intellij idea

I am trying to make use of Guava in an Intellij Gradle project and although my code compiles, at runtime I am seeing this >
java.lang.NoClassDefFoundError: com/google/common/collect/Ordering
I have read a number of posts suggesting that the Guava lib is not on the classpath at runtime. I have verified this by writing the classpath out using System.getProperty("java.class.path") and the Guava lib is not there.
I have tried (as others have suggested) to add either the folder or the lib itself as a Module dependency with scope = compile. See image >
Intellij Module Dependencies
This also has not worked.
Here are the libs used in the project >
Project Libs
Any advice on what is going on here gratefully accepted.
My build.gradle >
group 'EE'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
It contains no reference to the guava lib I added to the Project Libraries:: com.google.guava:guava:18.02
Looks like I have missed an important step somewhere.
Per official documentation from Guava.
https://github.com/google/guava/wiki/UseGuavaInYourBuild
Add the line below to your build.gradle file dependencies section:
dependencies {
compile group: 'com.google.guava',
name: 'guava',
version: '23.5-jre' # or 23.5-android for the Android flavor
}

Kotlin from IntelliJ: NoClassDefFoundError: kotlin/jvm/internal/Intrinsics [duplicate]

I would like IntelliJ IDEA to have my libraries as "compile" scope instead of "provided" scope. This is a part of my gradle file:
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// Logging
compile 'ch.qos.logback:logback-classic:1.2.1'
compile 'com.getsentry.raven:raven-logback:7.8.2'
// BigQuery
compile 'com.google.api-client:google-api-client:1.20.0'
compile 'com.google.apis:google-api-services-bigquery:v2-rev227-1.20.0'
// Configuration management
compile 'commons-configuration:commons-configuration:1.10'
//Json
compile 'org.json:json:20160810'
//Kafka
compile "org.apache.kafka:kafka-clients:0.10.1.1"
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'org.assertj:assertj-core:3.0.0'
testCompile 'org.mockito:mockito-all:1.10.19'
}
task wrapper(type: Wrapper) {
gradleVersion = '3.4'
}
The scope always reverts to "provided" in the dependency tab which is very annoying:
I am running:
IntelliJ IDEA 2016.3.4
Build #IC-163.12024.16, built on January 31, 2017
JRE: 1.8.0_112-release-408-b6 x86_64
It's a known issue in IntelliJ IDEA that is specific to Gradle 3.4:
IDEA-167412 Gradle 3.4-rc-1 changes compile dependencies to provided
original bug report in the Gradle project with more details
comment from the responsible developer regarding "Create Module per source set" option and how Gradle integration works in IntelliJ IDEA
It's already fixed in 2017.1 EAP build.
You can use Gradle 3.3 or older as a workaround until IDEA 2017.1 is released.