How to create a library project in Android Studio

2019-01-01 05:01发布

I'm new to the gradle build system and IntelliJ.

So how do I create an Android Library Project (e.g. com.myapp.lib1) and the application project (e.g. com.myapp.app) and make the build system include com.myapp.lib1 on the application project?

I went to the Project Structure -> Modules -> My App project and added a dependency to the lib project. IntelliJ now can recognize classes from the lib project when used in the app project, but when I run the app project, there are errors like:

Gradle: error: package com.myapp.lib1 does not exist

14条回答
明月照影归
2楼-- · 2019-01-01 05:16

You can add a new module to any application as Blundell says on his answer and then reference it from any other application.

If you want to move the module to any place on your computer just move the module folder (modules are completely independent), then you will have to reference the module.

To reference this module you should:

  • On build.gradle file of your app add:

    dependencies {
    ...
    compile project(':myandroidlib')
    }
    
  • On settings.gradle file add the following:

     include ':app', ':myandroidlib'
     project(':myandroidlib').projectDir = new File(PATH_TO_YOUR_MODULE)
    
查看更多
忆尘夕之涩
3楼-- · 2019-01-01 05:18

The simplest way for me to create and reuse a library project:

  1. On an opened project file > new > new module (and answer the UI questions)

enter image description here

  1. check/or add if in the file settings.gradle: include ':myLibrary'
  2. check/or add if in the file build.gradle:

    dependencies { ... compile project(':myLibrary') }

  3. To reuse this library module in another project, copy it's folder in the project instead of step 1 and do the steps 2 and 3.

You can also create a new studio application project You can easily change an existing application module to a library module by changing the plugin assignment in the build.gradle file to com.android.library.

apply plugin: 'com.android.application'

android {...}

to

apply plugin: 'com.android.library'

android {...}

more here

查看更多
皆成旧梦
4楼-- · 2019-01-01 05:19

Don't forget to use apply plugin: 'com.android.library' in your build.gradle instead of apply plugin: 'com.android.application'

查看更多
时光乱了年华
5楼-- · 2019-01-01 05:19

Purpose: Android library at single place - Share across multiple projects http://raevilman.blogspot.com/2016/02/android-library-project-using-android.html

查看更多
千与千寻千般痛.
6楼-- · 2019-01-01 05:23

Check out this link about multi project setups.

Some things to point out, make sure you have your settings.gradle updated to reference both the app and library modules.

settings.gradle: include ':app', ':libraries:lib1', ':libraries:lib2'

Also make sure that the app's build.gradle has the followng:

dependencies {
     compile project(':libraries:lib1')
}

You should have the following structure:

 MyProject/
  | settings.gradle
  + app/
    | build.gradle
  + libraries/
    + lib1/
       | build.gradle
    + lib2/
       | build.gradle

The app's build.gradle should use the com.android.application plugin while any libraries' build.gradle should use the com.android.library plugin.

The Android Studio IDE should update if you're able to build from the command line with this setup.

查看更多
伤终究还是伤i
7楼-- · 2019-01-01 05:24

Documentation Way

This is the recommended way as per the advice given in the Android Studio documentation.

Create a library module

Create a new project to make your library in. Click File > New > New Module > Android Library > Next > (choose name) > Finish. Then add whatever classes and resourced you want to your library.

When you build the module an AAR file will be created. You can find it in project-name/module-name/build/outputs/aar/.

Add your library as a dependency

You can add your library as a dependency to another project like this:

  1. Import your library AAR file with File > New Module > Import .JAR/.AAR Package > Next > (choose file location) > Finish. (Don't import the code, otherwise it will be editable in too many places.)
  2. In the settings.gradle file, make sure your library name is there.

    include ':app', ':my-library-module'
    
  3. In the app's build.gradle file, add the compile line to the dependencies section:

    dependencies {
        compile project(":my-library-module")
    }
    
  4. You will be prompted to sync your project with gradle. Do it.

That's it. You should be able to use your library now.

Notes

If you want to make your library easily available to a larger audience, consider using JitPac or JCenter.

查看更多
登录 后发表回答