I added a jar file in my android application. The file is compiled well and working well.
However the jar file contains Json classes under the org.jason package, and it's the same Json package name provided with Android.
When I do an import, the system always choose the Json of Android package and me, I want the Json added with the jar file.
Is there a way to decide which package to import? Either the package provided with Android or the package added with the JAR?
You need to use exclude while importing the library.
compile (){
exclude group: 'x', module: 'y' //by both name and group
}
Related
I'm searching a solution for adding my gradle dependencies in my built jar file.
For example, I've the dependency com.fazecast:jSerialComm and wanting to add the library in specific folder in the jar, library path should belibs/jserialcomm/jSerialComm-2.0.2.jar
Add the following to your build.gradle:
jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
This will merge the content of all dependencies into the output jar.
See also http://www.baeldung.com/gradle-fat-jar.
In a different approach, one would copy the jars of the dependencies into the output jar (jars in jar), but that also requires writing a custom class loader.
UPDATE after edit of question:
The OP wants to take the second approach, which is employed as follows:
plugins {
id "com.github.onslip.gradle-one-jar" version "1.0.5"
}
task awesomeFunJar(type: OneJar) {
mainClass = 'com.github.rholder.awesome.MyAwesomeMain'
}
This will include all dependent jars into a lib directory in the output jar. It will also install a custom class loader, which loads jars from the lib directory in the jar. This is something that the standard class loader does not do, no matter how you tweak the class path.
See also https://github.com/Onslip/gradle-one-jar/
Hello StackOverflow folks,
I have a jar file of java classes. I added this jar file to my android studio project under folder /libs. Now, what I want to do is use those classes within the jar file in MainActiviy.java. I just do not know how.
Some details:
My jar file is named: zombi.jar.
The class within the jar file to call is named: COMBI.class
I tried the following:
In MainActivity.java, I wrote:
// declared class variable
private COMBI mCOMBI;
Then, in OnCreate method, I wrote:
mCOMBI = new COMBI();
//to start calling method COMBIStart to launch the command-line system
mCOMBI.COMBIStart();
I actually called the classes as I would in normal Java. I think Android uses special java code that looks like java, but I don't know how to use them.
I could not get the code to work.
Can you help me?
I'm going to assume you've already written your import statement for the jar class. If you already put the jar file in the /lib folder, Android Studio should update your build.gradle file. Check to see if you have a link to the jar file in your dependencies{...}
If not, you can add it manually.
Just like in normal java you have to add an import to the correct package at the top of your file for everything that's not in the same package the file is in.
It should look something like this:
import android.view.View;
but instead of android.view.view it would be a reference to the class in the jar you're trying to add
I've been googling a lot for this and it seems easy, but it just doesn't work for me.
I want to include Twitter4j in my JAR in IntelliJ IDEA. I have put the twitter4j JAR into my /lib folder , specified it in Libraries,
specified it to compile in dependencies:
Dependencies
and specified to include it in artifacts.
In the final JAR, both twitter4j-stream and twitter4j-core are included:
Final JAR
Still, Java throws a NoClassDefFoundError when run.
(This is not Maven.)
Answer given by author of the question:
In the Artifacts, you have to choose "Extracted *.jar" instead of the
library.
I had similar error and the problem was that I did not write correctly the title in the command line: java title. Be sure that your title is equal to the program name written inside your program before it has been packed.
In public class title the title must be equal to the program name as well as to the name of jar archive.
(Obviously you can not use data files inserted in the JAR file with the CLASS file until the JAR file is unpacked...)
is it possible to use a Android Studio Library (aar file) in a Qt app?
The problem is, that I want to implement a mobile App with Qt, but there is only a library for Android Studio. Is it possible to include the library in the Qt project or have I to write a wrapper class for it?
If I have to implement a wrapper, do I have to use the JNI and are there any examples for using it with C++ and a Java lib?
I found the answer, that worked for me.
First you have to unzip the aar file, so you can get your jar library file.
Then you can follow the instructions in this link to include the library to your apk:
http://doc.qt.io/qt-5/android3rdpartylibs.html
When you have finished this, you have to implement your own Java wrapper class to interact with the library. Therfore you have to use the QAndroidJniObject class from Qt. Here are more information about it.
http://doc.qt.io/qt-5/qandroidjniobject.html
You could also have a look at this example, where they use their own java class. http://doc.qt.io/qt-5/qtandroidextras-notification-example.html
You can add .aar files directly to your project
In gradle file add .aar folder to dependencies
compile project(':myAarFolder1.1')
and include it in gradle.settings
include ':myAarFolder1.1'
the aar file location in my project is like this
android/myAarFolder1.1/.aar
android/myAarFolder1.1/build.gradle
android/myAarFolder1.1/build.gradle file content
configurations.maybeCreate("default")
artifacts.add("default", file('myAarFolder1.1.aar'))
Ok incase anyone else comes across this I figured it out.
If you don't already have an android source folder add one by inserting this into your .pro file
android {
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
}
Here is an example file structure:
project/android-sources
project/android-sources/settings.gradle
project/android-sources/build.gradle
project/android-sources/LibraryFolder
project/android-sources/LibraryFolder/Library.aar
project/android-sources/LibraryFolder/build.gradle
You might have to get your build.gradle file from ../build-Android/android-build/build.gradle and copy it into project/android-sources/ if it doesn't already exist.
Insert this into your build.gradle file within dependencies { }
api project(':LibraryFolder')
Insert this into your settings.gradle file at the bottom
include ':LibraryFolder'
Create the build.gradle file inside LibraryFolder and insert:
configurations.maybeCreate("default")
artifacts.add("default", file('Library.aar'))
and lastly if the library you are adding has other 3rd party dependencies you will have to add them to project/android-sources/build.gradle
For example I was adding ImagePicker and had to insert the below lines inside dependencies { } but above api project(':LibraryFolder')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
from the library's source
I have a default Libgdx Gradle setup, and I need to add my simple text rendering library to it. It consists of a .jar file and native lib file.
This line of build.gradle script seems to work as I would expect, and what it does is add jfreetype.jar java library to my build path.
compile files('../local_lib/jfreetype.jar')
Is there a magic command like this to add native library (.dll to be exact) that is available on my file system and is not Mavenized?
natives "../local_lib/jfreetype32.dll"
This line of code just gives me an error saying that something cannot be found at some repo. I guess there should be a magical line like with .jar file to add native files that are available only on my file system and not on some repo.
The Gradle Natives plugin should do what you want.
You can specify a configuration that points at jar files that contain native dll/so. A gradle task "unpackNatives" will then unpack the dll/so into the build dirs.
Depending upon how you launch your application, you may still need to tell the Java runtime where to find the dll/so. There is some info about how this works at the project website:
https://github.com/cjstehno/gradle-natives
You can add a flat directory as a repository in this way, as mentioned in the dependency-management section in the Gradle User Guide.
repositories {
flatDir {
dirs '../local_lib'
}
}
If you want to create your own dependency-configuration natives, create it like this (more info on the same page):
configurations {
natives
}
Hope that helps.