How to use local aar dependency? - android

i google about local aar,every one say it can work,but it don't work at android studio 1.1.0.
i try to use :
compile fileTree(dir: 'libs', include: ['*.aar'])
but it tip:
Warning:Project app: Only Jar-type local dependencies are supported. Cannot handle: /Users/kycq/AndroidStudioProjects/QingTaJiao/app/libs/KycqBasic-release.aar
how should i do to use local aar?
if i should use:
compile 'com.example.library:library:1.0.0#aar'
how to do this?

I was getting the same errors.
This was the only thing that helped me:
dependencies {
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}
repositories{
flatDir{
dirs 'libs'
}
}
Reference: Adding local .aar files to Gradle build using "flatDirs" is not working

In my case a have to distribute an aar. When i import the aar in another project this error appears.
I solve this problem distributing the aar with a maven dependency structure (pom, md5, etc...)
publishing {
publications {
maven(MavenPublication) {
groupId "<GROUP_ID>"
artifactId "<ARTIFACT_ID>"
version <VERSION>
artifact "$buildDir/outputs/aar/<AAR_FILE>"
pom.withXml {
def dependenciesNode = asNode().appendNode("dependencies")
configurations.compile.allDependencies.each { dependency ->
def dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", dependency.group)
dependencyNode.appendNode("artifactId", dependency.name)
dependencyNode.appendNode("version", dependency.version)
}
}
}
}
repositories {
maven {
url "$buildDir/repo"
}
}
}
On the android application i need to add the local maven repository:
allprojects {
repositories {
jcenter()
maven {
url "<LOCAL_REPO>"
}
}
}
And add the dependency:
compile("<GROUP_ID>:<ARTIFACT_ID>:<VERSION>#aar") {
transitive = true
}

Related

Packaging Library with another library aar in Kotlin(build.gradle.kts)

