The correct way to add a global library in android-studio / gradle

First of all, I know how to add a local library to the build.gradle file, it has already been discussed in several issues here (they are all basically the same), see here , here and here . But you must hard-coded the paths in the compile files('/path/to/lib.jar') in the build.gradle file, which is not good, but not redistributable, etc. If you use a library that is not included in project folder structure. I prefer to maintain this library for all my projects in the same place (so this is always true for all projects, etc.). Therefore, I would like to know how to add a library that is not accessible through Maven to an Android-Studio project using gradle in a reasonable way, given that the library is added as a global library to AS preferences.

What i have done so far:

I use the new Google Android studio, which uses gradle to control the assembly, to create the Xposed framework module . To do this, I must include the external XposedLibrary library, which I downloaded from the corresponding Github repository in order to keep it up to date.

It contains the jar XposedLibrary/XposedBridgeApi.jar , which I added to AS as a global library (Ctrl + Shift + Alt + S β†’ Global Libraries β†’ green plus to add the XposedLibrary folder). Compilation error, complaining that she does not know the imported classes. Therefore, I had to manually add the library to the build.gradle file, adding the appropriate line depending on something like:

 dependencies { compile files('libs/android-support-v4.jar') compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar') } 

I tried just adding compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') , but that didn't work.

So what is a good way to add a global AS library to dependencies without using full paths? (I don't like the idea of ​​symbolically linking to a jar file from the lib/ ; folder))

+4
source share
5 answers

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

+3
source

when accessing a file through

 files("relative/path/to/a.jar") 

the relative path is evaluated relative to the buildscript in which this fragment resides. Therefore, when your build.gradle file is in let say '/a/project/build.gradle', then the jar should be in '/a/project/relative/path/to/a.jar. In an assembly with several gradle projects, you can put the jar in a folder relative to the root project and specify it in all subprojects through

 rootProject.files("relative/to/root/a.jar") 

hope this helps, greetings,

Renee

+5
source

I think this is the right way: Import Xposed into Android Studio

Modify / app / build.gradle as follows:

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) provided fileTree(dir: 'deps', include: ['*.jar']) } 
+1
source

It is best to use the "provided files ('src / XposedBridgeApi-54.jar"), since lib cannot be included in the module, since XposedBridge is already installed on the phone.

+1
source

In Android Studio, you must first understand that the IDE uses the same model for the project that your command line (gradle) uses. This is why a pop-up window appears in the Project Structure dialog box saying that changes here will not have any effect. Therefore, adding a global library will also have no effect.

The correct way to resolve such problems is to edit the gradle build scripts so that the gradle command line command works correctly. Then you just need to click on "Tools | Android | Project Sync using gradle files" to update the project structure in the IDE.

Finally, if your dependencies are not in Maven Central, you will have to create a local maven repository. Read this thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for the background.

0
source

Source: https://habr.com/ru/post/1487339/


All Articles