Import aar file to ionic app, using VisualStudio - java

I am fairly new to ionic development, so please be nice with me.
I am developing an ionic application, which has to use classes enclosed in an aar file.
I have a Cordova plugin and created a structure like in this page. So, I have now my aar file in the aar folder of the plugin. This is my myfile.graddle file:
repositories{
jcenter()
flatDir{
dirs ‘aar’
}
}
dependencies {
compile(name:'library', ext:'aar’)
}
android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
Now, I am trying to import some java classes that are inside the aar file. But, when I do some imports, in the import statement there is no path, I mean, it has the format of "import ClassToImport;", with no package path. I have not tried it yet, but I suspect is not ok, and it will not find the classes I need.
So, my question is, how to make classes inside the aar file accessible to the java class inside my plugin?
Thank you.

Related

Add gradle dependencies(build.gradle) in .aar file Android

I am having an android library project which is using third party libraries. I have created it's .aar file and imported the .aar file in a different project. Classes and resources are imported but the gradle libraries are not imported. I don't want to mention those dependencies again in the project.
Is there any way to import that project with .aar file or something else which include build.gradle or dependencies.
If you are including this library in dependencies block then try to use api instead of implementation.
AAR file does not include external dependencies which are mention in app/build.gradle so instead of adding it directly we upload to local maven and integrate it.
To upload it in your local maven you can add the following function in app/build.gradle
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://localhost" + System.getenv("HOME") + "/.m2/repository")
pom.version = '1.0-SNAPSHOT'
pom.groupId = 'your.package'
pom.artifactId = 'sdk-name'
}
}
}
You can change the pom version, groupId and artifactId according to how you want to use it.
If this process is completed you can integrate the library.
In the app/build.gradle of the project in which you want to add the
library specify the path.
implementation 'your.package:sdk-name:1.0-SNAPSHOT'
In the project/build.gradle add the following
allprojects{
...
mavenLocal()
}
This will search for the library in the local machine.
If still, you get some error try to invalidate caches and restart the android-studio from the File Menu.

how to relocate package inside jar for gradle dependencies

currently I am using a package call a.jar. Unfortunately that this jar includes a com.example.b package with some customized changes to code base inside com.example.b.
Now I want to have some latest cool features from com.example.b package in b.jar from github.
I think the best solution (not sure how it can be done) is to relocate latest com.example.b to com.example.standalone.b , so that the a.jar can still use its customized com.exampl.b source, while inside project I can use com.example.standalone.b package.
I did research for shadow plugin, but seems it rename package by package name globally, so that the package (com.example.b) in both jars (a.jar and b.jar) would be also renamed and have conflict.
May I know how to do this for specific jar, like below example?
implementation a.jar:1.0
implementation (b.jar:2.0) {
rename 'com.example.b' to 'com.example.standalone.b'
}
finally i managed to solve it by below configuration in goovy gradle.
configurations {
relocateB // just define a separate configuration
}
task relocateB (type: ShadowJar) {
def pkg = 'com.example.b' // lib to relocate
relocate pkg, "com.example.standalone.b" // we want to relocate the above package
configurations = [project.configurations.relocateB] // our configuration from above
dependencies {
// you must exclude below files in 'kotlin_module' and 'kotlin_builtins' extension,
// otherwise you won't be able to import `com.example.standalone.b` in kotlin files.
// This is a bug from Kotlin, consumed two days for me to solve.
// (https://youtrack.jetbrains.com/issue/KT-25709)
exclude '**/*.kotlin_metadata'
exclude '**/*.kotlin_module'
exclude '**/*.kotlin_builtins'
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn relocateB
}
dependencies {
...
relocateB ('com.example.b:2.2.3')
api tasks.relocateB.outputs.files
api 'com.example.a' // original package that won't be polluted
...
}

Nested libraries aar file behavior

We are facing problem while importing the aar file of a library (let's call it library_2) into another library (let's call it library_1). In addition, we need to import in the app project only library_1.aar file and make library_2 methods available at project level. What would it be the folders structure and the corresponding .gradle files?
Description of the problem in the image below:
You need to use api instead implementation for your library_1. First, add the following code to your library_1 project build.gradle:
allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}
then in your library_1 module build.gradle, add the following code to your dependencies block (assuming you have add library_2 aar to library_1 libs folder):
dependencies {
api(name:'library_2', ext:'aar')
}
Now, you can access the library_2 when using library_1 aar with the following dependencies block in your app module:
dependencies {
api(name:'library_1', ext:'aar')
}
For more details about flat aar, read How to manually include external aar package using new Gradle Android Build System.
For more details about the differences between compile, implementation, and api read Gradle Implementation vs API configuration
I have resolved above issue that you are facing. Please have a look below code and let see if it work for you.
In your app.gradle file add below dependecy:
implementation ('package.name.of.aar:modulethree-debug#aar') {
transitive=true
}
Note: modulethree-debug#aar is aar file which you want to access in other module.

Android Studio 3.0 - "Failed to transform file 'gson-2.2.4.jar' to match attributes {artifactType=android-classes}"

I'm a newcomer to Android Studio development, and I'm currently following an online tutorial to make a messenger app.
I've followed the steps of this tutorial, and it now needs me to download the Library files for Smack API & GSON, and add the .jar files to the libs folder in my project.
With this done, I right-clicked the files in the directory and clicked Add as library (the tutorial says to do Build Path -> Add to Build Path, but this doesn't seem to be an option in this version of Android Studio).
At this point, the project directory menu sidebar looks like this:
From here, I tried to rebuild the project but was met with the following error:
I've not been able to find a way around this error at all, and don't know enough about Android Studio to know if this is an issue with the IDE or the package itself. I've had a look around S/O but none of the posts seem to describe the same issue, or offer any solutions to similar issues that appear to work for this too.
If anyone has run into this before, or if it is a common Android Studio problem, would you be able to advise me on how to resolve it please? Very frustrating when I just want to get on with learning how to code for Android!
Any help massively appreciated, thanks in advance!
Mark
Here is best solution for importing any jar, arr libs
allprojects {
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
....
}
You do not need to add repeatable lines with libs file name.
if you are a new to android studio, you can check the project structure from android by tapping drop-down menu.
Since Gson is in maven central, you have to add mavenCentral() in buildscripts phase. you have to add the Gson library in the dependency using project structure, now to update the gradle, for the add the .jar file to your build.gradle file like:
dependencies {
compile files('libs/gson-2.2.4.jar')
}
or
dependencies {
compile 'com.google.code.gson:gson:2.2.+'
}
And make sure you are connected to the internet
did you try
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
}
in your build.graddle file of app module

Cordova Plugin - Add third party sdk

I am trying to create the plugin for the following sdk - https://ktplayhelp.zendesk.com/hc/en-us/articles/221071888-Android
In the setup project configuration point it is telling to setup the sdk by importing module in Android studio and add the dependency in our application's build.gradle file.
Can anyone please help and tell me how can I import the Android native module in Cordova without using Android studio?
As you can't modify cordovas .gradle file you have to add your own and reference it in your plugin.xml you can do that like this:
<framework src="src/android/*.gradle" custom="true" type="gradleReference" />
This will allow you to do things like compiling an external module. To make this actually work you will have to create an .aar library out of the project you want to integrate.
The resulting gradle-extension will look something like this:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'KTplay', ext:'aar')
}
android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
This assumes that you have put your .aar library in a subdirectory of your plugin named libs. Whats left to do is to ensure that the library actually gets copied during the build process, this is why we have to add it as a resource file in plugin.xml:
<resource-file src="libs/KTplay.aar" target="libs/KTplay.aar" />

Categories