Android Gradle not finding remote AAR - android

I was working with Gradle v5.4 and Android Gradle Plugin v3.5.2, and had no problems!
I have a remote Artifactory repository where I upload .aar files that are needed in my project.
When I updated to Gradle v6.5 and Android Gradle Plugin v4.0.1 those .aar files are no longer downloaded.
This is how I add those dependencies:
implementation "ar.com.sebasira:some-library:1.0.0#aar"
This was working just fine before the upgrade.
In the logs I can see that AndroidStudio looks for it in this location:
https://myrepo.com.ar/ar/com/sebasira/some-library/1.0.0/some-library-1.0.0.pom
If I put that URL in the browser, indeed there's nothing to be found, but if I change the last part (the extension) to .aar I can download the artifact.
I thought that adding #aar was to instruct Gradle to look for .aar file but it seems not to be working.
I've also try adding the dependency like in this other way with no luck.
implementation group: 'ar.com.sebasira', name: 'some-library', version: '1.0.0', ext: 'aar'

I've found out that since Gradle v6.0 onwards, Gradle assumes that if no .pom file is present in the remote repository is because the artifact does not exists, so it does not look for it by default. According to release notes:
If Gradle fails to locate the metadata file (.pom or ivy.xml) of a module in a repository defined in the repositories { } section, it now assumes that the module does not exist in that repository.
...
You can opt into the old behavior for selected repositories by adding the artifact() metadata source.
So the solution is to add that to repository in question, like this:
repositories {
maven {
url 'the-repo-url'
metadataSources {
artifact()
}
}
}

Related

How to refer the published aar library files in the azure artifact to android app

Requirement:
I have two library projects example, projectCoreModules, and projectCoreValidation.
Project A referring these two aar library projects and it's working fine.
current requirement, new project B also wants to refer this two aar library.
So, I created a separate repo for coreModules and CoreValidation projects in Azure DevOps.
Whenever PR is raised, the pipeline build will be triggered and it will publish the aar files.
I know, we can refer to the azure artifact feeds, in our android apps using the below code in Gradle.
api (group: 'Tst', name: 'TestingPackage', version: '3.1.0', ext: 'jar')
Same like this way, Is it possible to refer to the published aar files to projectA and projectB. So that I can reduce the code.
Even though I have tried with the universal package, through the build pipeline I can able to publish as a universal package. But, I was unable to refer those published to ProjectA and ProjectB. so that Gradle build of these two projects gets to succeed.
We are using similar in our project. However instead of using ext: "jar", we don't use any ext and it is working. Try this:
api (group: 'Tst', name: 'TestingPackage', version: '3.1.0')
I suppose you have already setup your repository source at the project level build.gradle file. We have setup like so
allprojects {
repositories {
google()
mavenCentral()
maven {
url project.ARTIFACT_URL
name project.FEED_NAME
credentials {
username project.PROJECT_NAME_ON_AZURE
password System.getenv("SYSTEM_ACCESSTOKEN") != null ? System.getenv("SYSTEM_ACCESSTOKEN") : project.MAVEN_ACCESS_TOKEN
}
}
}
}

importing an existing JAR or AAR as new project module

