Android Studio: how to add and use project as library, not as module

爷,独闯天下 提交于 2020-01-05 04:58:10

问题


I have 2 libraries:

  1. chilkat-9.5.0-android-all
  2. simple-file-chooser-master

I have copied both into libs folder and used it as libs. When I try to import these libs into my java class I got red markup on their names. It means that android studio can't find thees libs. Anyway i had add file chooser as project to my main application. But when I 'Run' my app firstly I've got compiled file chooser not my main app.

So my main question is how to add .zip or .jar files as LIBRARIES not as project or module.

I will share these libraries download links:

  1. https://www.chilkatsoft.com/chilkatAndroid.asp

  2. https://github.com/ingyesid/simple-file-chooser

I know that this is not programming question but still useful for many people who started to use android studio after eclipse.


回答1:


I implemented a .jar library today in my project and did the following:

  1. add the .jar file to the libs folder of your project

  2. in the build.gradle file add: compile fileTree(dir: 'libs', include: ['*.jar']) to the dependencies.

  3. Build your project

  4. now try to import the objects in your class :)

like this: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }

other thing you can try is: right mouse click on your .jar in Android Studio and select "make module". - and build again.




回答2:


Here is i did it and it worked for most libraries. Since the libraries have their own manifest files they have got to be used. Extract the master-zip elsewhere and copy only the library folder into your project direct folder where there is the app folder. When done then open the application settings.gradle file and edit it like below:

include ':app', ':mylibrary'

Sync the project then open the build.gradle of your application then go to dependencies and add your library to it as below:

compile project(':mylibrary')

Sync the gradle with project then if any errors occur within the library gradle then open it and match it with your project data. Usually you would want to change compileBuildTools, sdk and other stuff. Happy coding



来源:https://stackoverflow.com/questions/29991716/android-studio-how-to-add-and-use-project-as-library-not-as-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!