Add a dependecy in Android Studio project from plugin


I'm creating a plugin for IntelliJ and Android Studio. I want my plugin first download a jar to local folder and next adds it as a dependency to opened project (module). For IntelliJ I've created a code for this, but I have a problem with Android Studio projects. What I know is that it uses Gradle for this purposes. Is there any way to connect to current Gradle and add a dependency programmatically from my plugin?

Thanks for help!

Comment actions Permalink

Yes. Android Studio provides GradleSettingsFile and GradleBuildFile classes that let you access and modify the contents of build.gradle files in the user's project. For an example of using the API, please refer to CreateLibraryFromFilesAction.doOKAction() in the Android plugin source code.

Comment actions Permalink

It works! Thanks a lot.

Comment actions Permalink

Is there any way to use GradleSettingsFile and GradleBuildFile for plug-in development?

Comment actions Permalink

Got it by including <INTELLIJ-DIR>/Contents/plugins/android/lib/*.jar files.

Comment actions Permalink


Comment actions Permalink

During my development I've encountered an issue related with adding file dependency to gradle project. Here is a code which adds a few jars as a dependencies:


final GradleBuildFile buildFile=GradleBuildFile.get(module);
final GradleBuildModel file=GradleBuildModel.get(module);

DependenciesModel dependencies=file.dependencies();
for (int i = 0; i < meta.libraries.size(); i++) {
String entry=meta.libraries.get(i);
String newDependency = getDependency1(libsFolder, buildFile, entry);

But the problem is that after I run this code, dependencies are added to build.gradle file but without
new lines characters and because of this build.gradle file cannot be sync.
The result looks like this:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('', {
exclude group: '', module: 'support-annotations'
compile ''
compile ''
testCompile 'junit:junit:4.12'

compile files('libs/easywsdl/ExKsoap2-')compile files('libs/easywsdl/ksoap2-android-assembly-3.6.2-jar-with-dependencies.jar')}

As you can see last 2 compile files sections are places in one line and this cause error. If I add new lines
manually, then everything works.

Could you tell me how to solve this problem? FYI: If I change addFile method to addArtifact then for artifacts
there are new lines added, so problem is strict with addFile method (and probably the others like addModule)



Please sign in to leave a comment.