How to add .jar file in libs folder of android studio using plugin

How to add new abc.jar file in libs folder in android studio ? I have included abc.jar in resources folder in plugin development environment. but I dont know how to copy it libs folder

4 comments
Comment actions Permalink

in Project Structure:
     add abc.jar to a library (either local or global)
     then add this library to the module Dependencies.

;)

0
Comment actions Permalink

Thanks , but I want to add it using plugin . any API is there by using it I can add file in every project by default after installing "Plugin written by me"

0
Comment actions Permalink

eh sorry I misunderstood

I don't know how to add it programmatically.

0
Comment actions Permalink

I did it like this:

    public static void copyJarResourceToLibs(Module module, String destJarName, String jarResPath) {
        AndroidFacet androidFacet = AndroidFacet.getInstance(module);
        VirtualFile rootDir = AndroidRootUtil.getMainContentRoot(androidFacet); // Internal API, this is just an example
        Project project = module.getProject();
        VirtualFile libsDir = rootDir.findChild("libs");
        VirtualFile jarFile = libsDir.createChildData(project, destJarName);
        InputStream in = MyClass.class.getResourceAsStream(jarResPath);
        try {
            OutputStream out = jarFile.getOutputStream(project);
            try {
                IOUtils.copyStreams(in, out); // Writes the contents of in to out
            } finally {
                try { out.close(); } catch (IOException e) { }
            }
        } finally {
            try { in.close(); } catch (IOException e) { }
        }
    }


Usage:

copyJarResourceToLibs(module, "MyJar.jar", "/libs/MyJar.jar");


(Error checking and whatnot removed, make sure to handle those yourself. This is probably not the best way to do it, but it works)

0

Please sign in to leave a comment.