I built one android studio project. I wanted to use it as a library in another project. I followed below steps for same:
Changed apply plugin: 'com.android.application' to 'com.android.library'
Removed applicationId from defaultConfig{}
Clicked on sync
This gave me .aar file in build-->outputs-->aar-->myapp.aar
I imported this .aar file in my other test project. I followed below steps for this:
File-->New-->New module-->Import .jar/.aar package-->myapp.aar
In build.grade of test application added compile project(path: ':myapp')
Clicked on sync.
This produced one folder inside external libraries called myapp-unspecified.
There i get all res files but i did not get the class files inside classes.jar.
Inside classes.jar I only have MANIFEST.MF.
Am I doing something wrong or am I missing something??
Experienced the same problem. My issue was that I had removed a dependency from my library project (in this case, testCompile 'junit:junit:4.12'), but did not delete the directory and classes that referenced the APIs from that dependency. An aar file was output to the build folder, but I was not able to access its contents when I included it in my application project.
After deleting the unused classes and directories and re-syncing my library project, I was able to produce a working aar.
Ran into the same issue. The classes are actually there or at least were for me. All that I needed to do was right click the application I was importing my library into. Click open module settings. Then go to dependencies on the right side. Add your library there as well so that the java will compile will your application.
Related
I am trying to create a Java library in Android Studio. I want to use functionality of another Java library (third party available as a .jar) in it. So I placed the library in libs folder of my java library project and added the following code in build.gradle file:
apply plugin: 'java-library'
dependencies {
implementation files('libs/libraryname.jar')
}
The .jar reference was added successfully and I can use the library methods in my code. My project also compiles successfully and I am able to generate a .jar file of my own java library project.
However, when I am using the generated .jar file in another project and try to access the methods of third party library, I get the following exception:
java.lang.ClassNotFoundException: Didn't find class
"com.packagename.classname" on path: DexPathList[[zip file
"/data/app/com.packagename-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64,
/system/lib64]]
What I have tried
I have tried multiple solutions provided here and here but none of them works.
I have tried to clean and re-build the project and .jar files as well but that doesn't help as well.
I have added the .jar dependency as a module before referring it in my project but it still generates the same error.
Note:
I am using Android Studio 3.1.4
Copy .jar in your libs project folder: change the navigator to project view, than you are able to see libs directory.
Right click on your jar folder inside your project and select add as library, this will automatically import the library on your gradle dependencies
I have an Android application (running on Android Studio). It is made of 2 modules:
- There is a low level pure java module (let’s call it module A).
- On top of it, there is the module B which is the Android application. It relies on moduleA for some processings.
This is working fine.
I now need the module A to call an external Library. I have downloaded the JAR file and put it in moduleA/libs folder.
This libs folder is referenced in gradle dependency of moduleA so the compilation is OK. However, there is an exception at runtime:
FATAL EXCEPTION Caused by: java.lang.ClassNotFoundException: Didn't find class "XXXX" on path: DexPathList
I have seen that the APK doesn’t contain the JAR file so it is normal that this exception occurs.
If I copy the same JAR file in moduleB/libs, then it works fine but I have the JAR file two times in the project! What is the clean solution to handle this?
I guess that it can be solved with gradle but I don't know how.
Thank you very much
Olivier
I have been able to fix this issue. This reading about Gradle helped me a lot:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Creating-a-Library-Project
Here is what I have done:
Instead of putting the JAR file in moduleA/libs folder, I have imported the JAR file in Android Studio by clicking on the project then right click -> new -> module. I then clicked on “Import .JAR/.AAR package”.
This created a module containing the JAR file + a gradle script.
Then, in moduleA’s gradle script, I have added this in the dependencies:
compile project(path: ':name_of_the_jar_file')
I rebuilt all and it works. The JAR file is now present in the APK and there is no more crash at runtime.
I have a jar file called PhoneGestures.jar which contains a bunch of java files I want to use in my project, which I have added to my project as a module using New->Module->Import .Jar :
I then added this jar module to my project dependencies like so:
and as you can see it is included in my project's build.gradle:
But now when I go to use one of the classes from my jar file, like "MobXRotate", for example, Android Studio does not recognise it:
Does anyone have any ideas why this might not be working? Thanks.
Make sure you have imported the class if the class isn't been found either,
try build your project first, that gradle copies the source code in your project structure!
There's no need to create a new module for a jar file.
Putting the jar file in libs folder will do the trick.
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
When using Unity3d, "native" (pure-java) Android functionality can be accessed via Plugin. As described in the documentation,
There are several ways to create a Java plugin but the result in each case is that you end up with a .jar file containing the .class files for your plugin
When I create an Android Application Project in eclipse, and set it is a library, it automatically generates a .jar file in the bin folder. I understand that this is a temporary file in the context of the Android toolchain, but this is the file that Unity needs in order to see the classes and build up it's own internal JNI magic. (The entire process is outlined very well here in case more clarity is needed on that process.)
Unfortunately, as near as I can tell, Android Studio does not generate this file. Is there a way I can tell it to give me this file, or perhaps some other way to generate code that Unity will be able to use?
With Unity 5.3+ you no longer need to build a .jar, instead you can use the .aar file that is automatically created by an Android Studio Android Library module. I found this a lot easier than getting the jar method to work:
In your Android Studio project, create an Android Library module: File > New > New Module > Android Library
make sure your minSdkVersion and targetSdkVersion match your Unity project setup
Add your classes etc to the module and build
A successful build should create a .aar file in build/outputs/aar/ e.g. my module creates mkunityplugin-release.aar
Open your Unity project, then drag the .aar file directly onto Assets/Plugins/Android in your Project view. This creates an asset which you can view in the inspector; it will show info on the plugin and the Android platform should be checked
Build your Unity project for Android. My first attempt failed because I had a different targetSdkVersion so I got a build error saying the manifests could not be merged; I fixed that in my Android Studio build.gradle and then it worked
This is where I first heard that .aar could be used directly https://www.reddit.com/r/Unity3D/comments/3eltjz/how_to_add_an_android_library_project_folder_to/
Yes, you can use Android Studio to generate your jars from a Library.
In my case the jar file was located very well hidden in the app build folder, under
<ProjectRootDir>/<AppModuleName>/build/exploded-aar/<ProjectName>/<LibraryModuleName>/unspecified/classes.jar
My example setup for this:
A app module containing nothing but a minimal Android App (Including a MainActivity)
A library module that contains the library code
Add a dependency from your app to your library e.g. add: compile project(':library')
If you build your app now it will also build the library and your apps build folder will contain the jar file under the location I explained above (in my case: PROJECT_ROOT/app/build/exploded-aar/SampleLibrary/library/unspecified/classes.jar).
You can create a jar file using Android Studio.
Just add these tasks in your app's build.gradle file.
//task to delete the old jar
task deleteOldJar(type: Delete) {
delete 'build/libs/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
///Give whatever name you want to give
rename('classes.jar', 'AndroidPlugin.jar')
}
exportJar.dependsOn(deleteOldJar, build)
Once you add the above lines you can export the jar using the task of the same name.
You can go through this post on TGC here on how to create an Android Plugin For Unity using Android Studio
Simple way:
Create new Android Studio Project
File > New > New Module and select Android Library
Copy <UnityPath>\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar to <project path>\<Android Library Module name>\compileOnly (create this folder before)
Add this jar as Library
Open this Module Settings
Set Compile Only
Build > Make Module <Android Library Module name>
Result
Jar-path: <project path>\<Android Library Module name>\build\intermediates\intermediate-jars\debug\classes.jar
Aar-path: <project path>\<Android Library Module name>\build\outputs\aar
Change apply plugin: 'com.android.application' to apply plugin: 'com.android.library'. In the build.gradle under app.
I'm not really sure about Android Studio but in ADT I use the export functionality by right-clicking on the project. With this you can create your .jar and place it wherever you want. Unity needs it to be on a specific folder to see it when running on a device (unfortunately I don't remember it well, but it should be something like Assets/Plugins/Android).