This question already has answers here:
How to make a .jar out from an Android Studio project
(11 answers)
Closed 5 years ago.
I want to create some library that i will use in the future beside my current project.
I can't find a way to create library in android studio.
How to do it on android ?
A library module is useful in the following situations:
When you're building multiple apps that use some of the same components, such as activities, services, or UI layouts.
When you're building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both.
In either case, simply move the files you want to reuse into a library module then add the library as a dependency for each app module.
To create a new library module in your project, proceed as follows:
Click File > New > New Module.
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.
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.
Visit https://developer.android.com/studio/projects/android-library.html
As introduction I would suggest you to peek into this conceptually simple tutorial. Basically you can start your own library module when you chose your project, without adding any Activity. Then you create your Java Class, usually with a View. When your library is ready, with all its business logic, you can glue everything inserting in the top level build gradle the instruction you are using a library, basically before you remove the following line, that is not needed for a library:
applicationId
(This line in your gradle file is a unique application ID that looks like a Java package name, that identifies your app to the device you are running and in google play)
and then you change this line:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.name_library'
When you have a more structured project you can follow the official documentation
As it is well explained the difference between a normal Application and a Library is:
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.
I would not encourage you to use solutions like web services that do it on your behalf, namely just copying/pasting your existing code. In fact a library often needs specific architectural choices, so is important to consider and learn different factors, is not just writing some business logic is quite complex to explain, but you can imagine that also the choice of what the user can see and modify can be crucial. Also should be as much as possible bug free, because once it is adopted could cause problems to the users. I remand you to a famous post, superbly written where you can find some solution to this aspect.
you can use https://jitpack.io/ is very easy publish an android library. just upload your code to github/bitbucket and paste the repository link on jitpack website. that's all
Related
I am using Android Studio for developing Android apps. But I have heard in Android Studio it is better to have only one app in a single (one project per app) if that is right, then it will be very wasteful to open many frames for many projects. But when I searched I found that
Android Studio project = Eclipse workspace
Android Studio module = Eclipse project
Now, if this is true, it means that Android Studio also can support a multi-app project. If yes, then, is every app in Android Studio independent like in Eclipse (i.e. they do not disturb each other by sharing any file or setting)? Or can we have many apps in a single project? If we can then is there any point to take care of?
Thanks!
Yes, you have two options:
Option 1: Create an addition app module
First create your standard Phone & Tablet Android project, including the auto-generated app module.
Add a new app module: File > New > New Module ... > Phone & Tablet Module
Complete the wizard and name your Application app2 for instance.
Now you'll have both app and app2 in the same project.
To actually run app2 you first need to select it in the pull-down menu in the top toolbar of Android Studio, next to the Start and Debug icons. You can also do this though Run Configurations: Run > Run... > Edit Configurations... and modifying Module.
Option 2: Create an addition library module
This is ideal for creating a separate library that is isolated from the app, and can be shared across more apps (or other projects):
Add a new library module: File > New > New Module ... > Java Library.
Complete the wizard and give your library a good name, like libgoodstuff.
Now libgoodstuff and app will reside in the same project.
To make app sources depend on libgoodstuff, you first have to add the library module to the project settings.gradle to look something like this:
include ':app', ':libgoodstuff'
Then in app/build.gradle you have to depend on the library module like this:
apply plugin: 'com.android.application'
···
dependencies {
···
implementation project(path: ':libgoodstuff')
···
}
···
Yes you can. Inside your project if you want to create a new app do the following:
Create a new module by right clicking on your project -> new -> module
Select phone and tablet module
Now you will be able to run either app. This is an excellent way to share code between two apps as it allows you to keep and develop your libraries in one location.
You can definitely have multiple app modules in the same Android Studio project. Having said that, I've yet to find a reason to define multiple app modules in a project.
If you need different version of the same app, Gradle's build variant is powerful enough to satisfy perhaps 99% of the use-cases (I have a project with a dozen variants, each with its own custom code/res).
If you are writing different apps then it's better to make each its own project so apps don't inadvertently change each other's behaviour.
Not sure what you mean by "is every app in Android Studio independent as Eclipse", but each module is its own world by default unless dependencies to other modules are explicitly defined.
Adding this as an answer since I don't have enough reputation for comments yet.
For the answer to your question - Check this question that I have raised. Is this the same boat you were in ?
TL;DR
I was able to have multiple apps in the same Android Studio Project, build and run them without any issues. Another member
corroborated my claims in the comments on the Question.
#Android Studio Pros : Please check the above link and add your insights. This seems to be a confusing aspect.
My Take
I think I agree with #Kai's answer. But there are instances where we want multiple apps to have common library dependencies and don't want to duplicate the library dependencies. Wouldn't multiple apps be fine as long as the common library dependencies have ONLY common code and nothing else. The separate modules hold the individual app related code and that's where the differentiation is.
Yes, it is possible. As the existing answers showed, it’s quite straightforward to create additional application module in the same Android Studio project.
So I’ll try to answer underlying question why anyone might need it.
It’s certainly not worth it to put multiple completely independent apps in one project.
However, if you app is big enough, you might benefit from putting separate features into separate modules. You can also create a separate executable app module for each feature, so that you can:
launch/debug each feature separately
save some time on every compilation/dexing/putting everything into a single apk
encourage teams/developers to work independently, and even in separate repositories.
The main app module can be used only to combine existing features together.
I’ve recently created an article demonstrating this approach where I tried to explain everything in more details: https://medium.com/#domplebump/multiple-application-modules-in-one-android-project-36e86ceb8a9
Background
I'm working on an app that has become very popular, so much that a part of it is supposed to become an SDK (which would be available for developers), and the app will split to 2 apps (both use the SDK).
According to what I know, there are multiple ways to create an SDK module (previously called "project" on Eclipse) :
Completely open sourced (Android library) - all sources and resources are open sourced and can be modified. An example might be Facebook's SDK and a lot of Github repos.
a single Jar file, which can be closed sourced.
The problem
Sadly, I can't make the SDK open sourced, and it should relatively be protected vs prying eyes (obfuscated etc...).
The issue here is, the SDK needs to use some resources of its own (drawables, strings,...), and so far (because I didn't have a lot of experience with creating SDKs) I've found 2 ways to handle resources for SDKs :
use reflection and/or "context.getResources().getIdentifier" . This is quite messy, as I lose the whole "R" usage of the code. Also, it has issues with "styleable" , as I've written here. It also makes it hard to find unused resources.
even worse ways: put resources in assets folder, put files in a wacky way inside the jar file, ...
Note that a part of the SDK includes custom views (for example, classes that extend from TextView), so even if I do split the SDk into 2 modules- resources and java files, both might have issues of dependencies (each uses the other one).
The question
Is it possible to somehow solve this issue?
Is it possible for the code part of the SDK to remain closed sourced, reach the "R" file as usual, and make it easy for both me and whoever use the SDK ?
How would I then generate the jar file as being obfuscated via Android Studio? and is it possible to prepare it to to be used via gradle afterwards?
Can I maybe make the Android-library of the SDK into an obfuscated jar file and not worry about the "R" file ? I ask this because this way I could enjoy both worlds: for our apps, it would remain open sourced, and for third party apps it would be closed sourced.
EDIT: seeing that this is supposed to be easy, I've tried it myself. I've created a totally new POC project which has an Android library module called "sdkmodule", and added this class to it:
public class SdkClass
{
public String doIt(Context context)
{
return context.getResources().getString(R.string.app_name);
}
}
Then, I've made the app's module to use this one, and I wrote this code in it:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SdkClass sdkClass=new SdkClass();
Log.d("AppLog","string from SDK:"+sdkClass.doIt(this));
Log.d("AppLog","string with same ID name from app:"+getResources().getString(R.string.app_name));
}
What I expected is that the first log would print the string that's in the SDK module, and the second to show the string of the current project, but instead I got an exception:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/user/sdkmodule/R$string;
On another try, I've got the same string that's used on the app itself (the one that uses the SDK module). And, on another run, the SDK produced the needed string as I've wanted.
How could it be? What should I do ?
In addition, I've tried to make a second activity in the SDK itself, and I've created a resource there that has the same resource name (used for a textView in its layout) as of the app itself, yet with a different value, yet when I've reached this activity, I've seen the one used by the app.
Here's the project, zipped (ignore the name of the folder, I wanted to try flavors too) :
https://drive.google.com/file/d/0B-PZZGk2vPohX25WUDNKTmotUTg/view?usp=sharing
The answer to your problem is to package and distribute your library as an AAR bundle
This format allows you to provide an obfuscated SDK jar and with its resources and the R mapping file.
This format is a standard and fully supported by maven-android-plugin (actually it's the replacement of the old APKLib format which supports only the distribution of source files).
Of course it's also supported by Gradle and Android Studio.
The Android Archive (AAR) format does what you want. It's like an Android-specific JAR file and contains compiled code, but includes its own resources and manifest. You can also include obfuscation as part of the build process. By default, the current version of Android Studio (1.2) and Gradle automatically build .AAR files for all library modules you create in your project.
You can change an app module into a library project that will publish an AAR file just by changing apply plugin: 'com.android.application' into apply plugin: 'com.android.library' in your module's Gradle file. The .AAR file will be placed in your MODULENAME/build/outputs/aar folder after each build. Some more information is available here.
Edit 1, after question updated:
The resources in your AAR get consolidated with the app module when the final APK gets compiled. The app resources will override the library's. This is probably by design, to allow people using a 3rd party library to customize its resources when creating their app, without having to rebuild the library. I think the easiest way to solve your resource conflict issue would just be to name your sdkmodule resources something more unique. Why not make the string key R.string.com_example_sdk_name or something?
No, the AAR libraries don't get obfuscated by default, but you can set up ProGuard in the Gradle build file for your AAR library to take care of this. Other tools are also available.
I am working on a Android app in which I would like to add plug and play module functionality ,Say I have Two android project
[A] An app for capturing a image using camera and storing it in memory
[B]Enabling map and locate current location .
now I want to add add this functionality in my Another Android app.While going through android developer link and Android Library Projects - Tutorial I figure out that to use plug and play module I have to make my above project as library project so that I can use it in my new app ,now my questions are
1 Is this only way to use library project to add plug and play module
functionally in my app or there are another way also?
2 what are the pros and cons of using library project in order to add
plug and play module functionally?
basically I am researching on how to add plug and play service in my android app and trying to find best solution ,so any clarity on this topic will be extremaly helpful !!!
thanks in advance !!!
An alternative would be to use a linked source folder. In Eclipse, you can set this up in the project settings in the Java Build Path section. You can choose some source code directory outside your project that will virtually be in your project's space (and other projects that use it will do the same). I find this to be handy just from an IDE UI standpoint when I'm co-developing a module along with an app or two. What I don't like about library projects is that they sometimes don't update correctly within your project so you have to rebuild it and your project or clean your project to continue. Also, the module is simpler because it's just a directory of source code files, not an entire Eclipse project.
One other downside of libraries is that they can introduce conflicts that can be a headache to fight. For example, your library might have a different version of the Android compatibility library in it than your main project, and therefore give you compile time errors. Or there are sometimes errors with duplicate libraries, and you have to go fool with the Order and Export settings of your project.
If your module is going to rely on String, layout, and image resources, etc. that are common to all apps that implement the module, then a library project will be easier to work with so you don't have to pass all your resources in through your module's class constructors and duplicate them in each project.
A third option is Gradle, although as far as I know, that would essentially just help automate one of the above two options. I'm not very familiar with Gradle.
I have seen this question, and have some more doubts regarding creating a jar file which I can distribute and can be used in any android applications.
What my requirement is
As I said, I want to build and distribute a closed source library. I
don't want the source code to be visible.
In that library I don't want to use any assets, layouts, resources
etc. But I want to use some android specific codes, like getting
android device id.
The most popular answer in the above linked SO question tells to create a regular java project and import android.jar in it. I tried to do that, but I don't know how to add android.jar to any java project. I would like to get some clarification on that too.
Moreover I would like to know if there are any other methods using android sdk itself (without using java project) create a closed source library jar file.
I think what I want is possible, since google analytics for android native apps seems to have done it. I am sure in the .jar file they distribute they are using android specific codes, since there seems no other way for them to get the device information to display in the analytics viewer.
EDIT : CAN SOMEONE CLARIFY THIS??
I think I have made some progress. This is what I have done
Created a regular android project (not library project, the "is
Library" checkmark is unchecked)
In the project I have coded out my logic. It uses some android
specific classes like SharedPreference, UUID, PackageManager. But
nothing related with assets, layouts also no class extending
Activity. Just a java class extending java.lang.object
Exported the project using Project->rightclick->export->Java->JAR
file. In the next screen I unchecked the checkbox near
AndroidManifest.xml. Set a destination directory to export and
clicked next thrice with keeping the default settings. Then I clicked
Finish, and got a lovely libMyLibraryName.jar at my desktop.
Then I created another android project, added this libMyLibraryName.jar to new project using project->rightclick->properties->java build path -> libraries->add external jar.
And I tried to use my class in the library, in my new project
MyLibraryClass objClass = new MyLibraryClass(this);
And I was able to compile and run successfully. I even sent the library to one of my co worker who was able to use the library in his on machine (Just making sure library project in my workspace wont influence the project using it).
Now I have 2 questions.
1) My first question is , what they meant by the term "true library" in the below given documentation ? Is it any non android java project which can be exported to a JAR file?
However, a library project differs from an standard Android
application project in that you cannot compile it directly to its own
.apk and run it on an Android device. Similarly, you cannot export
the library project to a self-contained JAR file, as you would do
for a true library. Instead, you must compile the library indirectly,
by referencing the library in the dependent application and building
that application.
Well this portion is taken from documentation under title "Library Projects".
2) My second question is, anything wrong with the way I have created the JAR file? Any possible pitfalls which might bite me back later? I would like to make sure I am not doing something terribly wrong, before using it in my important projects.
I might add that I didn't try the method of creating a JAVA project and importing android.jar. I am ready to try that one, if what I have done currently is wrong.
The android.jar will be located where you installed your Android SDK. Under the platforms directory there should be a number of other directories named android-<version>. The android.jar will be there. Choose the one specific to the minimum android version you are targeting.
Once you have that, copy it into your project. If you're using eclipse I think you can just cut and paste jars straight into your project, right click and add it to build path. If you're not using eclipse or any other IDE, you just need to ensure that the android.jar is on the classpath when building your jar file.
After that your newly built android library can be dropped into any Android project.
In answer to your additional questions:
What they mean by a true library is a jar file as opposed to an Android library project.
I don't think there's anything wrong with the way you created the jar file. I would have made it using the android.jar as I mentioned above but your way should also work. To verify this I would examine the jar contents and make sure all you have in there is .class files.
I have an Android code base which uses APIs with settings to get different data for several apps. All apps use the same code base but with one or two design tweaks. So how do I re-use the main code base without having to copy the whole Android project each time?
iPhone uses multiple targets in the same project which works well. If android cant do this do I need to compile binaries of the code base in one project and then import into each new app project? If so how? I'm using Eclipse and am an intermediate Java developer.
Any help much appreciated!
Doug
Check out "Working With Library Projects" from the Android documentation. This should be just what you're looking for: http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject
The current way to approach this issue if you are using Android Studio with Gradle is by using Gradle, Build Type + Product Flavor
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
Build Variants
One goal of the new build system is to enable creating different versions of the same application.
There are two main use cases:
Different versions of the same application
For instance, a free/demo version vs the “pro” paid application.
Same application packaged differently for multi-apk in Google Play Store.
This new concept is designed to help when the differences are very minimum. If the answer to “Is this the same application?” is yes, then this is probably the way to go over Library Projects.
Note: This answer is basically obsolete now that one can create .aar libraries with resources. It still works, though, and there may be times when the portability of a .jar is desirable, so I'm leaving it here.
Blumer's answer is a good one, and you should definitely look into Android's idea of library projects. However, there is another alternative. If you have a module that contains only Java code, but no resources of any kind (images, layouts, etc.), you can compile it to a .jar file separately and use the resulting .jar in multiple application projects. It works like this:
Create a new Java project in Eclipse (not an Android project) and move the source code of your module there.
If your module references any classes from the SDK, you'll need to add the Android SDK .jar to the project's classpath (Project > Properties > Java Build Path > Libraries > Add JAR).
When your module is ready to use, bundle up its .class files into a .jar. You can do this manually, or you can look around to figure out how to get Eclipse to do it for you.
Copy your module .jar file into the "libs" directory of your app's main project.
Add the module .jar to the project's classpath (again, Project > Properties > Java Build Path > Libraries > Add JAR).
Now you should be able to build multiple apps using the same .jar, while maintaining only one copy of the module's source code.
Depending on your particular situation, this may or may not work any better for you than the standard Android library mechanism. But it's worth considering as an alternative.
The Android documentation recommends another approach if there aren't too many "different APIs" used.
The idea is to use reflection instead of making direction references to the code. Make sure to use optimized reflection instead of lookups every time.
References
http://developer.android.com/training/multiple-apks/api.html
http://developer.android.com/google/play/publishing/multiple-apks.html#ApiLevelOptions
You might want to consider using a source control system like Subversion (or GIT). Keep your most feature complete version in the trunk, and make branches for your separate versions that use different data sources or require minor layout changes, etc.