Add an AAR dependency in Android Studio 4.2 - android-studio

I'm trying to use https://github.com/brim-borium/spotify_sdk in Android Studio with Flutter
As such, I need to import Spotify SDK (two AAR files), and to be able to add them to the build process.
Because of spotify_sdk (flutter package), I can't use (which seem to work)
implementation (name: 'spotify-app-remote', ext: 'aar')
implementation (name: 'spotify-auth', ext: 'aar')
in my project, I specifically need Gradle to consider spotify-app-remote & spotify-auth as projects, because spotify_sdk imports them like this.
implementation project(':spotify-auth')
implementation project(':spotify-app-remote')
Here is the Gradle error:
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
> Could not resolve project :spotify-auth.
Required by:
project :app
> No matching configuration of project :spotify-auth was found. The consumer was configured to find an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
- None of the consumable configurations have attributes.
> Could not resolve project :spotify-app-remote.
Required by:
project :app
> No matching configuration of project :spotify-app-remote was found. The consumer was configured to find an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
- None of the consumable configurations have attributes.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
Exception: Gradle task assembleDebug failed with exit code 1
Here is my settings.gradle
def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
def properties = new Properties()
assert localPropertiesFile.exists()
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
So I need to be able to force Gradle to import these AAR as a projet.
Here is my project structure, if it useful
Project Structure
From documentation and StackOverflow posts, I've read that Android Studio used to have an Import AAR/Jar file functionality. It is now missing in Android Studio 4.2
Is there any functionality that replaces Import AAR/Jar file? Or any way to force Gradle to recognize these files as projects?
Thank you very much

Related

Why is Gradle failing on :linkDebugTestLinux in my Kotlin multiplatform project?

I'm porting a C# library to Kotlin to take advantage of multiplatform. When running the build task, it fails in the subtask linkDebugTestLinux.
For context, I'm using IDEA Ultimate on Manjaro. I'm certain there's nothing wrong with my code as compileKotlinLinux finishes without error.
There are zero DDG results for "linkDebugTestLinux" and nothing helpful for "konan could not find home" or "kotlin native ...". After hours of stitching together incomplete and outdated examples from the official docs, I've given up.
My build.gradle.kts:
plugins {
kotlin("multiplatform") version "1.3.40"
}
repositories {
mavenCentral()
}
dependencies {
commonMainImplementation("org.jetbrains.kotlin:kotlin-stdlib")
commonTestImplementation("org.jetbrains.kotlin:kotlin-test-common")
commonTestImplementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
}
kotlin {
// js() // wasn't the issue
linuxX64("linux")
}
Output of task build without args:
> Configure project :
Kotlin Multiplatform Projects are an experimental feature.
> Task :compileKotlinLinux
[...unused param warnings...]
> Task :compileKotlinMetadata
[...unused param warnings...]
> Task :metadataMainClasses
> Task :metadataJar
> Task :assemble
> Task :linuxProcessResources NO-SOURCE
> Task :linuxMainKlibrary
> Task :linkDebugTestLinux FAILED
e: Could not find "/home/username/" in [/home/username/path/to/the/repo, /home/username/.konan/klib, /home/username/.konan/kotlin-native-linux-1.3/klib/common, /home/username/.konan/kotlin-native-linux-1.3/klib/platform/linux_x64].
[...snip...]
BUILD FAILED in 16s
4 actionable tasks: 4 executed
Process 'command '/usr/lib/jvm/java-8-openjdk/bin/java'' finished with non-zero exit value 1
In the boilerplate I omitted it suggests to use --debug, so I've uploaded that here.
After some investigation, it was assumed that the problem is in the path. In the debug log, you got the /home/yoshi/,/ fragment. As far as this directory name was unexpected, the compiler interpreted this , as a delimiter between lib names. So, it tried to find library /home/yoshi/, that was obviously unavailable.
For now, I would recommend you to change the directory name to be something trivial.

How to clear specific gradle cache files when running the "Clean project" command?

