I want import .arr in my library module not main module,how can i do?
in my library module build.gradle set aar, err: can not find it.
dependencies {
compile(name:'nameArrFile', ext:'aar')
}
repositories{
flatDir{
dirs 'libs'
}
}
To Add to the answer: From the Android Studio File Menu,
Click Import .JAR/.AAR Package then click Next.
Enter the location of the compiled AAR or JAR file then click Finish.
Import the library module to your project
(the library source becomes part of your project):
Click File > New > Import Module.
Enter the location of the library module directory then click Finish.
The library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
Make sure the library is listed at the top of your settings.gradle file, as shown here for a library named "my-library-module":
include ':app', ':my-library-module'
Open the app module's build.gradle file and add a new line to the dependencies block as shown in the following snippet:
dependencies {
compile project(":my-library-module")
}
Click Sync Project with Gradle Files.
In this example above, the compile configuration adds the library named my-library-module as a build dependency for the entire app module.
If you instead want the library only for a specific build variant, then instead of compile, use buildVariantNameCompile. For example, if you want to include the library only in your "pro" product flavor, it looks like this:
Your code is correct. Make sure the following things are in place and it will work just fine.:
nameAarFile should be just the file name without the extension .aar. Also make sure there is no spelling error in the file name.
make sure the extension of the actual aar file is right. I see you have mistyped it as arr at several places in your question (I corrected). make sure the same is no the case with extension in your file name
Make sure the .aar file is in the libs folder. If in any other directory either move to libs or include the other directory under FlatDir - > dirs
Related
I am trying to add this library to my project because I want to change something in a class there. That is why I adding it as a dependencies in my gradile setting is not enough.
I know that in the library it says "If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.". But what does that mean, should I copy all files and put them in my libs folder ?
Quoting the documentation:
If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.
Step #1: Download the project, whether via the ZIP file that GitHub gives you or by using git to clone the repository
Step #2: Move the MaterialDesign/ directory into your project directory, as a peer of your existing app/ module
Step #3: Modify your settings.gradle in your project directory to include :MaterialDesign in the list of modules to build
Step #4: In your app module's build.gradle file, add compile project(':MaterialDesign') to your dependencies
As Gradle does not support apklib dependencies how can one migrate from apklib dependencies to aar dependencies? Is it possible to either manually or automatically convert apklib dependency to aar? If yes - how, if no - why not?
In this question I assume that I don't have original project for apklib, but rather the file itself.
apklib doesn't contain object files, only source files, so the only way it can be included in any project is by recompiling it. According to some docs you can read at:
Android dependencies : apklib vs aar files and https://plus.google.com/+ChristopherBroadfoot/posts/7uyipf8DTau
The apklib format was invented as a way to share Android code+resources. It’s essentially a zipped up Android library project, which was already the status quo for code+resource sharing.
And you can see in Chris Broadfoot's post that the task that generates the apklib just zips up the AndroidManifest.xml file and the src and res directories:
task apklib(type: Zip) {
appendix = extension = 'apklib'
from 'AndroidManifest.xml'
into('res') {
from 'res'
}
into('src') {
from 'src'
}
}
Note that his post is about creating an apklib from Gradle, which is a slightly weird thing to want to do, but it provides a pretty clear idea of how one of these things is structured.
The good news is that this is "standard" Android project structure as of when Eclipse was the primary IDE, and Android Studio knows how to import modules that look like this. So follow these steps:
Unpack your apklib into a temporary directory. It's in zip format, so something like unzip library.apklib should work.
Look at what's unpacked. You should see a directory containing AndroidManifest.xml, src, and res folders.
In Android Studio, inside an existing project, choose File > Import Module and point it at the directory from step 2.
Android Studio should manage the import by copying the files into your project in a new module, arranging them in its preferred directory structure, and will add a build.gradle file for the module.
At this point you're up and running. If you actually want the .aar file, then check to see if your new module is set up as a library module or an application module. In its build.gradle, if you've got apply plugin: 'com.android.library' then it's a library module, and Gradle will generate an .aar as part of the build: it will be in the build/outputs/aar directory after a successful build.
If your module imported as an app module and not a library (apply plugin: 'com.android.application', then change that apply plugin statement, and you may also need to remove the applicationId statement from the build file. Now it should compile as a library and generate a .aar.
Happy coding!
You cannot convert the apklib to aar. You have to update the dependencies manually to point to an aar file. The aar is compiled and contain lint and proguard rules which the apklib can't necessarily determine automatically.
You can read a bit more on the differences here:
Android dependencies : apklib vs aar files
Question as in title. A similar question was asked here, and the only workaround that time was to publish the project into a local Maven repository.
Is this problem fixed (as claimed by some) in Android Studio 0.5.+? In its release note there is a statement that says "Support for source folders outside the module content root". Does that mean we can finally import the library from outside the project folder?
I tried File->Import Project.. but it doesn't work.
EDIT 2: See accepted answer for latest solution (as of 0.8.+)
EDIT:
My project directory structure has only one module main which looks like this
MyApp
main
build.gradle
src
build.gradle
settings.gradle
The library project directory has only one module named like lib (they are all auto-generated when creating a new library project)
MyLibrary
lib
build.gradle
src
build.gradle
settings.gradle
The following line is added into MyApp/settings.gradle:
include ':main', '..:MyLibrary'
The following is added into MyApp/main/build.gradle:
dependencies {
compile project(':..:MyLibrary')
}
The result is the following error:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':main'.
> Configuration with name 'default' not found.
As a side note, the library project MyLibrary itself can compile without error. The problem seems to be settings.gradle not being able to recognize the library project structure.
(as of version 2.1+):
Below are the steps that I took to share library source outside of the project directory. Instead of plain Java library, my code is compiled as Android module, but that library module is not contained inside the main project. It is fine with me as long as there are no code duplications and I only need to maintain one set of library code for all projects:
File->new Project. Give a name to your library project (here I use LibraryProject). Continue the remaining steps to create a normal project (since this is intended as a library, I chose "add no activity")
By default, Android studio creates the module named as "app" inside the project folder. To prevent names collision with the actual application module, rename the module to something else (Right click "app" module at left panel->Refactor->Rename).
In the build.gradle inside your library module folder, change the top line
apply plugin: 'com.android.application'
to
apply plugin: 'com.android.library'
and then remove the "applicationId" line under "defaultConfig". Also, since this is a library, remove the xlmns:... namespace and <application ..> body from Manifest.xml as well.
That's all for the library part. Now, to create/modify your main application:
If it is a new project, first create new project File->new Project->ApplicationName.
Open settings.gradle (there should only be one such file in every project) and include the following line (note the missing leading semi-colon in library module):
include ':app', '..:LibraryProject:yourLibraryModule'
Then go to File->Project Structure.
Under the tab "Dependencies" click the green "+" button at right. Select "Module dependency". Choose your library module, then click OK.
You should now be able to use the library classes in your application.
ALTERNATIVE METHOD
If, for some reason, there are still problems with the above method, you can try the following (suggested in [here][1]):
Repeat steps 1 to 4 above. By default the main and external (library) project look something like this:
/MainProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/
/LibraryProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/
As usual, refactor the modules name (in android studio right-click module->refactor->rename) to something less confusing, such as
/MainProject
+ build.gradle
+ settings.gradle
+ MainModule/
+ build.gradle
+ src/
/LibraryProject
+ build.gradle
+ settings.gradle
+ LibraryModule/
+ build.gradle
+ src/
Modify the settings.gradle in MainProject:
include ':LibraryModule', ':app'
project(':LibraryModule').projectDir = new File(settingsDir, '../LibraryProject/LibraryModule')
Sync the project and you're done.
Note on Proguard
Currently you should not use a proguard on external library projects/modules. Instead, you replace the following (original answer [here][2])
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
debug {
minifyEnabled false
}
}
with the following (in build.gradle of the library):
buildTypes {
release {
consumerProguardFiles 'proguard-project.txt'
}
}
where proguard-project.txt is the file that contains the proguard rules for your library project.
[1]: https://stackoverflow.com/a/17490233/1919013
[2]: https://stackoverflow.com/a/31031491
Yes, it works now in 0.5.0. There isn't a friendly UI to it (Import Project isn't what you want, as that creates an entirely new project; Import Module is still broken; see https://code.google.com/p/android/issues/detail?id=62122), but you can set up your build files to make it work.
Let's say you have a directory structure that looks like this:
MyApp
appmodule
build.gradle
src
main
java
build.gradle
settings.gradle
MyPlainJavaLibrary
build.gradle
src
java
Note that MyPlainJavaLibrary isn't underneath the MyApp directory in the filesystem.
Set up your settings.gradle file like so:
include ':appmodule', '..:MyPlainJavaLibrary'
and include a dependency to it in build.gradle like this (don't forget the leading colon):
dependencies {
...
compile project(':..:MyPlainJavaLibrary')
}
This works for me. In my Project viewer, I now see MyApp and MyPlainJavaLibrary as two root-level module directories in the view, and I can make java import statements work across module boundaries and such.
Note that in this default setup, this shared module will only have a single build output folder that will be shared among all the projects using it. This may or may not be what you want. If you'd like to have a different output directory for each project using the shared module, you can probably do some magic with the output directory in the sourceSet; if you want help with this, I'd recommend trying it out on your own and posting a question with details on your work if you get stuck.
You can only have one settings.gradle file per project, so this isn't going to take a whole other multimodule Gradle project and bring in all the modules in one fell swoop; you'll need to bring them in individually. However, it should satisfy the use case of using a module in multiple projects.
I wanted to do something similar for my Android Wear project, where you have mobile and wear modules. In Android Studio 1.5.1, you can do File->New->Module and pick Android Library as your module. This will take care of many of the settings described above and put your library inside your existing Android Wear project.
To make full use of Android Studio, I would recommend initially creating a normal android project. Then, within that project, add a Module "android library" type, and (optionally) a "java library" (if you have code you want to use that may be common to Android applications and to server side code).
Using this mechanism, you do not need to manipulate and modify gradle and manifest files.
I also wanted to use the same collection of classes across projects, I found an easier way.
Put the class files you want to use in their own folder outside all of your project folders.
Make a symlink(linux) or shortcut(win) to this folder and copy it to:
~/src/main/java/com/yourco/yourproject/
in each of your project folders.
You now have a package of all your commonly used classes synced on all your projects. You're welcome.
You are able to use module(as a shared source code base)apply plugin: 'com.android.library' in the project.
[Project vs Module]
I want to add library, android pdf writer to my project to create pdf documents in the app.
Below link has some files associated with the library.
http://sourceforge.net/p/apwlibrary/code/HEAD/tree/
I would like to know,
1. How do I access/add these files to my project?
2. Since I only want to generate a pdf document there is a file in there called PDFWriterDemo.java, could I just add that to my project or do I have to add all the files in the library?
Sorry I couldn't find the documentation on how to add them to project to use these files.
Thanks
There is no jar file. You have to embed source files directly.
You should include all the source files except for:
APW.pdf
PDFWriterDemo.java
assets.zip
helloworld.pdf
README.txt
You don't need any additional jar file.
I write detailed instruction here: http://karino2.livejournal.com/293016.html
(Using AS 1.2 Beta at time of writing)
You first need to download the source code since there is no jar. To do that:
Go to the link that you provided and click on "Download Snapshot".
Unzip the file you just downloaded into a new folder
Remove all of the files that are not Java files (they aren't necessary)
Then, to add it to your project, it depends on if you are using Gradle.
With Gradle
In Android Studio, click on Project Settings
Click on the plus sign to add a new module
Click on Android Library
Choose whatever name you want for the application and module name (AndroidPDFWriter is pretty consistent) but make sure that the package name matches the library's package name (crl.android.pdfwriter)
Choose No Activity on the next page and press finish to create the module.
Copy the sources from the folder with the source code into that new module (make sure to copy it in the right directory, i.e. src/main/java/crl.android.pdfwriter)
From there, an Android module is created with all of the necessary resources. I chose to delete all of the extra file that AS creates for you but that's your call. Make sure to keep the manifest (I emptied it though to just have the Manifest tag since nothing else was useful). You can then declare the dependency on this module from your main one like you would normally.
Without Gradle
In Android Studio, click on Project Settings
On the modules page, click on the plus sign to add a new module
Click on Import Module
Choose the folder with all of the sources that you had unzipped.
Go through the following pages of the import module process normally.
From there, the Android module is created (make sure that the Module SDK is an Android one) and you can add a dependency the normal way as well.
This isn't the best way to do things, but I see no alternative for this particular library. Hope this helps !
Get the jar file and add to your project. Just adding PDFWriterDemo.java will not work as this class has dependency on other classes like Page and you might have to include all of them.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/xyz.jar')
}
Assuming you are using Android Studio, follow these steps to include .jar into project:
Get the jar file.
Put the jar into the libs folder
Right click it and hit 'Add as library'
Ensure that compile files is in your build.gradle file, in the dependencies add the below.
.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/xyz.jar') //xyz.jar is the filename of your jar
}
Question as in title. A similar question was asked here, and the only workaround that time was to publish the project into a local Maven repository.
Is this problem fixed (as claimed by some) in Android Studio 0.5.+? In its release note there is a statement that says "Support for source folders outside the module content root". Does that mean we can finally import the library from outside the project folder?
I tried File->Import Project.. but it doesn't work.
EDIT 2: See accepted answer for latest solution (as of 0.8.+)
EDIT:
My project directory structure has only one module main which looks like this
MyApp
main
build.gradle
src
build.gradle
settings.gradle
The library project directory has only one module named like lib (they are all auto-generated when creating a new library project)
MyLibrary
lib
build.gradle
src
build.gradle
settings.gradle
The following line is added into MyApp/settings.gradle:
include ':main', '..:MyLibrary'
The following is added into MyApp/main/build.gradle:
dependencies {
compile project(':..:MyLibrary')
}
The result is the following error:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':main'.
> Configuration with name 'default' not found.
As a side note, the library project MyLibrary itself can compile without error. The problem seems to be settings.gradle not being able to recognize the library project structure.
(as of version 2.1+):
Below are the steps that I took to share library source outside of the project directory. Instead of plain Java library, my code is compiled as Android module, but that library module is not contained inside the main project. It is fine with me as long as there are no code duplications and I only need to maintain one set of library code for all projects:
File->new Project. Give a name to your library project (here I use LibraryProject). Continue the remaining steps to create a normal project (since this is intended as a library, I chose "add no activity")
By default, Android studio creates the module named as "app" inside the project folder. To prevent names collision with the actual application module, rename the module to something else (Right click "app" module at left panel->Refactor->Rename).
In the build.gradle inside your library module folder, change the top line
apply plugin: 'com.android.application'
to
apply plugin: 'com.android.library'
and then remove the "applicationId" line under "defaultConfig". Also, since this is a library, remove the xlmns:... namespace and <application ..> body from Manifest.xml as well.
That's all for the library part. Now, to create/modify your main application:
If it is a new project, first create new project File->new Project->ApplicationName.
Open settings.gradle (there should only be one such file in every project) and include the following line (note the missing leading semi-colon in library module):
include ':app', '..:LibraryProject:yourLibraryModule'
Then go to File->Project Structure.
Under the tab "Dependencies" click the green "+" button at right. Select "Module dependency". Choose your library module, then click OK.
You should now be able to use the library classes in your application.
ALTERNATIVE METHOD
If, for some reason, there are still problems with the above method, you can try the following (suggested in [here][1]):
Repeat steps 1 to 4 above. By default the main and external (library) project look something like this:
/MainProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/
/LibraryProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/
As usual, refactor the modules name (in android studio right-click module->refactor->rename) to something less confusing, such as
/MainProject
+ build.gradle
+ settings.gradle
+ MainModule/
+ build.gradle
+ src/
/LibraryProject
+ build.gradle
+ settings.gradle
+ LibraryModule/
+ build.gradle
+ src/
Modify the settings.gradle in MainProject:
include ':LibraryModule', ':app'
project(':LibraryModule').projectDir = new File(settingsDir, '../LibraryProject/LibraryModule')
Sync the project and you're done.
Note on Proguard
Currently you should not use a proguard on external library projects/modules. Instead, you replace the following (original answer [here][2])
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
debug {
minifyEnabled false
}
}
with the following (in build.gradle of the library):
buildTypes {
release {
consumerProguardFiles 'proguard-project.txt'
}
}
where proguard-project.txt is the file that contains the proguard rules for your library project.
[1]: https://stackoverflow.com/a/17490233/1919013
[2]: https://stackoverflow.com/a/31031491
Yes, it works now in 0.5.0. There isn't a friendly UI to it (Import Project isn't what you want, as that creates an entirely new project; Import Module is still broken; see https://code.google.com/p/android/issues/detail?id=62122), but you can set up your build files to make it work.
Let's say you have a directory structure that looks like this:
MyApp
appmodule
build.gradle
src
main
java
build.gradle
settings.gradle
MyPlainJavaLibrary
build.gradle
src
java
Note that MyPlainJavaLibrary isn't underneath the MyApp directory in the filesystem.
Set up your settings.gradle file like so:
include ':appmodule', '..:MyPlainJavaLibrary'
and include a dependency to it in build.gradle like this (don't forget the leading colon):
dependencies {
...
compile project(':..:MyPlainJavaLibrary')
}
This works for me. In my Project viewer, I now see MyApp and MyPlainJavaLibrary as two root-level module directories in the view, and I can make java import statements work across module boundaries and such.
Note that in this default setup, this shared module will only have a single build output folder that will be shared among all the projects using it. This may or may not be what you want. If you'd like to have a different output directory for each project using the shared module, you can probably do some magic with the output directory in the sourceSet; if you want help with this, I'd recommend trying it out on your own and posting a question with details on your work if you get stuck.
You can only have one settings.gradle file per project, so this isn't going to take a whole other multimodule Gradle project and bring in all the modules in one fell swoop; you'll need to bring them in individually. However, it should satisfy the use case of using a module in multiple projects.
I wanted to do something similar for my Android Wear project, where you have mobile and wear modules. In Android Studio 1.5.1, you can do File->New->Module and pick Android Library as your module. This will take care of many of the settings described above and put your library inside your existing Android Wear project.
To make full use of Android Studio, I would recommend initially creating a normal android project. Then, within that project, add a Module "android library" type, and (optionally) a "java library" (if you have code you want to use that may be common to Android applications and to server side code).
Using this mechanism, you do not need to manipulate and modify gradle and manifest files.
I also wanted to use the same collection of classes across projects, I found an easier way.
Put the class files you want to use in their own folder outside all of your project folders.
Make a symlink(linux) or shortcut(win) to this folder and copy it to:
~/src/main/java/com/yourco/yourproject/
in each of your project folders.
You now have a package of all your commonly used classes synced on all your projects. You're welcome.
You are able to use module(as a shared source code base)apply plugin: 'com.android.library' in the project.
[Project vs Module]