how to import JAR or AAR package as new project module in A new Android Studio Arctic Fox | 2020.3.1 Canary 9 ?
please let me know.
This works on Android Studio Arctic Fox Beta 02
Step 1 : Navigate to, File -> Project Structure. You can also press Ctrl+Alt+Shift+S
You will see a window just like below.
Step 2 : Click On app module as shown in image
Step 3 : Click on + icon as marked in image
Step 4 : You will see option to select jar/aar dependency. Click on it
You will see another window just like above asking you to specify path. Specify the path in which you kept the aar/jar file and hit Ok.
That should work
You can directly implement using JAR/ARR file path.
implementation files('/File Path/file.aar')
For Android Studio Bumblebee, original answer given here
I have followed steps suggested by the Android developer site:
Copy .aar file into the libs folder of the app
File -> Project Structure... -> Dependencies
Click on "+" icon and select JR/AAR Dependency and select app module
Add .aar file path in step 1.
Check your app’s build.gradle file to confirm a declaration.
Step 1: Put your aar file in the libs folder. And let’s take the file name is supernover.aar as an example.
Step 2: Put the following code in your Project level
build.gradle file,
allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}
and in the app level module write the below code,
dependencies {
Implementation(name:'supernover', ext:'aar')
}
Step 3: Then Click sync project with Gradle files.
If everything is working fine, then you will see library entry is made in build ->intermediates -> exploded-aar.
In my opinion, the best way to do this is to deploy the jar/aar to a local maven repository. if you install maven, you can use the mavenLocal() repository in gradle and read from there as with any other repo, regardless of the IDE you are using. All versions of Android Studio will work, all version of IntelliJ will work, VSCode will work, the command line will work, etc. Another advantage is, you'll be able to swap versions of the library as you do with all the others, just change the version in gradle (after deploying the new one), and will work for all your projects. Putting jars/aars manually into a project is just a bad practice, and reaaally outdated to top.
Once you installed maven, type this in your terminal:
mvn install:install-file -Dfile=d:\mylibrary-{version}.aar -DgroupId=com.example -DartifactId=mylibrary -Dversion={version} -Dpackaging=aar
Where you swap aar and jar depending on the type. The package name, group ID and library name are up to you, anything will work. I would use the library's package and name, and version 1.0 if you don`t have a version.
Here's an example link. Is old, but the process is the same. mvn install, then consume from mavenLocal().
For anyone in search of a solution still.
Create a new android Application project.
Convert new project into a standalone Library module.
Add maven-publish plugin to the module-level build.gradle
Connect your project to your Github repository (or create a new one).
In the module-level build.gradle, implement the Github Packages authentication flow. I'm using 'zuko' as an example - replace every instance of that name with your Github login.
android {
...
publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/zuko/[git-repository]")
credentials {
username = 'zuko'
password = 'token' // this is a Git Personal Access Token
}
}
}
publications {
release(MavenPublication) {
groupId 'com.zuko.libraries'
artifactId 'choose-a-name'
version '1.0.0'
artifact("$buildDir/ogury-mediation-mopub-5.2.0.aar")
// you can actually put the artifact anywhere you want.
// This is the location of where you place your .aar file
}
}
}
...
}
If everything is connected properly, save your work, and run the the task: ./gradlew publish. The error logs are straightforward so just defer to the instructions and Google for more assistance.
To install a successfully published package into your desired project, use the same auth procedure for publishing.repositories, you don't need the second half, publishing.publications.
example: implementation 'com.zuko.libraries:choose-a-name:1.0.0'
You could configure a repository in you buildscript that looks for dependencies in a local directory
Use this to register a local directory as repository in your app module's build.gradle where libs is a directory under app module (<project>/app/libs/)
buildscript {
repositories {
flatDir { dirs 'libs' }
}
}
then declare your dependencies from the local file tree you registered earlier
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
}
This will include all jar/aar artifacts present under libs directory to be included in your module's dependencies.
PS: Local jar/aar artifacts will expect any transitive dependencies to be on the classpath unless they are fat-jars (package all transitive dependencies within the artifact), so these need to be added explicitly as dependencies.

How to version local gradle dependencies?

