Before you say that this question was asked. I want to say that I have read through most of the questions and they give just workarounds for this solution and other ones are out of date.
My question is, how create Android library in Android Studio, so I can export it to closed .jar with no sources and distribute it to other developers.
When I click New Module -> Android Library, it creates me folder under my current project. I don't want that.
I want to create library from the beggining (where is option for this in Android Studio?), so it is standalone project which later I can export to jar.
Android Studio doesn't have UI to allow you to create an Android Library as a new project. To do this, you'll have to one of a few different routes.
Before you start, I'd encourage you to think about packaging your library as an .aar instead of a .jar, since you say it will be calling Android APIs. The reason for this is that .aar is a richer format for Android projects and allows the archive to include manifest information, such as the minimum required SDK and such -- it will go farther to ensure that your library is compatible with the environment it's being included in. The disadvantage is that only the Gradle build system supports .aar files, so Eclipse users wouldn't be able to use the archive.
Though you're not using Android resources, if in the future you do need to include resources in your archive, you'll be set up.
There's more information on .aar here: http://tools.android.com/tech-docs/new-build-system/aar-format
If you want to package in .aar format, you could go a couple routes:
You could create a project with the New Project wizard. This will initially be set up to make a full-fledged Android app that compiles to an APK. You can go into the app module's build.gradle file and change this line:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.library'
It will now build an .aar. However, this project will still have resources in it, and maybe layouts and activities depending on what options you chose in the wizard, so you may have to rip out what you don't want.
The alternative is you could start with a blank slate, crafting your own build.gradle file from scratch that uses the com.android.library plugin, and fleshing out the source directories. This will be more cumbersome, but as with all difficult activities, you'll learn a lot and it will build character, or you can at least tell yourself that as you're puzzling things out.
If you want to package as .jar instead of .aar, then again there are a couple routes you could go. For this type, if you want maximal assistance from the UI, you could start a new Android project and add a plain Java module to it. You can either remove the unwanted Android module from the project, or you could even leave it alone; the build process will output a .jar for your plain Java module in any event.
Your plain Java module will need to depend on the android.jar archive from the appropriate platform in your SDK.
Alternately, you could build this module up from scratch, authoring the build.gradle file and setting up the sources yourself, and then you could import that into Android Studio as a new project.
Actually it's a bad idea to use your IDE as project management/build system
Take a look on gradle or maven
also take a look to
http://tools.android.com/tech-docs/new-build-system/user-guide
however, if you want do this in ide:
File -> Project Structure. in opened window add jar in Artifacts tab
Build -> Build Artifact.
https://www.jetbrains.com/idea/webhelp/packaging-a-module-into-a-jar-file.html
it's shouldn't be specific for android lib
Related
I am planning to modify the Android framework within the source tree. I have few questions,
If i build only the framework directory what will be the output? Will it be a jar file or the whole system image?
How can I use the built framework to develop apps that use this custom framework? (will there be any android.jar or something similar?
Thank you.
Building only the framework (mmm frameworks/base) is not enough. If you do in this way you will only obtain a bunch of jar files containing built framework. To use the modifications, you need to build the full image against which you will test your changes. Do not forget to do make update-api
To use the modified framework, you need to build sdk. To do this, you need to do the following steps after you've updated the api (see more https://android.googlesource.com/platform/sdk/+/master/docs/howto_build_SDK.txt):
source build/evnsetup.sh
lunch sdk-eng
make sdk
After you've built the SDK, you need to point Android Studio project to the new location of the SDK. To do this, create a new project in Android Studio, select the root folder in the Project panel, and make a right click on it selecting the "Open Module Settings" item (or simply pressing F4). Point the location of the SDK to the folder with newly created SDK (it should be somewhere under the out/host/linux-x86/ directory if you use Linux-based host operating system).
I downloaded this library and i extracted the rar into my app\libs folder.
Then I added compile 'com.github.lzyzsd:circleprogress:1.1.0#aar' to my build gradle as it says, and clicked on sync.
Then I noticed that the max is set to 100 (in file DonutProgress.java).
I changed it to 5, clicked on sync again but nothing changed.
After few hours of trying I decided to completely remove the extracted folder from my app\libs and sync again to see what happens.
Surprisingly, everything was still working like library was still there.
Can someone explain me what's happening here, I have to edit it but it seems whatever I do to the library doesn't affect the app.
It seems like it's using another library from totally different folder, I don't know, I tried searching for DonutProgress on my computer but didn't find anything that seems useful.
Please help.
When you add compile 'com.github.lzyzsd:circleprogress:1.1.0#aar' to your build.gradle dependencies, you are telling Gradle to go fetch this library from a repository when you build.
For most Android apps, this means that when you do a build, Gradle will go and download the library (in this case a .aar) from jCenter. This is good because it means you no longer need to manage the JARs (or other library files) yourself.
If you want to use a custom version of that library, then you should not add that dependency to your Gradle build script. Instead you should add it as either a module or a local dependency (e.g. compile files('libs/custom_library_name.aar')).
I installed a bunch of updates via Android SDK Manager this morning including Android SDK Tools 22.0.1. This has seemed to be disastrous so far. One disaster is handling libraries. Here is the scenario:
I have a project projFoo that uses a library project libFoo. libFoo uses a dozen or so libraries or projects. In other words, libFoo has the following in its Java Build Path:
projA, projB, projC,...
lib1.jar, lib2.jar, lib3.jar ...
I used to need only adding libFoo to the build path of projFoo, and everything was working fine until I installed the updates.
After the updates, I have been adding projA, projB, lib1.jar one by one to projFoo's build path even though projFoo does not use them directly. Without doing this, java.lang.NoClassDefFoundError would occur when I run the app.
I have cleaned all projects and restarted Eclipse many times. I also checked those referenced projects and libraries in Order and Export of libFoo, but adding those references still seems to be necessary. Is this the right way to handle this?
In the order and export tab of Java Build Path make sure Android private Libraries and Android Dependencies are checked.
Any libraries in .jar format should just go in the libs folder of your project. There's no need to add them to the build path.
Any libraries that are a project can just be added to the Java Build Path Projects tab, or if it is an android project, you can add it to the Library section under the Android pane.
I am trying to reference a pure java-Project in my Android-Project -> The Java-Project has a whole bunch of classes I need to use. Oh, and as the first response pointed out: I am using eclipse, yes :)
Only Problem is: I Always get Could not find class 'XXX', referenced from method com.example.helloworld.MainActivity.onCreate. I seem to have missed SOME step or error...?
What I already did:
The Project is added as Project into the Java Build Path (logically nessecary)
It is marked in "Order and Export" in the Java Build Path and pushed to the top (this solved the problem for someone else here when dealing with Jar-files)
Ir is marked in "Project References" on Project settings.
I added the folder where the relevant class is under "Libraries" in build Path... I am not sure if that should be nessecary.
The Java-Project I reference has a whole load of Jars, but if that is the reason, should I not get a different errormessage?
I had the same issue and after some hours of frustration and search I have finally found the answer here:
Android, class not found from imported jar file
Basically, the issue was that the referenced pure-java project or the generated jar was built with Java 1.7, and Android projects are set to use 1.6.
Two ways to do this.
Jar that java project and copy the jar to the libs folder of your Android project.
Add the project as a dependent project to your Android project
Both work wonderfully.
But mind you, this pure-java project must add android.jar and not things like rt.jar :). Else you will get Dalvik exceptions.
Edit :
Dont forget to refresh and Project - Clean your android project.
Two things to check (you mentioned you are using Eclipse):
If your android project needs to use the pure-java project's JARs, check that those JARs are being exported in the build path options of the pure-java project. This is in the build path of the pure-java project you are trying to include, not the build path of the android project that is trying to include it. Under "Order and Export" tab for the pure-java project, note the comment "Exported entries are contributed to dependent projects". In this case, your android project is the dependent project and your pure-java project needs to do the contributing.
Make sure the pure-java project is actually opened in the IDE in addition to your android project. Probably trivial but sometimes overlooked.
If answer does not suffice, you may wish to mention if the missing classes are in .java source files, or if they are only found in JARs in the build path of the pure-java project
Edit: To further track down the issue, please confirm the following to make sure I understood your question:
The pure-java project does successfully build in the IDE
The 'class not found' error is one you are getting when you try and -build- your android project and not when you try and -run- it.
CLASSPATH (aka build path) can be nasty to untangle. If including the project is not working (assuming it does build), you could try building a JAR of your pure-java project and copying (and including) that and all the other JARs into your android project.
The android SDK does certainly complicate the build environment. One way to find out whether the problem is (A) your android project setup) OR (B) the way the pure-java project is packaged) is to create a different pure java project and try including the first one, preferably using the -exact- same line of code that gives you the build error in the android project, if possible.
I'm new to android programming. I'm trying to include code from a non-android project in an android app. This contains shared code used by a lot of my other non-android apps.
I was wondering what the best way to do this is from a code maintenance perspective. The shared code that I want to include does get modified from time to time and I want to keep the process of updating any apps I write as simple as possible (automatic if at all possible).
Am I better to build a .jar file containing the shared code and copy this to my app (eg: using an ant script) or is there a more streamlined approach.
I specifically want to avoid turning my shared code project into any kind of android project.
Make a jar and add it to the android project's build path. Whenever there is an update to jar, you would need to update it in your project and update (increase) version of your app to automatically allow users to download and update the update (android market would take care of that for you).
Its the most widely and maintainable way.
Hope this helps.
Once you have your .jar, you simply have to add it to your project (or update with the new .jar file if this library gets updated).
Once it is in you project (let's say under the /lib folder,
right click on the lib folder -> select build -> add to build path.
Unfortunately, I'm pretty sure there's no way to make an automatic update.
In the projects' properties go to "Project References". It will should you the other open projects and you can click whichever you want.
I haven't tried it, but if you change the non-Android code and run the Android project, I would expect the non-Android project to be recompiled automatically.