Situation is that I in my project use library A. I also import external library which also has itself a library A. So as you can assume, when I try to compile, I receive Multiple DEX files define error which means that there are duplications.
However, If I remove my library from the project, I cannot use its provided methods. And I cannot find how can I remove that library from the module.
Any suggestions?
You should be able to exclude it like this:
compile('library:1.0.0') {
exclude group: 'something', module: 'something'
}
So do this on the external library for all the things you're using that's causing a problem.
From here: https://discuss.gradle.org/t/how-to-exclude-transitive-dependency/2119/2
define multiDexEnable True in your build.gradle(app)
defaultConfig {
multiDexEnabled true
}
and also define in dependency in same build.gradle(app)
compile 'com.android.support:multidex:1.0.1'
Thank you guys for your suggestions.
I didn't want to do as Michael suggested because I believe it is kind of useless (having multiple libraries with same purpose) (already knew this trick).
I have tried Ядм Жцмдшдт answer, but couldn't succeed in compiling code completely. I have received various errors.
In the end I have solved my own issue. What I did:
Remove library from my main app libs folder. Remove dependancies if any in Android Studio (File -> Project Structure -> Dependencies (On module app) -> remove if any regarding your library.
Clean project in Android Studio (Build -> Clean Project).
Go to the module where my library A is. Go to that module build.gradle file and add following line in the dependencies cluster
compile files('libs/libraryA.jar')
Sync code and enjoy results.
TLDR
I didn't have libraryA compiled in my external module but it threw me duplication error, that's where I was confused. By removing it from my main project and adding it to my module's compilations list solved the problem.
Related
I'm pretty new to android studio, but i got this error and i can't run my project. I saw a lot of answers which suggest to add the support-annotations dependency, but I'm using appcompat so i don't need that. Any thoughts?
Do you have the Android Support Library?
To do it add this to your build.gradle file of the "app" module:
dependencies {
...
implementation "com.android.support:support-annotations:27.1.0"
}
Then sync the project.
Sometimes you can do this automatically by pressing Alt+Enter over the error message and selecting the solution.
You need to fetch the libraries that contain the imports, in your build gradle file of your module add the line inside dependencies tag:
api 'com.android.support:support-annotations:X.X.X'
or
implementation 'com.android.support:support-annotations:X.X.X'
Where X.X.X is the version of the rest of your support libraries.
You should read this article that explains how to implement this with more detail.
I have a library project used by my project. Also my project has several build flavors. In code I reference some classes from that library. I need that library to be removed from my .apk for some of the flavors. I know that this will lead to NoClassDefFoundError but it's ok for me.
I tried to use flavorCompiletask instead of compile but this were leading to errors during compilation of my project (as expected). So how can I make that libeaey project present during compilation but removed from .apk?
P.S. I know that I can remove it during compilation AND alter some of the code files which are using the classes from that library but for me NoClassDefFoundError is ok so I'd like to not have different versions of the same .java files per flavor.
EDIT1
I've found this article but Gradle version seems to be older then current one. Also now I know that I'm looking fot ability to add compile time dependencies.
you could add flavour specific compile block in dependencies, so ex. if you have flavour free and pay
use
dependencies {
payCompile '<something>'
}
you might use provided:
dependencies {
provided 'optional-lib'
}
I am using Android Studio 1.2
I create a private library I want to use that one in another application.
To use it I create an AAR files, but this AAR don't work. I have in my library a dependency to an AAR file.
The AAR files do not the dependencies?
If I use the jar and I includ ans create all the dependencies the project works fine.
NOTE :
I know how to importe the AAR file. The problem is to use an AAR in the AAR..
Thanks.
If I'm understanding your question correctly, there are 3 projects involved:
Library Project 2 --> Library Project 1 --> Application Project
You are editing "Library Project 1" and have added to it's app/build.grade a dependency on the Library Project 2's aar. Something like this: compile 'com.arasthel:gnavdrawer-library:1.1.5'
I am not sure where you are running into an issue, but I'll attempt an answer anyway. If I'm completely off-base, can you please elaborate on how the AAR dependency is not working? Any error messages?, a class/resource not found, etc.
I think it's unlikely you are unable to use a class from Library Project 2 inside Library Project 1, because I just tried this myself and it seems to be working just fine. It's worth noting that the Library Project 1 aar file will NOT include classes or resources from Library Project 2. Library Project 2 will be noted as a dependency in Library Project 1's pom if published using gradle's maven plugin to publish Library Project 1.
My guess is that you are having a problem in the Application Project? Perhaps the class from Library Project 2 is not found in the Application Project?
If that is correct, then there are two possible solutions:
Enable transitive dependencies on the aar dependency in the Application project's app/build.gradle: Instead of compile 'com.example:myLibrary:versionX', make it compile('com.example:myLibrary:versionX'){transitive=true}. I just verified this causes gradle to read Library Project 1's pom and automatically add dependencies found there into the Application Project.
If you would like to use transitive dependencies, your Library Project will need to be generating a pom and publishing it along with the aar. See https://stackoverflow.com/a/30085677/431296 for some additional information on how I have this working.
Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.
Add following code to you project build.gradle file, and you should put you AAR file to the libs folder.
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
And finally add compile info to your dependencies:
dependencies {
compile(name:'AARFileName', ext:'aar')
}
In my project, i have a module abc:
//abc module (lib) dependency
dependencies {
compile 'com.android.support:support-v4:21.0.3'
}
And my app's dependency:
dependencies {
compile project(':abc')
}
And the errors are:
“Attribute ”mnp“ has already been defined”
“Attribute ”xyz“ has already been defined”
...
And i found that all these attributes are defined in declare-styleable of module abc and they are quite a lot. I dont know where they produce the duplicate since i can run normally from Eclipse, not Android Studio. Some suggests that to manually rename the attributes of those but I think it is time consuming.
Any better solution? Thank you!
This happens to me as well when I import project from Eclipse. Android Studio seems to binds library project even thou there was an error adding library modules.
I can usually fix this by firstly delete library modules, then opening Project's settings.gradle file and delete imports from there. After this, import of library modules works well.
And when I have issues with styleable file, I can usually fix this by setting SDK version to at least 20 (Android 5.0)
First of all, I know how to add a local library to the build.gradle file, it was discussed in several questions here already (which are all basically the same), see here, here and here. But you have to hardcode the paths in the compile files('/path/to/lib.jar') statements in the build.gradle file, which isn't nice, not redistributable, etc, IF you use a library not within the project's folder structure. I prefer to maintain this library for all my projects in the same place (so it is always up to date for all projects etc.). So I would like to know how to add a library, which is not available via Maven, to an Android-Studio project using gradle, in a sane way, given that the library is added as a global library in AS's preferences.
What I have done so far:
I use Google's new Android-Studio, which uses gradle for the build management, to build an Xposed framework module. For that, I have to include an external library, XposedLibrary, which I downloaded from the respective Github repository to keep it up-to-date.
It contains the jar XposedLibrary/XposedBridgeApi.jar, which I added in AS as a global library (Ctrl+Shift+Alt+S -> Global Libraries -> green plus to add the folder XposedLibrary). The compilation failed, complaining that it doesn't know the imported classes. So I had to manually add the library to the build.gradle file, adding the respective line in the dependencies like so:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar')
}
I tried out to just add compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') but this didn't work.
So, what is a nice way to add an AS global library to the dependencies without using full paths? (I don't like the idea of symlinking to the jar file from within the lib/ folder ;) )
when referencing a file via
files("relative/path/to/a.jar")
the relative path is evaluated relative to the buildscript this snippet is in. so when your build.gradle file is located in let's say '/a/project/build.gradle' then the jar should be in '/a/project/relative/path/to/a.jar'. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via
rootProject.files("relative/to/root/a.jar")
hope that helps,
cheers,
René
This post describes how to get XposedBridgeApi.jar working with Gradle in Android Sudio: http://forum.xda-developers.com/showpost.php?p=41904291&postcount=1570
I think here is the proper way:
Import Xposed in Android Studio
Edit the /app/build.gradle like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided fileTree(dir: 'deps', include: ['*.jar'])
}
The best way is to use "provided files('src/XposedBridgeApi-54.jar')" as the lib isn't allowed to be included in the module, because the XposedBridge is already installed on the phone.
With Android Studio, you have to first understand that the IDE uses the same model for a project that your command line build (gradle) uses. That is why the Project Structure dialog has a pop up that says edits here will have no effect. So adding a global library will also have no effect.
The correct way to fix such issues is to edit your gradle build scripts so that the command line gradle build works properly. Then you should just have to click on "Tools | Android | Sync Project with Gradle files" menu item to refresh the project structure in the IDE.
Finally, if your dependencies are not going to be in Maven Central, then you'd have to create a local maven repository. Read the thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for background.