Nested libraries aar file behavior - java

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.

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.

Import aar file to ionic app, using VisualStudio

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.

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" />

Add OkHttp to my project

I am trying to add OkHttp to my project, i download both OkHttp and Okio library and add it to the libs directory.
than i add the compile methods:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I try to Build the App and i get this error:
Error:(27, 1) A problem occurred evaluating root project 'APP'.
> Could not find method compile() for arguments [com.squareup.okhttp3:okhttp:3.4.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Any idea what can be the problem? i am using Android Studio om mac OS
Delete this line of your root gradle :
compile 'com.squareup.okhttp3:okhttp:3.4.1'
And Add this to your dependencies's app's module build.gradle
There are basically two build.gradle files in our project.
Top level gradle (can be found in the project directory)
Module level gradle (present inside the app folder of the project)
Here the problem is you are compiling the okHttp
compile 'com.squareup.okhttp3:okhttp:3.4.1'in the Top level gradle file try to place it in your Module level gradle. Also if you are importing a library using gradle you don't have to add it in the lib folder explicitly
Google gradle documentation

Error: Could not find com.google.gms:google-services:1.0. when adding google service plugin in build.gradle in android studio

I am integrating OAuth login for Google+ on my android application, following the tutorial.
According to the tutorial, I should add the Google Service plugin by adding
classpath 'com.google.gms:google-services:1.0' dependency to top-level build.gradle in my android project.
However, when I sync the gradle with the changes, I see the error as follows:
Error:Could not find com.google.gms:google-services:1.0.
Searched in the following locations:
file:/C:/Program Files/Android/Android Studio/gradle/m2repository/com/google/gms/google-services/1.0/google-services-1.0.pom
file:/C:/Program Files/Android/Android Studio/gradle/m2repository/com/google/gms/google-services/1.0/google-services-1.0.jar
https://jcenter.bintray.com/com/google/gms/google-services/1.0/google-services-1.0.pom
https://jcenter.bintray.com/com/google/gms/google-services/1.0/google-services-1.0.jar
In my build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.google.gms:google-services:1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
It seems that the android studio is not able to find google-services plugin from repositories.
Does anybody have the same issue? Or, am I missing something?
I ran into the same issue today. In the samples they use this line instead:
classpath 'com.google.gms:google-services:1.3.0-beta1'
This works.
For me I had to switch the repository to jcenter(). I was using mavenCentral and getting this error. As soon as I switched to jcenter() it pulled the dependency down.
To do this, open your build.gradle and replace each instance of mavenCentral() with jcenter()
Google updated their guide. Where it was classpath 'com.google.gms:google-services:1.0' now reads classpath 'com.google.gms:google-services:1.3.0-beta1' like joluet suggested.
Google now released the plugin v1.3.0
I was able to solve it using this:
classpath 'com.google.gms:google-services:1.3.0'
I resolved this issue by doing the following things:
In build.gradle (at project level):
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.google.gms:google-services:1.5.0-beta2'
}
}
allprojects {
repositories {
mavenCentral()
}
}
In build.gradle (at module level):
On top added:
apply plugin: 'com.google.gms.google-services'
and in dependecies added:
compile 'com.google.android.gms:play-services-analytics:8.3.0'
Apart from this make sure you have updated the Google Play services and Google repository in the SDK.
As of 25th Nov,2015, google released a newer version.
Try adding:
classpath 'com.google.gms:google-services:2.0.0-alpha1'
And don't forget to add jcenter() repository under buildscript.
Thanks.
I think you forgotten gradle's project level,
From developers page:
Add the dependency to your project-level build.gradle
classpath 'com.google.gms:google-services:1.5.0-beta2'
Add the plugin to your app-level build.gradle:
apply plugin: 'com.google.gms.google-services'
Google has updated GCM go check it and for that you have to update your Android Studio too.
I would advise you against doing what you are doing. I did it too (by following Set up a GCM Client App on Android blindly) but ended up with exceeding 65K method limit.
So, you should remove the following lines from your gradle:
apply plugin: 'com.google.gms.google-services'
classpath 'com.google.gms:google-services:1.0'
Now import APIs of Google Plus with simple gradle import:
compile 'com.google.android.gms:play-services-plus:8.1.0'
Adding jcenter repository to project level build.gradle helped in my case:
buildscript {
repositories {
jcenter();
mavenCentral()
}
I was trying to setup firebase for my android app when I faced this problem. Following screenshot should clarify things -
Couple of things I would like to point out -
1. Once you
apply plugin: 'com.google.gms.google-services'
This line should be at the bottom of the app level gradle file. Not sure why but putting at the top did not workout for me.
If you use Android Studio, try to add google service with the IDE.
Right click on folder module.
On left list at Developer Services choose desired service.
Wait for syncing and the IDE will automatically add google dependecies.
add classpath on buildscript
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.gms:google-services:1.3.0-beta1'
}
}
You maybe miss this step
Copy the google-services.json file you just downloaded into the app/ or mobile/ directory of your Android Studio project. Open the Android Studio Terminal pane:
$ move path-to-download/google-services.json app/
Have fun.
You need to check the current version as in the link below says, by now:
classpath 'com.google.gms:google-services:1.3.0-beta4'
Check this answer:
Update gradle version
We have to look for the last version in my case was:
dependencies {
classpath 'com.google.gms:google-services:1.4.0-beta3'
}
Here you can find a list of the versions.
I got a similar problem while following the instructions for implementing a GCM client in Android Studio, however, the message I got was :
Error: Could not find com.google.gms:google-services:1.3.0-beta1...
Although I never figured out what was wrong, I simply copied my sources, resources, manifest, and keystore into a new Android Studio project. I then edited the auto-generated gradle build files for the new project by manually re-adding my dependencies.
I made sure not to copy over my build files from the old project verbatim (since the source of the error likely lay in them somewhere).
It is not a satisfying solution, but it worked for me and it took little time.
For me adding this under dependencies worked
compile 'com.google.gms:google-services:3.0.0'
Inside the .idea folder exists a workspace.xml file that defines the project's structure. I suggest you to close Android Studio, then delete .idea folder and then import the project again. The .idea would be generated by the IDE again. For me this solution works.
You should just add classpath 'com.google.gms:google-services:1.3.0-beta1' to dependencies of your (Top level build file) the build.gradle that has the (Project:<name of your project). Hope this helps!
I recently updated to Android Studio 2.3 and somehow apply plugin doesn't work.
After that, I tried various ways and then what work is when I commented out app's build.gradle:
apply plugin: 'com.google.gms.google-services'
and then I add this to top-level build.gradle (below classpath 'com.android.tools.build:gradle:2.2.3'):
classpath 'com.google.gms:google-services:3.0.0'
and then it works.

Categories