I have added the following task in my project's build.gradle file:
task('clearLibCache', type: Delete, group: 'MyGroup',
description: "Deletes any cached artifacts with the domain of com.test in the Gradle or Maven2 cache directories.") << {
def props = project.properties
def userHome = System.getProperty('user.home')
def domain = props['domain'] ?: 'com.test'
def slashyDomain = domain.replaceAll(/\./, '/')
file("${userHome}/.gradle/caches").eachFile { cacheFile ->
if (cacheFile.name =~ "^$domain|^resolved-$domain") delete cacheFile.path
}
delete "${userHome}/.m2/repository/$slashyDomain"
}
I'd like this task to be executed when I hit the "Clean project" menu, and only in this case.
How to do that ?
That "Clean project" menu item under the hood appears to do a couple of things (based on the output of the Gradle Console window when you click it):
A gradle clean, the equivalent of calling ./gradlew clean
Generate sources and dependencies for a debug build, including a mockable Android sources jar if needed.
I would make your task a dependency for the Gradle clean task, so that whenever the project is cleaned, this task is also invoked. This can be achieved by adding the line clean.dependsOn clearLibCache in your build.gradle after you declare the task.

Trouble Publishing Android Studio Library on jCenter with Bintray

I'm following this tutorial to publish an example Android Studio library on Jcenter:
http://crushingcode.co/publish-your-android-library-via-jcenter/
It seems very clear.
I've created my GitHub repository with this library at this link:
https://github.com/alessandroargentieri/mylibview
I've also Signed in to Bintray.com, and created a new repository which must contain my library (as explained in the tutorial above).
To publish a repository on Bintray I must create an organisation, then you create the repository. So these are my data:
Bintray username: alessandroargentieri
organisation: alexmawashi
repository: https://bintray.com/alexmawashi/my_android_repository
then, in Android Studio, in the gradle file of my library module, I've this data:
apply plugin: 'com.android.library'
ext {
bintrayRepo = 'my_android_repository' //maven
bintrayName = 'mylibview' // Has to be same as your library module name
publishedGroupId = 'mawashi.alex.mylittlelibrary'
libraryName = 'MyLibView'
artifact = 'mylibview' // Has to be same as your library module name
libraryDescription = 'Android Library to use a custom view'
// Your github repo link
siteUrl = 'https://github.com/alessandroargentieri/mylibview'
gitUrl = 'https://github.com/alessandroargentieri/mylibview.git'
githubRepository= 'alessandroargentieri/mylibview'
libraryVersion = '1.0'
developerId = 'alexmawashi'
developerName = 'Alessandro Argentieri'
developerEmail = 'alexmawashi87#gmail.com'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
...
...
apply from: 'https://raw.githubusercontent.com/nisrulz/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nisrulz/JCenter/master/bintrayv1.gradle'
When I use the terminal and write:
gradlew clean build install bintrayUpload --stacktrace
After a few minutes, I get this error:
What went wrong:
Execution failed for task ':mylittlelibrary:bintrayUpload'.
> Could not create package 'alessandroargentieri/my_android_repository/mylibview': HTTP/1.1 404 Not Found [message:Repo 'my_android_repository' was not found]
What am I doing wrong?
Thanks.
There also might be a problem here:
https://raw.githubusercontent.com/nisrulz/JCenter/master/bintrayv1.gradle
If your repo belong to your organisation then you are going to need the userOrg parameter set.
See https://github.com/bintray/gradle-bintray-plugin#step-4-add-your-bintray-package-information-to-the-bintray-closure step 4
Also see: HTTP/1.1 401 Unauthorized when uploading binary on bintray
For this to work properly, your gradle.properties file needs to have a bintray.user and a bintray.apikey (which is your bintray API key) configured (see the include in https://raw.githubusercontent.com/nisrulz/JCenter/master/bintrayv1.gradle )

Gradle build error with branch io and crashlytics

Error:A problem occurred configuring root project 'app_name'.
> Could not resolve all dependencies for configuration ':_debugCompile'.
> Could not find com.crashlytics.sdk.android:answers-shim:0.0.3.
Searched in the following locations:
https://repo1.maven.org/maven2/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.pom
https://repo1.maven.org/maven2/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.aar
file:/Users/arun/repos/app_name/libs/answers-shim-0.0.3.aar
file:/Users/arun/repos/app_name/libs/answers-shim.aar
https://maven.fabric.io/public/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.pom
https://maven.fabric.io/public/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.aar
file:/Users/arun/Library/Android/sdk/extras/android/m2repository/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.pom
file:/Users/arun/Library/Android/sdk/extras/android/m2repository/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.aar
file:/Users/arun/Library/Android/sdk/extras/google/m2repository/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.pom
file:/Users/arun/Library/Android/sdk/extras/google/m2repository/com/crashlytics/sdk/android/answers-shim/0.0.3/answers-shim-0.0.3.aar
Required by:
:app_name:unspecified > io.branch.sdk.android:library:1.14.2
branch-io originally was io.branch.sdk.android:library:1+ I then tried
io.branch.sdk.android:library:1.14.1 and io.branch.sdk.android:library:1.14.2 with the same error.
Any ideas what is happening?
com.crashlytics.sdk.android:answers-shim:0.0.3 isn't available on mavenCentral. Add jcenter to your repository section.
ref: https://bintray.com/fabric/fabric/com.crashlytics.sdk.android%3Aanswers-shim/view
Example:
repositories {
jcenter()
}
If you don't plan to use the Fabric Answers integration, and don't want to import the answers-shim, just import your project as follows:
replace io.branch.sdk.android:library:1.14.1 with
implementation ('io.branch.sdk.android:library:3.+') {
exclude module: 'answers-shim'
}
Source - https://github.com/BranchMetrics/android-branch-deep-linking

Running Gradle ShadowJar Plugin only on specific subproject

Please note: Even though my myapp-server is a DropWizard app, this is strictly a Gradle question; I only mention DropWizard for good measure.
I have a multi-project build that I am building with Gradle:
myapp/
myapp-client/
myapp-shared/
myapp-server/
build.gradle
settings.gradle
Where settings.gradle looks like:
include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'
I have successfully got Gradle to compile my Groovy source code, run unit tests, generate GroovyDocs, and package both binary and source JARs for all 3 subprojects. The build invocation for which is: gradle clean build groovydoc sourcesJar -Pversion=<whatever version I specify>.
The myapp-server is actually a project that I need to run the Shadow JAR plugin on, which is usually executed via gradle clean build shadowJar.
Here is my myapp-server/build.gradle:
import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
apply plugin: 'application'
apply plugin: 'shadow'
mainClassName = 'com.me.myapp.server.MyAppServer'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:0.8'
}
}
dependencies {
compile project(':myapp-shared')
compile (
'io.dropwizard:dropwizard-core:0.7.1'
)
}
shadow {
outputFile new File(destinationDir, "${outputJarBaseName}.${extension}")
transformer(ServiceFileTransformer)
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
run {
args 'server', 'config.json'
if (System.getProperty('debug', 'false') == 'true') {
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}
}
artifacts {
shadowJar
}
My question: How do I configure Gradle to only run the shadowJar task on my myapp-server subproject, and not on any of the others? Ideally, I could just have my build invocation be something like:
gradle clean build groovyDoc sourceJar shadowJar -Pversion=<blah>
And then somehow configure Gradle to ignore running shadowJar on everything except myapp-server. Ideas?
Update
When I run gradle clean build groovyDoc sourceJar shadowJar -Pversion=0.1.3 I see a bunch of Gradle output that ends with:
:myapp-shared:groovydoc
:myapp-client:sourcesJar
:myapp-server:sourcesJar
:myapp-shared:sourcesJar
:myapp-server:copySignedLibs UP-TO-DATE
:myapp-server:shadowJar
BUILD SUCCESSFUL
Total time: 16.315 secs
However when I drill down into myapp-server/build/libs/myapp-server-0.1.3.jar and open it (extracting it like a ZIP), I only see my compiled classes inside of it, not all the classes from all of its dependent JARs (it should be a "Fat JAR"). For instance, myapp-server depends on DropWizard. All the DropWizard classes should be inside this JAR. All of DropWizard's transitive dependencies should have class files inside this JAR, etc.
I've just checked out the project located here.
After fetching the sources I've run: gradle clean shadowJar in myapp directory. As far as I see, there is a jar file created under myapp/myapp-server/build/distributions that seesm to be the fat jar You're looking for (also when file size is taken into consideration). After entering the mentioned distributions directory and running java -jar myapp-server.jar the server is started. It seems that it's all working fine. Am I right?
Also running curl -v -X GET http://localhost:8080/myapp/dosomething gives the following output:
* Hostname was NOT found in DNS cache
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /myapp/dosomething HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Mon, 05 Jan 2015 14:59:50 GMT
< Content-Type: application/json
< Content-Length: 0
<
* Connection #0 to host localhost left intact
If you were looking to only reference one sub project to shadowJar you could do it as follows.
gradle clean build groovyDoc sourceJar :myapp-server:shadowJar -Pversion=<1.0>
You can drill down to your subprojects tasks via :<project name>:<task name>
Reference:
https://gradle.org/docs/current/userguide/multi_project_builds.html
57.4. Running tasks by their absolute path

Resources