0

I looked at the general information in Android Developers here and previous questions in Stack Overflow here1, here2, here3, and here4.

I would like to create a new "utility library" that I can use from other projects, but there is something I don't understand. Here's what I do:

First, I start new project in Android Studio 2.2 (OS X 10.11):

Application name: Test
Package name: com.example.tomihasa.test
Minimum SDK: API 15: Android 4.0.3
Activity: Empty Activity
( ) Backwards Compatibility (AppCompat)

Second, I start a new library: File, New, New Module, Android Library

Application/Library name: MyLibrary
Package name: com.example.tomihasa.mylibrary
Minimum SDK: API 15: Android 4.0.3

I now have directories:

/Users/tomihasa/AndroidStudioProjects/Test
/Users/tomihasa/AndroidStudioProjects/Test/mylibrary

Why is mylibrary inside Test? Should it be somewhere else if I want to use it elsewhere? How can I move it?

Third, in settings.gradle I have:

include ':app', ':mylibrary'

Fourth, I build the MyLibrary:

Build, Make Module 'mylibrary'.

I can not find any .JAR or .AAR package as mentioned in here.

What am I missing?

Community
  • 1
  • 1
tomihasa
  • 138
  • 1
  • 13

1 Answers1

0

You're creating a module which is inside your Test project. All your modules will be included here. The module itself should have its own build scripts and directories and can be used independent to the rest of the project if it doesn't have any dependencies of the other modules.

You can still use this module as a library in other projects if you want. Android Studio will create aar files (debug and/or release) every time you build it and put it in your Test/myLibrary/build/outputs/aar/ folder. They should be named something like {module-name}-debug.aar and {module-name}-release.aar respectively. You can also upload the aar to a local maven repository or use the aar directly in other projects. If it doesn't appear, then you may need to do a clean project in Android Studio by going to Build->Clean Project.

If you want to create a stand-alone library, you need to create a separate project. Then you have to import the built aar in to your other projects using one of the methods above.

EDIT:

To install to local maven repository, first you need to include the mavenLocal() repo in your build script.

Then in your library's build.gradle file, you need to apply the maven plugin:

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {

   ...

   task installArchives(type: Upload) {
      description 'This task will upload the build to mavenLocal()'
      repositories.mavenInstaller {
          pom.version = '0.0.1'
          pom.artifaceId = 'myLibrary'
          pom.groupId = 'com.example.tomihasa'
      }
   }
}

In the gradle console, type ./gradlew build installArchives to build and upload.

Then your projects can install it by including mavenLocal() in to their build scripts and doing:

dependencies {
    compile 'com.example.tomihasa:myLibrary:0.0.1'
}
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • The autogenerated library I made mentions jcenter in build.gradle. Can I use a library locally without any repo websites? The autogenerated .aar file is named mockable-android-25.jar. Is that normal? – tomihasa Nov 03 '16 at 23:28
  • @tomihasa: That is not your library. That is a stub .jar that contains all the Android methods so your project will compile on your computer. In your library's `build.gradle` file, you need `apply plugin: 'com.android.library` at the top. – DeeV Nov 03 '16 at 23:46
  • For me it says jcenter() instead of maven. I haven't studied repos yet, so can I do everything without JCenter or Maven until I'm ready learn repo techniques? – tomihasa Nov 04 '16 at 16:05
  • You would use both. They're nothing more than locations to look for dependencies. `jcenter()` points to a remote depository which contains most of the third party dependencies. `mavenLocal()` points to a local one on your computer. – DeeV Nov 04 '16 at 16:31
  • I thought I found a simple workaround for local "libraries": I just create a new folder in the project's Java code directory and start Android Studio, but didn't work. – tomihasa Nov 06 '16 at 09:10
  • My local/offline workaround is working: I right-click the java folder in Android Studio 2. Then I give the full name of the package. Then I create the classes I need to that directory. – tomihasa Nov 07 '16 at 18:12
  • That's fine though it sounds like every project you have is going to have to depend on this external project. The idea of the maven libraries is you could very easily distribute it to other projects without weird IDE configurations. It's also easier to distribute to other people should it get to that. – DeeV Nov 07 '16 at 18:14
  • I know. I'm quite new to Android development and trying to figure out the basics first with some books. I started with an OpenGL ES book as I'm trying to figure out if I should use a game engine or OpenGL for three dimensional apps and games. I'm trying to find out how difficult OpenGL is, so I can make a decision after that. – tomihasa Nov 08 '16 at 00:24