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.
Related
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.
In my build.gradle, I added the plugin:
apply plugin: 'maven'
Then using gradle install I can copy the resulted jar into the maven repository : ~/.m2/repository
However, my Gradle repository resides in ~/.gradle/caches/modules-2/files-2.1. How can I install the jar into this repository?
What worked for me is gradle install -Dmaven.repo.local=the/path/of/the/folder.
I don't know which IDE you are using but in eclipse you can add a new Run Configuration, in Gradle taks add install and in program arguments -Dmaven.repo.local=the/path/of/the/folder.
If you insist on manipulating the cache, then your best bet is to write a shell script that will manually replace latest JAR in the cache.
The reason is that Gradle does not come with this functionality built-in, as Gradle uses notion of "local cache" in a strict sense, as opposed to "local repository" which is used by Maven.
The difference is that you are never supposed to save files to local cache manually.
To solve your problem the recommended way: Suppose that project A is a dependency of project B. Then you can call publishToMavenLocal command in project A to refresh the depedency. Add mavenLocal() repository in gradle.build of project B, so every time you build project B, Gradle will check the local repository when resolving the dependency A.
mavenLocal() is resolved like this:
In order to make use of a non-standard local maven repository, you can use the following configuration in your build.gradle:
repositories {
maven {
url '/Users/manuelj/apache/maven/repository'
}
}
A build.gradle sample to create a Jar file along with its logback dependencies. using mavenlocale()
apply plugin: 'java'
apply plugin: 'eclipse'
version = '1.0'
sourceCompatibility = 1.7
target Compatibility = 1.7
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.mkyong.DateUtils'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it :
zipTree(it)
}
}
with jar
}
//Get dependencies from Maven central repository
repositories {
mavenCentral()
}
//Project dependencies
dependencies {
compile 'ch.qos.logback:logback-classic:1.1.2'
}
Reference create a Jar file along with its logback dependencies.
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" />
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
I've added the external library bsh-2.0b4.jar to an android project in android-studio by going into Project structure -> Modules -> myProject -> Tab-Dependencies -> + Sign and then add the .jar file.
I also tried to copy the file into the /libs directory and then rightclick in studio and add as library... (both methods independently!!). I inserted the following code as a test
import bsh.Interpreter;
...
Interpreter interpreter = new Interpreter();
interpreter.eval("result = (7+21*6)/(32-27)");
return interpreter.get("result").toString();
I compile with the buildin button in android.
The build.gradle looks like:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
}
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
But when I compile everything I receive the error
Gradle: error: package bsh does not exist
Gradle: error: cannot find symbol class Interpreter
Can anyone help me pls?
Try this...
Create libs folder under your application folder.
Add .jar files to libs folder.
Then add .jar files to app's build.gradle dependency.
Finally Sync project with Gradle files.
1.Create libs folder:
2.Add .jar to libs folder:
3.Edit app's build.gradle dependency:
Open app/build.gradle
4.Sync project with Gradle files:
Finally add .jar files to your application.
Change your dependencies like that
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'org.beanshell:bsh:2.0b4'
}
You can now remove manually downloaded dependencies in libs directory.
What you did was adding libraries to Android Studio project only. You should always add them to Gradle build files as only this is interpreted by Android Build Tools.
There is also new version of build tools 18.0.1, you can install them and change version in you build.gradle. As far as I know they can handle aar dependencies better.