How to create your own library for Android development to be used in every program you write?

混江龙づ霸主 提交于 2019-11-26 07:58:08

问题


I am a Delphi programmer and have written, over the years, hundreds of classes and routines which I can use in every Delphi program I write.

This library is called dlib and can be used in every Delphi program by putting this folder in my library path and using one of the units in the uses section of a Delphi unit.

Being completely new to Java and Android development, I am wondering how to do this in similar way.

So my question, how can I write own classes, put them in some global folder, and use these classes and routines in every Android program I write ?

I know this is a basic question, which I can probably find out by searching Google and trying it out in Eclipse, but if someone can put me on the right track, I know I will save much time.

Thanks.


回答1:


You have to create Android Library Project. Create android project in Eclipse, enter Project Properties -> Android and check isLibrary property. Now you can add this library to your Android Application project by adding it to list on the same property page.

More detailed instructions here in Working with Library Projects section




回答2:


Instructions for creating a library in Android Studio:

Create a library module

To create a new library module in your project, proceed as follows:

  1. Click File > New > New Module.

  2. In the Create New Module window that appears, click Android Library, then click Next.

    There's also an option to create a Java Library, which builds a traditional JAR file. While a JAR file is useful for many projects—especially when you want to share code with other platforms—it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So this guide focuses on creating Android libraries.

  3. Give your library a name and select a minimum SDK version for the code in the library, then click Finish.

Once the Gradle project sync completes, the library module appears in the Project panel on the left. If you don't see the new module folder, make sure it's displaying the Android view.

Convert an app module to a library module

If you have an existing app module with all the code you want to reuse, you can turn it into a library module as follows:

  1. Open the module-level build.gradle file.

  2. Delete the line for the applicationId. Only an Android app module can define this.

  3. At the top of the file, you should see the following:

    apply plugin: 'com.android.application'

    Change it to the following:

    apply plugin: 'com.android.library'

    Save the file and click Tools > Android > Sync Project with Gradle Files.




回答3:


If your library is in .java files composed of java code. There's a really detailed tutorial of how to use the library at mobile.tutsplus.com. Link below:

http://mobile.tutsplus.com/tutorials/android/android-essentials-creating-android-compliant-libraries/

For Example I wanted to use the Pull To Refresh library by Chrisbanes at Github.com here https://github.com/chrisbanes/Android-PullToRefresh/tree/master/library. The library's structure is in the form of an Android app. It has the form like below:

res/
src/
AndroidManifest.xml
pom.xml
project.properties

How to use on Eclipse:

  1. Create new project in Eclipse. Give a name to your project. Select "Create project from existing source". Select the location of the root folder containing the above mentioned files in "Location". Select your target and click finish.
  2. Select properties of the newly project you created. Select "Android" option. Select the "Is Library" checkbox if it's not already selected. close properties.
  3. Add a reference to the library from the project that is going to use this library. Select your project that uses this library. Open Properties. Select "Android" option. At the bottom on the "Is Library". Don't select checkbox of "Is Library". Click "Add" button on the right. Your project that you created on step 1 and 2 should be listed ready for selection. select it and click apply. close properties.
  4. You're ready to reference the classes from your project.



回答4:


With java, you create a Java Archive (jar) that contains all your classes (*.class files) of that library and the jar file is your library.

To use it, simply add it to the classpath.

(For "jar" and "classpath": basic Java concepts, please use google to find tutorials, you'll have to understand those concepts anyway, the sooner, the better ;) )




回答5:


Convert all of your class in Java and make a jar file. Use this jar in your android project by copying in libs/ folder and then adding in to build path. Make a clean of project and then run it.



来源:https://stackoverflow.com/questions/4085313/how-to-create-your-own-library-for-android-development-to-be-used-in-every-progr

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