Add a dependecy in Android Studio project from plugin Follow
Hello,
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!
Please sign in to leave a comment.
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.
It works! Thanks a lot.
Is there any way to use GradleSettingsFile and GradleBuildFile for plug-in development?
Got it by including <INTELLIJ-DIR>/Contents/plugins/android/lib/*.jar files.
@davidgreens
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);
dependencies.addFile(COMPILE,newDependency);
}
file.applyChanges();
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('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'
compile files('libs/easywsdl/ExKsoap2-1.0.2.0.jar')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)
Thanks,
Roman