如何创建您自己的 Android 开发库,以便在您编写的每个程序中使用?

我是一个 Delphi 程序员,多年来,我写了数百个类和例程,我可以在每个 Delphi 程序中使用我写的。

这个 图书馆叫做 dlib,它可以是 每个 Delphi 程序中都有,方法是把这个文件夹放在我的库路径中,并使用 Delphi 单元的 use 部分中的一个单元。

作为 Java 和 Android 开发的新手,我想知道如何以类似的方式进行开发。

因此,我的问题是,我怎样才能编写自己的类,把它们放在某个全局文件夹中,并在我编写的每个 Android 程序中使用这些类和例程?

我知道这是一个基本的问题,我可以通过搜索 Google 并在 Eclipse 中尝试来找到答案,但是如果有人能让我找到正确的方向,我知道我将节省很多时间。

谢谢。

88404 次浏览

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 ;) )

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

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.

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.

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.

If you're using new android studio version and gradle 7.0.3

Android Studio Arctic Fox | 2020.3.1 Patch 3 Build #AI-203.7717.56.2031.7784292, built on October 1, 2021 Runtime version: 11.0.10+0-b96-7249189 amd64 VM: OpenJDK 64-Bit Server VM by Oracle Corporation Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 1280M Cores: 2 Registry: external.system.auto.import.disabled=true

Creating module

  1. Create new project with empty activity
  2. Click file -> new -> new module -> and choose android library.
  3. After new module created you can add java class or something for your library

Export library to AAR file

You can check this for more information

Export library to jitpack.io

Set this file like this

build.gradle (project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'


// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}


task clean(type: Delete) {
delete rootProject.buildDir
}

build.gradle (module:app)

plugins {
id 'com.android.application'
}


android {
lintOptions {
abortOnError false
}
}


android {
compileSdk 31


defaultConfig {
minSdk 16
targetSdk 31
versionCode 1
versionName "1.0"


testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}


buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


dependencies {


implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

build.gradle (module: YourLibraryName)

plugins {
id 'com.android.library'
id 'maven-publish'
}


task androidSourcesJar(type: Jar) {
classifier 'sources'
from android.sourceSets.main.java.srcDirs
}


project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
groupId 'com.github.YourGithubUsername'
from components.release
artifact androidSourcesJar // optional sources
}
}
}
}


android {
compileSdk 31


defaultConfig {
minSdk 16
targetSdk 31
versionCode 1
versionName "1.0"


testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}


buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


dependencies {


implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Create file jitpack.yml in root project (YourProjectName -> Gradle -> right click -> new -> file -> name it "jitpack.yml" and put this code to jitpack.yml file

jdk:
- openjdk11
before_install:
- chmod +x gradlew
install:
#  - ./gradlew build :lib:publishToMavenLocal
- ./gradlew build publishToMavenLocal

Share your project to Github

Visit jitpack website

Follow this step for upload your library