I am creating a library project (lets say B) and I have a dependency for another library (let's say A) which I have in the form of an aar (a.aar) file.
When I build b.aar should be packaged in a way that the a.aar will be bundled inside b.aar
Main Project is not controlled by us, so I do not have control to get a.aar while building the main project and they are looking for a way to bundle a.aar while we build b.aar.
Below is the b build.gradle.kts file.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.android.library")
kotlin("android")
id("gradle-****-android-module")
id("maven-publish")
}
group = "com.****.******"
version = "0.1-SNAPSHOT"
android {
compileSdk = 33
defaultConfig {
minSdk = 23
targetSdk = 33
consumerProguardFiles("proguard-rules.pro")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
}
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.4.1")
androidTestImplementation("androidx.test:runner:1.4.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0-alpha06")
testImplementation("junit:junit:4.12")
implementation("com.google.mlkit:image-labeling:17.0.7")
implementation("com.google.android.gms:play-services-mlkit-text-recognition:18.0.2")
implementation("com.google.android.gms:play-services-mlkit-image-labeling:16.0.8")
implementation("com.example.servicelibrary:servicelibrary:1.0")
}
And in my repositories section, I'm trying to load the a.aar file from local maven.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
val agpVersion: String by project
val repositoryUrl = "file:${rootProject.projectDir.absolutePath}/repository"
repositories {
maven(url = repositoryUrl)
mavenLocal()
google()
mavenCentral()
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
dependencies {
val pmcModuleVersion: String by project
val kotlinVersion: String by project
classpath("com.android.tools.build:gradle:$agpVersion")
classpath(kotlin("gradle-plugin", version = "$kotlinVersion"))
classpath("com.****.gradle.plugins:gradle-****-android-module:$pmcModuleVersion")
}
}
allprojects {
val repositoryUrl = "file:${rootProject.projectDir.absolutePath}/repository"
buildscript {
repositories {
mavenLocal()
google()
mavenCentral()
maven(url = repositoryUrl)
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
}
repositories {
mavenLocal()
google()
mavenCentral()
maven(url = repositoryUrl)
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
}
tasks {
val clean by registering(Delete::class) {
delete(builder)
}
}
Any inputs will be really helpful
In order to bundle LibraryA while building LibraryB, you will need to include LibraryA as a dependency in the build.gradle file of LibraryB.
Follow the steps mentioned below-
Paste ‘LibraryA’ inside the android’s project directory outside the app folder.
In the app level gradle i.e in 'build.gradle(:app)' file of LibraryB, add the following line to the dependencies section:
implementation project(':libraryA')
Make sure that the libraryA module is included in the 'settings.gradle' file of the project
include ':libraryA' //below include ':app'
If their are modules in LibraryA (Eg. Firebase or OneSignalConfig etc) and is mandatory to use then add the module's implementation also in the step 2 -
implementation project(':LibraryA:<module_name>') //add below implementation project(':libraryA')
example - implementation project(':LibraryA:Firebase')
Now try to build and run the app
IN CASE OF ANY DEPENDENCY ERROR WHILE BUILDING/RUNNING THE APP, TRY BELOW STEP
If using latest android structure then Add below code in 'settings.gradle' inside dependencyResolutionManagement{} and inside pluginManagement{}
repositories {
..
maven{url 'https://jitpack.io'} //add this below mavenCentral()
}
If you are using old android structure, then in project level gradle i.e in 'build.gradle(project_name)' add inside allprojects{}-
repositories {
..
maven{url 'https://jitpack.io'} //add below jcenter() and google()
}

I cannot declare import for gradle dependency of my just published library

UPDATE2: I was not generating a aar file, now it's included in the package that you can check here: https://github.com/fabrizioiacobucci/range-bar-preference/packages/787218
I see javadoc, sources and aar are there, but when I add the package as dependency in another project I don't even see it in the External Libraries.
UPDATE: This is the problem, I don't see my source files in the downloaded jar:
I recently forked a little project on Github and migrated its old code version to AndroidX and new gradle build.
After some time everything work fine and I was able also to publish the library on Git packages.
However, I tried to declare it as dependency on a different project on my local computer. It downloads fine but when I try to import it in a source file, I cannot find the package:
If I go into the project folder I see the library downloaded and related files.
This is the Project build.gradle file of the published library.
build.gradle (project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
}
}
plugins {
id 'maven-publish'
}
apply from: "${rootDir}/scripts/publish-root.gradle"
apply plugin: 'io.github.gradle-nexus.publish-plugin'
ext {
VERSION = '1.0.0'
DESCRIPTION = 'A range bar that can be used as an android shared preference'
GROUPID = 'com.fabrizioiacobucci.android'
ARTIFACTID = 'range-bar-preference'
GITREPO = 'https://github.com/fabrizioiacobucci/tree/development/range-bar-preference.git'
PROJECTURL = 'https://github.com/fabrizioiacobucci/tree/development/range-bar-preference'
PUBLISHGIT = 1
PUBLISHMAVEN = 0
}
nexusPublishing {
repositories {
sonatype {
stagingProfileId = '1c4ab2dc896731'
//packageGroup = "com.fabrizioiacobucci.android"
username = ossrhUsername
password = ossrhPassword
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
}
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
build.gradle (module)
apply plugin: 'com.android.library'
android {
compileSdkVersion 30
buildToolsVersion '31.0.0 rc3'
defaultConfig {
minSdkVersion 14
targetSdkVersion 30
version VERSION
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
versionCode 1
versionName VERSION
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
implementation 'com.appyvet:materialrangebar:1.3'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.5.1'
}
apply from: "${rootDir}/scripts/publish-module-maven.gradle"
apply from: "${rootDir}/scripts/publish-module-githubpkg.gradle"
publish-module-maven.gradle
apply plugin: 'maven-publish'
apply plugin: 'signing'
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
task javadoc(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.srcDirs
classpath = configurations.compile
}
task javadocJar(type: Jar, dependsOn: javadoc) {
archiveClassifier.set('javadoc')
from javadoc.destinationDir
}
artifacts {
archives androidSourcesJar
archives javadocJar
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
// The coordinates of the library, being set from variables that
// we'll set up later
groupId GROUPID
artifactId ARTIFACTID
version VERSION
artifact("$buildDir/outputs/aar/range-bar-preference-release.aar")
artifact androidSourcesJar
artifact javadocJar
// Mostly self-explanatory metadata
pom {
name = 'Range Bar Preference'
description = 'A range bar that can be used as an android shared preference'
url = PROJECTURL
groupId GROUPID
licenses {
license {
name = 'The Apache Software License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution = 'repo'
}
}
developers {
developer {
id = 'FabrizioIacobucci'
name = 'Fabrizio Iacobucci'
email = 'fabrizio.iacobucci90#mail.com'
}
}
scm {
connection = 'scm:git:github.com/fabrizioiacobucci/range-bar-preference.git'
developerConnection = 'scm:git:ssh://github.com/fabrizioiacobucci/range-bar-preference.git'
url = 'https://github.com/fabrizioiacobucci/range-bar-preference/'
}
}
}
}
}
}
ext["signing.keyId"] = rootProject.ext["signing.keyId"]
ext["signing.password"] = rootProject.ext["signing.password"]
ext["signing.secretKeyRingFile"] = rootProject.ext["signing.secretKeyRingFile"]
signing {
sign publishing.publications
}
publish-module-githubpkg.gradle
artifacts {
archives androidSourcesJar
archives javadocJar
}
project.publishing {
publications {
maven(MavenPublication) {
groupId = GROUPID
artifactId = ARTIFACTID
version = VERSION
artifact("$buildDir/outputs/aar/range-bar-preference-release.aar")
artifact androidSourcesJar
artifact javadocJar
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
pom {
name = 'Range Bar Preference'
packaging = 'aar'
description = 'A range bar that can be used as an android shared preference'
url = PROJECTURL
licenses {
license {
name = 'The Apache Software License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution = 'repo'
}
}
developers {
developer {
id = 'FabrizioIacobucci'
name = 'Fabrizio Iacobucci'
email = 'fabrizio.iacobucci90#mail.com'
}
}
scm {
connection = 'scm:git:github.com/fabrizioiacobucci/range-bar-preference/range-bar-preference.git'
developerConnection = 'scm:git:ssh://github.com/fabrizioiacobucci/range-bar-preference/range-bar-preference.git'
url = 'https://github.com/fabrizioiacobucci/range-bar-preference/'
}
}
}
}
repositories {
maven {
name = "GitHubPackages"
url = uri('https://maven.pkg.github.com/fabrizioiacobucci/range-bar-preference')
credentials {
username = System.getenv("GITHUB_USER")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
I don't know if there is anything else relevant to share, please let me know in case.
Do you have any idea what am I missing?
Thanks a lot in advance for any help.
As promised I forked the original project upgraded to android and then all the dependencies to the latest.
For the fast release of SDK to do the POC, I released it to jitpack.io
How to add the new library as a dependency.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add this to your app module dependencies:
dependencies {
implementation 'com.github.dk19121991:range-bar-preference:0.0.7'
}
I did try it myself and it's working fine for me, I updated the app module in my forked version to use the library from jitpack only, and it's working smoothly.
Please try the library and let me know if you feel anything else you need.
https://github.com/dk19121991/range-bar-preference
import com.nfx.android.rangebarpreference
change fabrizioiacobucci to nfx will solve your poblem

How to add a library to Gradle build in Android Studio project?

I want to add the following library to the Gradle build of my project. The library that I want to add is : signal-protocol-java-2.8.1.jar that I have downloaded. How can I add it in the Gradle build, so that I import the classes included in the .jar it in the java classes that I am developing ?
Here is my build.gradle file :
buildscript {
repositories {
google()
mavenCentral()
jcenter {
content {
includeVersion 'org.jetbrains.trove4j', 'trove4j', '20160824'
includeGroupByRegex "com\\.archinamon.*"
}
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha09"
}
}
ext {
BUILD_TOOL_VERSION = '30.0.2'
COMPILE_SDK = 30
TARGET_SDK = 30
MINIMUM_SDK = 19
JAVA_VERSION = JavaVersion.VERSION_1_8
}
wrapper {
distributionType = Wrapper.DistributionType.ALL
}
allprojects {
repositories {
google()
jcenter()
}
}
subprojects {
ext.lib_signal_service_version_number = "2.15.3"
ext.lib_signal_service_group_info = "org.whispersystems"
ext.lib_signal_metadata_version = "0.1.2"
if (JavaVersion.current().isJava8Compatible()) {
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
}
}
task qa {
group 'Verification'
description 'Quality Assurance. Run before pushing.'
dependsOn ':Signal-Android:testPlayProdReleaseUnitTest',
':Signal-Android:lintPlayProdRelease',
':libsignal-service:test',
':Signal-Android:assemblePlayProdDebug'
}
You have to edit your module-level build.gradle (the one you've posted is project-level). It's typically in the "app" folder of your project. Find there dependencies block and add this line:
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
...
}
Then create a folder "libs" near this gradle file and put your .jar library there. Sync the project with Gradle.

Android maven publish plugin is not publishing project dependencies

I have an android project which includes 3 modules ModuleA, ModuleB, ModuleC. ModuleA depends on both ModuleB and ModuleC. I'm trying to publish the ModuleA to a maven repository using android-maven-publish plugin and looks like it is not automatically publishing ModuleB and ModuleC to maven when I run publishToMavenLocal. When I try to use published ModuleA as a dependency in a different project gradle could not resolve ModuleB, ModuleC.
Is there something that I am missing?.
I am using gradle plugin version 3.5.0, gradle version 5.4.1 and mavenPublishVersion 3.6.2.
This is how project's gradle looks like
build.gradle(project)
buildScript {
...
dependencies {
classpath "digital.wup:android-maven-publish:3.6.2"
}
}
allprojects {
apply plugin: "digital.wup.android-maven-publish"
}
and ModuleA's build.gradle
build.gradle(ModuleA)
...
dependencies {
api project(":moduleB")
api project(":moduleC")
}
publishing {
repositories {
...
mavenLocal()
}
publications {
mavenSrcModuleA(MavenPublication) {
from components.android
groupId = "com.example.sdk"
artifactId = "moduleA"
version = "1.0.0"
}
}
}
and ModuleB's build.gradle
build.gradle(ModuleB)
...
publishing {
repositories {
...
mavenLocal()
}
publications {
mavenSrcModuleB(MavenPublication) {
from components.android
groupId = "com.example.sdk"
artifactId = "moduleB"
version = "1.0.0"
}
}
}
and ModuleC's build.gradle
build.gradle(ModuleC)
...
publishing {
repositories {
...
mavenLocal()
}
publications {
mavenSrcModuleC(MavenPublication) {
from components.android
groupId = "com.example.sdk"
artifactId = "moduleC"
version = "1.0.0"
}
}
}

Dependencies not added to POM file - Android Gradle Maven Publishing

I'm using the maven-publish plugin to publish an aar file to a maven repository. However I noticed that compile dependencies are not added to the pom.xml even after I add the transitive property. I'm using com.android.tools.build:gradle:1.1.3
Any hints on how to resolve this ?
build.gradle
publishing {
publications {
sdkAar(MavenPublication) {
artifacts {
groupId 'com.test'
artifactId 'my_sdk'
version currentVersion
artifact 'build/outputs/aar/release.aar'
artifact androidJavadocsJar {
classifier "javadoc"
}
}
}
sdkJar(MavenPublication) {
groupId 'com.test'
artifactId 'my_sdk_jar'
version currentVersion
artifact 'build/libs/release.jar'
artifact androidJavadocsJar {
classifier "javadoc"
}
}
}
repositories {
maven {
credentials {
username archiva_username
password archiva_password
}
}
}
}
Thanks in Advance
If you want the dependencies to be added automatically to the POM, you need to use the components feature. Here's an example from the user guide:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
That from ... is important. What I don't know is whether the Android plugin sets up its own software components. I can't see any references to such things.
Remember that the new publishing mechanism is currently incubating, and perhaps that's why the Android plugin doesn't offer any direct support for it at the moment.
If you really want to use the publishing plugin, you can grab the runtime dependencies of your artifacts and manually add them to the POM using the syntax described in the user guide. I wouldn't recommend that approach though as it's messy and looks error prone.
dependency not added automatically you need to add publishing tag.
publishing {
publications {
aar(MavenPublication) {
groupId libraryGroupId
version = libraryVersion
artifactId libraryArtifactId
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
configurations.getByName("_releaseCompile").getResolvedConfiguration().getFirstLevelModuleDependencies().each {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.moduleGroup)
dependency.appendNode('artifactId', it.moduleName)
dependency.appendNode('version', it.moduleVersion)
}
}
}
}
please try this code, I tried it successfully.
//generate pom nodes for dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each { dependency ->
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
}
}

Categories

Resources