Code sharing in Android Studio

后端 未结 4 1993
臣服心动
臣服心动 2021-01-18 07:53

I have started working on a project where I will need to share a bunch of Java classes across a bunch of apps. In Eclipse it was possible to create

相关标签:
4条回答
  • 2021-01-18 07:55

    You have a couple different options.

    One option is to maintain your libraries as separate projects and compile them to an archive format, such as JAR or AAR; JAR files are for pure Java libraries, and AAR is for Android libraries (which contain code that accesses Android APIs and/or has Android resources). As was pointed out in the comments, AAR doesn't force you to publish your code to the world any more than JAR files would; it's just an archive file format whose files can be local to your machine or your organization.

    With that archive file in hand, you can include it in other projects. If you're part of a multi-developer organization, you may find it convenient to use a repository manager to publish and maintain those libraries within your organization, and you can use Maven coordinate-style specs to include libraries in your projects, which you don't have to manually copy over to your development machine.

    The disadvantage of this approach is that it makes it a little harder to make changes to those libraries: you need to load up the project, make changes, build an archive, and distribute the archive.

    The other approach is to keep the library as a source module like you did in Eclipse. You observed that Android Studio will make a copy of the module if you import it via UI, but if you bypass the UI and modify the build scripts directly, you can do what you want, which is to use the module in-place and share a single copy among multiple projects. To do this, work in your settings.gradle file and add this:

    include ':module_name'
    project(':module_name').projectDir = new File(settingsDir, '../relative/path/to/module')
    

    I would strongly encourage you to not use a pure relative path here; in this example, the path is anchored to the settingsDir variable supplied by Gradle, which is defined to be the directory where settings.gradle is found. If you use a pure relative path (i.e isn't anchored to anything), you're dependent on the working directory being the same in all environments where the build file is run (command line vs. Android Studio vs. CI server), which isn't a good thing to assume.

    0 讨论(0)
  • 2021-01-18 08:01

    You need to think in the eclipse projects as Android Studio/IntelliJ Idea modules. Then, you can generate android (or java) libraries and then include them in your project.

    To mark an Android Studio module as a library you can go to File -> Project Structure -> Facets and there click on Library Module

    enter image description here

    0 讨论(0)
  • 2021-01-18 08:09

    I was in same situation as you, and i founded an approach using git. Steps to do, to have library:

    1. Create project in Android Studio.
    2. Create android library module in that project.
    3. In that library module create git repository.
    4. Add modulename.iml in .gitignore file
    5. Use GitHub or Bitbucket for private cloud repository. and push your library to it.
    6. Create new android library model in any project that you want.
    7. Close Android Studio (not sure is that mandatory).
    8. Using explorer go to your created module folder.
    9. Remove all data in it, except modulename.iml.
    10. Clone your library from "GitHub" into it.

    That's all.

    Now you are able to use library in multiple project whether you are at home or at work. Just after finishing you work not forget to push library changes. And after opening new one pull them. I think you can automate this thing somehow.

    The benefit is that you don't need to build any jar, or aar.

    0 讨论(0)
  • 2021-01-18 08:21

    You can certainly create and use a library without making it open source or available to others.

    First, you don't need to make it an aar unless it contains Resources. If it's just plain classes, you can just make it a .jar file.

    Either way, the easiest way to share these libraries (either aar or jar) is to set up your own repository. Nexus and Artifactory are the two most common repository managers.

    You keep the library in its own project, and then publish it to your own, in-house repository.

    Then, projects that need to use the library are configured (in gradle) to use the in-house repository, and get the library from it.

    0 讨论(0)
提交回复
热议问题