I am trying to convert an Android Studio project as a library.
What is the best way to do this and the steps I need to follow?
What file can I remove from that library project, so that the library look cleaner?
An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. Unlike JAR files, AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
Convert an app module to a library module
Open the build.gradle file for the existing app module. At the top, you should see the following:
apply plugin: 'com.android.application'
Change the plugin assignment as shown here:
apply plugin: 'com.android.library'
Also in this file, you have to delete this line
applicationId "your.application.id"
Click Sync Project with Gradle Files.
Check this for more details https://developer.android.com/studio/projects/android-library.html
You need to do THREE things :
1- change the apply plugin: 'com.android.application' in the build.gradle of the module to apply plugin: 'com.android.library'
2-delete applicationId "your.application.id" in the module's build.gradle
3-remove <application and it its content from your module's AndroidManifest.xml file
4-also remove the resources in mipmap directories and the themes and colors.xml (or any other resource file that might conflict with the parent or app module) to avoid any surprises and crashes when using this library module from a module that has the same files.(I have wasted 4 hours to know the reason of app crash at startup with no helpful logs).
Related
I created android application and need to converted it to library.
I changed module build.gradle file by changing
id 'com.android.application'
to
id 'com.android.library'
and it works great.
But for adding new features a need to convert it again to app.
I can use 2 files build.gradle and build.gradle.app and change between them,
but the question is what is the best practice for managing both app and library in the same project
If you want to test your library, then you need to have a project in which there will be two modules. The first (library) is the library itself and the second module(sample) is the project to which you link this library. In sample you can check all the new functions of your library.
library module - id 'com.android.library'
sample module - id 'com.android.application'
and inside sample build.gradle add this, to connect the library
dependencies {
...
implementation project(':library')
...
}
This is how the project structure looks like
I'm trying to create an Android library for two days already.
Realy need your help.
I have an existing App which I want to do as a Library.
According to Android developers documentation:
f you have an existing app module with all the code you want to reuse,
you can turn it into a library module as follows:
Open the module-level build.gradle file. Delete the line for the
applicationId. Only an Android app module can define this. 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'
I have done this step.
The next step is saying:
When you want to build the AAR file, select the library module in the
Project window and then click Build > Build APK.
I don't really understand how to build the AAR file.
Also in my library, I have others dependencies which I need to be in my Library.
I tried a lot of suggestions in StackOverflow but didn't find the answer.
Unfortunately, I didn't find a good example of creating an Android Library with dependencies.
I don't really understand how to build the AAR file
Just compile the library or use ./gradlew build.
The output will be stored under the library's subdirectory under build/outputs/aar.
I have others dependencies which I need to be in my Library.
The aar file doesn't contain the transitive dependencies.
I suggest you publishing your library into a public or private maven repository.
I'm stuck trying to add https://github.com/ge0rg/MemorizingTrustManager to my Android Studio project (which consists of one MainActivity only at the moment). I tried the steps in the readme and did some Google research but no success. I just can't manage to integrate this package as library. Maybe someone has already succeeded doing so and can help me out.
Whenever I try to add this as library, its add only example module(example is the name of module in the library) but I want to add whole project in studio project.
EDIT
Project Screenshot
Download Demo Screenshot
It seems like you've put a folder of sources in the libs directory. It would be better to included those sources as their own module just as you have done with the emoji library.
Click an existing module
Press F4
Click the Green Plus add-module button in the top left.
Select Import Gradle Project
Select the MemorizingTrustManager sources directory
Double check that Android Studio has added dependencies for you, otherwise look in settings.gradle and add the module name (eg. include :memorizingTrustManager), then in your main module, add the same module to dependencies:
compile project(':memorizingTrustManager')
Finally check the gradle file of the imported MemorizingTrustManager module and ensure it has
apply plugin: 'com.android.library'
instead of
apply plugin: 'com.android.application'
and that it does not contain an applicationId
The modules in Android Studio are all defined in settings.gradle which is in root directory of a project. But why are they different? I mean why a module is android library or main module.
I hope I explained my question clearly. If not, please let me know. I really want to figure it out.
I tried adding an launcher activity in android library module, but it cannot be changed to a regular module. I want to know can I change a library module to regular module ?
Thank you
An android library is supposed to be used by other modules and it cannot be launched on its own. While a regular module has a launcher activity defined which is launched everytime you click on the application icon
If you are using gradle you need to change
apply plugin: 'com.android.application'
to
apply plugin:'com.android.library'
Thank you for Ajit Pratap Singh. His answer solved my problem. At the top line in build.gradle files of each modules, there is "apply plugin:" text defines which type of module it is. like 'com.android.application' is regular module, 'com.android.library' is library module.
I'm trying to migrate from maven to Gradle for an Android project and I am facing the following issue:
I have three projects i.e
root
- projlib
build.gradle
- projA
build.gradle
- projB
build.gradle
build.gradle
settings.gradle
Basically what I want to achieve is that projB depends on projA and projlib. projlib is a lib folder that compiles and generates a lib(jar) file. projA is an Android Application and projB is another Android Application that needs to reference code in projA. Right now what I have added in the projB build.gradle file is
dependencies {
compile project(':projlib')
compile project(':projA')
}
So say if there's a class
FooProjLib in projlib and
FooProjA in projA
Then In projB I can do
FooProjLib foo = new FooProjLib
which works fine
but when I do
FooProjA foo = new FooProjA
Gradle gives me package projA does not exist, what I have observed is that both dependency is resolved but only the lib can be reference and not the apk.
Does anyone have an idea how to solve this?
You can't do exactly what you want. projA can't build an application (i.e. an APK) and also have other things depend on it. projB can only depend on projA if projA is an Android library, meaning in its build file you have this declaration:
apply plugin: 'android-library'
instead of
apply plugin: 'android'
Of course, this means that projA won't build an APK, but will build an AAR instead.
If projA needs to also be an APK, you'll have to restructure things such that the common code that's in projA that projB also needs is moved out to a shared library. If both projects are similar, perhaps you could have just one module for them and use project flavors to differentiate them; it's hard to say whether this is a good approach without a lot more information.