How to use .arr sdk in my react-native library project - android

I wanted to create react-native wrapper around android sdk of zoom.us (they provide video conference feature through that), I have created a git repo using their sdk https://github.com/manjeets12/ZoomApiWrapper, but problem is that they only provide .aar sdk and it is giving me error project with path ":sdkName" could not be found in project ':react-native-zoom-api-wrapper'
I tried to use many solutions liking putting .aar files within libs folder and compiling from there or using flatDir options, as they don't provide any maven repo alternative, I am not sure how should I proceed in using this

So I've found a solution:
My aar-name is: 'my_name.aar'. You need to add this file to the libs folder in the root of your android folder (same level as your build.gradle).
Your build.gradle file has to contain this:
dependencies {
compile(name:'my_name', ext:'aar')
}
repositories {
flatDir {
dirs 'libs'
}
}
Your android project should compile then. Next step is to integrate the library to react-native.
npm install --save your_project
react-native link your_project
And finally add this to your android/app/build.gradle
repositories {
flatDir {
dirs "../../node_modules/your_project/android/libs"
}
}
For a working project see this git: https://github.com/transistorsoft/react-native-background-geolocation/blob/master/docs/INSTALL-ANDROID-RNPM.md

Related

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.

Import repository from github in gradle

I am building a react native android library which depends on:
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.5'
}
It turned out that android-maps-utils isn't working the way I want so I forked it and I wanted to use my version of this library instead of the official one. I can't find a way to do it though, I searched how to import a library from github inside gradle and I found ajoberstar/gradle-git but there is no clear instruction on how to do it and all examples I found doesn't work for me (for example this one: Gradle: how to clone a git repo in a task?). Is there any easier way on how to accomplish something like that?
EDIT:
This is my structure (simplified):
root:
| build.gradle
| settings.gradle
| aars
| map-util.aar
| libs
| android
| build.gradle
root build.gradle -> repositories { flatDir { dirs 'aars' } }
settings.gradle ->
include ":react-native-maps-lib"
project(":react-native-maps-lib").projectDir = file("./lib/android")
android/build.grandle -> compile(name: "map-util", ext:"aar")
Reference the AAR you build from building the forked source somewhere on your local filesystem.
repositories {
flatDir {
dirs 'libs'
}
}
then put the artifact in .../libs, and add it normally to your dependencies, of course matching the version to what you just built.
I figured out what is the problem! Let me first make clear though what I wanted to do - I'm building react native project. It uses my forked version of react-native-maps, which uses android-maps-utils. I wanted to fork android-maps-utils and use my forked version.
android-maps-utils produces aar file that I needed to import locally. Following Jeffrey Blattman suggestion I added libs directory inside react-native-maps and put aar file named android-maps-utils.aar there.
I also added this inside react-native-maps build.gradle:
allprojects {
repositories {
...
flatDir {
dirs "$rootDir/libs"
}
}
}
and this inside react-native-maps/android/build.gradle:
dependencies {
...
compile(name: "android-maps-utils", ext:"aar")
}
Now react-native-maps compiles but my react-native project doesn't due to:
A problem occurred evaluating project ':react-native-maps'.
So, I also needed to include this line:
compile(name: "android-maps-utils", ext:"aar")
inside my react native app android directory. Now everything compiles!

Gradle looking for aar in wrong project's lib folder

I have an Android application made up of multiple projects. One of those projects is an App project that just extends the Application object.
Inside the build.gradle for that app project, I add other projects as dependencies.
I've just created a new module to house an SDK (aar) I want to use. I've also added it to my app project's build.gradle.
compile project(':newmodule-thesdk')
Inside the libs folder of newmodule-thesdk, I have added the aar file. We'll call it thesdk.aar.
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'thesdk-1.0', ext:'aar')
}
When I attempt to sync gradle, the sync fails because thesdk-1.0 does not exist in the libs folder of my app project. Why is it looking for it there? Why is it not just finding it in the newmodule-thesdk project?
It appears solving the problem required me to do the following in my App project's build.gradle.
repositories {
flatDir {
dirs project(':newmodule-thesdk').file('libs')
}
}
I had the similar problem and I just changed my xxx.aar to xxx-N.N.aar, in your case thesdk.aar to thesdk-1.0.aar and then it worked fine
You just have to change your code this way
dependencies {
compile fileTree(include: ['thesdk-1.0.aar'], dir: 'libs')
}
Keep in mind that the file should exist!
u could do 2 approaches
1) As you already said it:
repositories {
flatDir {
dirs project(':newmodule-thesdk').file('libs')
}
}
it can be done in this way
2)you can make a new gradle project with including the sdk and reference the old code project in it.
and then reference old project when u dont want to include the sdk and when you want to include the sdk reference the new gradle project

Reuse modules in Android Studio

Whenever I add a android library-project as module to my Android Studio project, the sources get copied to the Android Studio project folder.
Is there a way to solve it like in eclipse, where there is only one copy of library project, any many projects can reference it?
You have different ways to achieve it:
using a local module referring the right path
adding an aar file
using a maven repo
CASE 1:
Using gradle and a local library, inside a project you can refer an external module.
Just use:
Project
|__build.gradle
|__settings.gradle
|__app (application module)
|__build.gradle
In settings.gradle:
include ':app'
include ':myLib'
project(':myLib').projectDir=new File('pathLibrary')
In app/build.gradle:
dependencies {
compile project(':myLib')
}
Pay attention to myLib.
You have to use the path of the library inside the other project, not the root of the project.
CASE 2:
Compile the library module, get the aar file, and then add to the main project:
Add the folder where you put the aar file as repository:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
Then add the dependency:
dependencies {
compile(name:'nameOfAarFile', ext:'aar')
}
CASE 3:
Another way is to to publish your module into a maven repository.
In this way, just use:
dependencies {
compile ('mypackage:mymodule:X.Y.Z')
}
I would compile your shared code as library project. So you will get an aar file which you can reference.
To create that Android Archive you need to build the project as a release build with this command:
gradlew aR
After that you have a file called <modulename>-release.aar this file is located in <projectroot>/<modulename>/build/outputs/aar. I rename those files to <modulename>.aar then you can put it into your lib directory of your module.
When done you can reference it from the module where you need it like this:
compile(name:'<modulename>', ext:'aar')
This also speeds up the build time since you don't need to compile the project anymore.

How to add jars / dependecies via gradle in IntelliJ IDEA

I don't have any experience in MAVEN, gradle or anyother build system. I'm working in Android Studio basically and I want to add few jars (and also their source). But the jars are hosted in maven repo. I can download the jars and directly add to the lib/ folder. But then, I don't know how to attach the sources. So what is the correct way to find add those jars via gradle in Android Studio? Thanks!
Configure maven repo:
repositories {
mavenLocal()
mavenCentral()
}
Then add appropriate dependencies in:
dependencies {
compile 'com.google.guava:guava:17.0-rc2'//e.g.
}

Categories

Resources