My Android project contains a library that is shipped as AAR file.
There are multiple options to include local AAR files.
I can declare a file dependency:
implementation files('libs/mylib.aar')
Or I can put the AAR into another module and then use a project dependency:
implementation project(':mylibmodule')
However, I want to specify the exact version of my library:
mylib:1.0.0
Unfortunately, I do not know how to specify the version without using some remote repository.
Note that I do not want to upload the library to JitPack, MavenCentral or similar.
All I want is to specify the version of a local AAR file.
Update
The AAR file is a Zip-File with the following content:
/proguard.txt
/R.txt
/AndroidManifest.xml
/public.txt
/classes.jar
/res/values/values.xml
Note that the AndroidManifest.xml contains the version of the library.
However, I assume that gradle always expects a pom file for the versioning information.
I realized that gradle allows to specify a local Maven repository at a specific path:
repositories {
maven {
url uri("${projectDir}/mylibdir")
}
}
To use a local Maven repository, I need to build my library as a Maven artifact. To create a Maven artifact, it suffices to create a POM file in the right subfolder.
The AAR file remains unchanged since Maven does not care about the artifact format.
Creating a Maven artifact can be automated with the maven-publish plugin, e.g.:
apply plugin: 'maven-publish'
publishing {
publications {
myRelease(MavenPublication) {
groupId 'com.foo'
artifactId 'my-artifact'
version '1.0.0'
artifact("$buildDir/outputs/aar/my-artifact.aar")
}
}
repositories {
maven {
url "$buildDir/repo"
}
}
}
However, since I do not really need to use Maven, the simpler choice is to add the version to the AAR file name.
I did this with the following snippet in the build.gradle of my library:
android.libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = "${archivesBaseName}-${variant.name}-${defaultConfig.versionName}.aar"
}
}

How to publish in my maven local repository an existing aar with gradle?

I have a project that have the following structure:
projectRoot
build.gradle
module1/
build.gradle
artifact1.aar
module2/
....
My artifact1.aar is a compiled artifact and i have no access to the sources.
my module 1 gradle build file is the following:
configurations.maybeCreate("default")
artifacts.add("default", file('artifact1.aar'))
With this the code contains in the .aar is available in module 2 by simply reference a gradle project dependency.
But i want to publish the .aar in my maven local, in order that it can be accessible for other android project.
I check the maven-publish plugin and the android-maven-publish plugin but the two seems to be called after java-library plugin or com.android.library plugin.
So my question is how to publish in my maven local repository an existing aar with gradle ?
I'm using gradle in version 4.8.
I used an rxbinding aar for this example.
As you correctly mentioned, there has to be a subproject in the "publisher" project, which must contain the aar, and a build file with the following content:
// rxbinding/build.gradle
apply plugin: "maven-publish"
configurations.maybeCreate("default")
def publishArtifact = artifacts.add("default", file('rxbinding-2.1.1.aar'))
publishing {
publications {
aar(MavenPublication) {
groupId = 'my.sample.artifact'
artifactId = 'somerandomname'
version = '1.0.0'
artifact publishArtifact
}
}
}
You can apply the maven-publish plugin to any project, but then you have to define the artifacts manually, like we've just done here. Some plugins (like the java-library plugin) are integrated with the maven-publish plugin to automatically publish the default artifacts of the project.
Now run ./gradlew rxbinding:publishToMavenLocal - This should place the artifact into your local repo
Add implementation 'my.sample.artifact:somerandomname:1.0.0' to the consumer app, in any project on your machine.
It is very important to mention any aar published this way, will not bring it's dependencies, so you have to know what libs are needed to actually use the published aar.
I have also created an example project, where you can try this.
If you have maven installed, you could use the install-file goal of the maven-install-plugin.

Gradle only downloads pom from mavenCentral - Android

I have successfully deployed an AAR artifact to mavenCentral using Sonatype's OSS Nexus but when i reference it on another project gradle is only downloading the pom and it's respective ASC file to ~/.gradle/cache.
This is my build.gradle http://pastie.org/private/zaxa13hsd52e4elfpnne4w
This is the build.gradle for the library https://github.com/Machinarius/PreferenceFragment-Compat/blob/master/build.gradle
Can anyone point me in the right direction?
The aar.asc packaging is coming from this section in your gradle configuration:
configurations {
archives {
extendsFrom configurations.default
}
}
Remove those lines from your build.gradle file and deploy again. You should see the packaging be aar now.
The value for the POM's packaging element (aar.asc) looks wrong. You should nevertheless be able to fetch the artifact with something like dependencies { compile 'com.github.machinarius:preferencefragment:0.1#aar' }. With this "artifact only" notation, you won't get transitive dependencies, but you can always declare those yourself.

Categories

Resources