org.jose4j.json.internal.json_simple can not be found - java

I have created an Android project and added an external JAR on IntelliJ.
I have already installed "json-simple-1.1.jar" to use package org.jose4j.json.internal.json_simple. I did a library setting to use that from the project structure menu.
But when I run the program, error message says,
"error: package org.jose4j.json.internal.json_simple does not exist
import org.jose4j.json.internal.json_simple.JSONArray;".
(Not only JSONArray, but the other JSON such as JSONObject)
It worked when I used that outside of the Android project.
Are there any special settings for the Android project?
So far, I stored the jar file under the project folder by making "lib" folder additionally.

In app/build.gradle dependencies section add the following:
compile group: 'org.bitbucket.b_c', name: 'jose4j', version: '0.7.0'
Reimport/refresh the project in IntelliJ IDEA.

Related

Jar Lib on Android Project And Android APP

I create a Jar Lib called "errors" in Java.
I would like to use it in a module called "AndroidHTTPRequestLibrairy" and in my Android App.
But i have a error :
Program type already present:
In my module (AndroidHTTPRequestLibrairy) build.gradle i have :
implementation files('libs/errors-0.1.3-SNAPSHOT.jar')
And in my app I have :
implementation project(path: ':androidhttprequestlibrairy')
and
implementation files('libs/errors-0.1.3-SNAPSHOT.jar')
I dont understand why.
Can someone help me ?
Sincerely,
This issue appears because of namespace conflict.
You are trying to put one library twice in one project. When the Gradle build your project and at the moment when it tries to put in your app.apk archive the errors-0.1.3-SNAPSHOT.jar library it finds out that there is already the errors-0.1.3-SNAPSHOT.jar library and throws an exception.
So the solutions is to remove the implementation files('libs/errors-0.1.3-SNAPSHOT.jar') dependency form the application build.gradle file, because it allready transited in the implementation project(path: ':androidhttprequestlibrairy') dependency

How to access aar file, in libs, from Main

I spent the last three days trying to read an .aar in Android Studio and I just can't figure it out.
My project view looks like this:
--- AnApplication
----------------------.gradle
-----------------------.idea
----------------------- app
-------------------------------- build
-------------------------------- libs
---------------------------------------- cube5-debug.aar
(...)
I want to access a class inside cube5-debug.aar from my main activity.
I know the class is there in the .aar file.
I don't know the precise import line I must write at the start of my main.
I have tried import com.company_cube.cube5 (the package of the project where I generated my .aar) and many other variants.
I don't know if Android Studio is recognizing the file as a library. However, the files sync with the gradle successfully.
(For context, the project where I generated the .aar was made by Unity, by exporting a Unity project into an Android one, and then generating the .aar from that one; the goal was to use this .aar file as a library in a second Android Studio project - the one I am talking about in this post)
I am patiently waiting for any help you can give me. Thanks
EDIT: To import the .aar, I followed the images of this small post:
Importing .aar in android Studio
And solved the error the post reports by putting the .aar in the libs folder like Daniel Nugent said. However, when I try to write, in the Main:
Intent intent = new Intent(getApplicationContext(),UnityPlayerActivity.class);
AS doesn't recognize UnitPlayerActivity. This class is inside the .aar file supposedly. Before making the aar in my first AS project, it was here:
cube5->libs->unity-classes.jar->com.unity3d.player->UnityPlayerActivity
When I open the .aar with Winrar, and open unity-classes.jar with a text editor, I see a almost everything as encoded symbols.
I'm not sure it is normal. Any idea?
EDIT 2: I think I know where the error is: when I import my .aar, I get an IDE fatal error. I don't know how to solve it, so if you can, please check the post I made about it:
IDE fatal error on importing .aar
For android Studio 2.3.3 you can use
repositories {
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'cube5-debug', ext: 'aar')
}
in the app level build.gradle. I did't check it for AS 3.0.
Add .aar file to your app by doing:
in project build.gradle
allprojects {
repositories {
flatDir {
dirs 'libs'
}
}
}
in app build.gradle
dependencies {
compile(name:'cube5-debug', ext:'aar')
}
So I managed to solve it. The .aar file I talked about was exported with AS, after I imported an Unity project into AS. I did this in order to use the Unity program inside AS. However, this gives error for some versions of Unity and AS. I installed Unity 5.5.0f3 and an older version of AS, and everything worked fine.
Note1: if you aim to install that Unity version, you will be prompted with a requirement of some windows 10 tool, which you must get (I cannot remember the name). I did it by downloading windows 10 install and installing only that tool, which comes with the package.
Note2: I made the program from Unity work inside AS but only because it was a simple program. I later wanted to do an Augmented Reality App in Unity, and use it in AS, but it cannot be done apparently. Evertyting works fine until I import the .aar into AS, and a class in that .aar library gets unresolved ("Video player”). This should have to do with the AR camera class, which AS does not recognize. I ended up creating a AR app with Unity, another app in AS, and from the AS app, calling the Unity app with two lines of code, whenever necessary. To return to the AS app, now in background, I simple click the back button, which is recognized by the Unity app (if you make code in Unity for it) and makes it destroy itself, returning to the AS app.

IntelliJ and Android Gradle dependencies - unable to find module

I have following directory structure:
D:\PROJECT
+---javaGradleProject1
+---javaGradleProject2
+---javaGradleProject3
\---AndroidProject
| build.gradle
| settings.gradle
\---AndroidModule
build.gradle
Android module depends on all of java gradle projects that are at the same level in root directory as AndroidProject.
In AndroidProject/settings.gradle I have:
include ':AndroidModule'
include 'javaGradleProject1'
project(':javaGradleProject1').projectDir = new File(rootDir, '../javaGradleProject1')
include 'javaGradleProject2'
project(':javaGradleProject2').projectDir = new File(rootDir, '../javaGradleProject2')
include 'javaGradleProject2'
project(':javaGradleProject2').projectDir = new File(rootDir, '../javaGradleProject2')
And then in AndroidProject/AndroidModule/build.gradle I have dependencies set like this:
compile project(':javaGradleProject1')
compile project(':javaGradleProject2')
compile project(':javaGradleProject3')
This structure of dependency perfectly works and project builds when I invoke
gradle build
on AndroidProject/build.gradle.
But when I try to synchronize my IntelliJ with current gradle dependency settings I receive
Error: Unable to find module with Gradle path ':javaGradleProject1'
Error: Unable to find module with Gradle path ':javaGradleProject2'
Error: Unable to find module with Gradle path ':javaGradleProject3'
and because of that my project cannot be run from Run Configurations (it does not compile at all in IDE). I was trying to add these dependencies manually by hitting F4 and module dependencies but after synchronization all of my changes are overwritten (actually, IntelliJ just removes it).
Is there anything wrong in my gradle structure?
I have tested it on IntelliJ IDEA 14.1.4 and Android Studio 1.3.
I was able to solve this by deleting the settings.gradle file from every Gradle project that I wanted to use as dependency (clearing the contents of this file was not enough).
NOTE: As I have little knowledge of Gradle and Android Studio, I cannot provide information about why the presence of this file does not allow Android Studio to include the Gradle project as a module.
Add a colon (:) before the names of your modules when you add it in settings.gradle, e.g. for the first one, change this:
include 'javaGradleProject1'
project(':javaGradleProject1').projectDir = new File(rootDir, '../javaGradleProject1')
to this:
include ':javaGradleProject1'
project(':javaGradleProject1').projectDir = new File(rootDir, '../javaGradleProject1')
Now in addition to compiling, IntelliJ / Android Studio should give you code completion.
In my case I had to change the external module name to lowercase only.
Right click project, Select
"Configure Project Subset ..."
and select your module, rebuild your project.
Tried all the available solutions, and in last I have commented the include ':app' in settings.gradle and this resolved the issue in my case
In my case non of the answers above resolved it.
Finally, I went to settings.gradle file and inside the dependencyResolutionManagement block, I changed the repositoriesMode to "PREFER_PROJECT"
repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
That was all I did and it worked like magic, maybe such can help you too, you can try it out.

TessBaseAPI cannot resolve symbol tess-two OCR Android

I am trying to run Demo App for OCR using tess-two but i am having problem with code:
I have follow these following steps:
Firstly i have downloaded and install the ndk from here https://developer.android.com/tools/sdk/ndk/index.html.
And I configured it successfully
Then I Download it "tess-two-master" from here https://github.com/rmtheis/tess-two and i extract it, in that location i run ndk-build command and it compiled successfully also.
Then i Open a completely new Android Studio project and follow these instructions https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle from section "Configure tess-two with gradle"
In the last step I go in "File -> Project Structure -> Select a module from the left subwindow -> Dependencies (last tab) ->Press the green "+" on your right -> Module Dependency -> OK"
After that i have downloaded this project https://github.com/GautamGupta/Simple-Android-OCR and I copy&paste in my new project the code in these files: "SimpleAndroidOCRActivity.java", "main.xml", "strings.xml". Of course my files had different names (in my case "MainActivity.java", "activity_main.xml", "strings.xml") so some renaming in the code was necessary.
I also opened my "AndroidManifest.xml" and I add at the end (but before "/manifest") between "/application" and "/manifest" in the just downloaded "AndroidManifest.xml" (it means that I have add it "uses-permissions" and "uses-feature" tag).
Last step I have downloaded from here https://code.google.com/p/tesseract-ocr/downloads/list the file in the english language. and in the folder of my project excatly in "app-->src-->main". I have created here a new folder and name it "assets". Inside that folder i have created another folder named "tessdata". and i put it there my .traineddata file.
Then when i am trying to sync it, i am having following error:
Warning:Dependency OCR3:app:unspecified on project tess-two resolves to an APK archive which is not supported as a compilation dependency. File: C:\Users\jana-PC\AndroidStudioProjects\OCR3\app\build\outputs\apk\app-release-unsigned.apk
When i am trying to compile it, it doesn't compile because it is showing me error that TessBaseAPI cannot resolve symbol:
My code in this part is:
import com.googlecode.tesseract.android.TessBaseAPI;
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.setDebug(true);
baseApi.init(DATA_PATH, lang);
baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
Anyone with some help, i would be very grateful !!
Have you tried this solution: https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle
To use the tess-two api, you must first add this line to your app:gradle file in you android project
compile 'com.rmtheis:tess-two:7.0.0'
Add this line and sync the project again and I'm sure your problems will be solved.
Gradle dependency management is what helped me out;
I was getting unresolved symbols when attempting to import tess-two;
I experienced unresolved symbols during import of tess-two to an android as well.
Figured out that i had to import tess-two as a 'Gradle Project', not as an
'Android .aar' or as an 'Android library'; This meant i had to choose the top-level project-level directory of the tess-two project as the import directory, rather than individual sub-project 'tess-two' within the tess-two project;
Finally determined that gradle syncs the tess-two project by use of gradle build file at the project level of tess -two; But if you import
the tess-two subprojects, the project-level gradle.build of tess-two never fully
resolves the dependencies underneath the tess-two project; Hope this helps;

How to create Android library in Android Studio [duplicate]

How do I create an Android Library Project (e.g. com.myapp.lib1) and the application project (e.g. com.myapp.app) and make the build system include com.myapp.lib1 on the application project?
I went to the Project Structure -> Modules -> My App project and added a dependency to the lib project. IntelliJ now can recognize classes from the lib project when used in the app project, but when I run the app project, there are errors like:
Gradle: error: package com.myapp.lib1 does not exist
I wonder why there is no example of stand alone jar project.
In eclipse, we just check "Is Library" box in project setting dialog.
In Android studio, I followed this steps and got a jar file.
Create a project.
open file in the left project menu.(app/build.gradle): Gradle Scripts > build.gradle(Module: XXX)
change one line: apply plugin: 'com.android.application' -> 'apply plugin: com.android.library'
remove applicationId in the file: applicationId "com.mycompany.testproject"
build project: Build > Rebuild Project
then you can get aar file: app > build > outputs > aar folder
change aar file extension name into zip
unzip, and you can see classes.jar in the folder.
rename and use it!
Anyway, I don't know why google makes jar creation so troublesome in android studio.
To create a library:
File > New Module
select Android Library
To use the library add it as a dependancy:
File > Project Structure > Modules > Dependencies
Then add the module (android library) as a module dependency.
Run your project. It will work.
Google’s Gradle Plugin recommended way for configuring your gradle files to build multiple projects has some shortcomings If you have multiple projects depending upon one library project, this post briefly explain Google’s recommended configuration, its shortcomings, and recommend a different way to configure your gradle files to support multi-project setups in Android Studio:
An alternative multiproject setup for android studio
A Different Way :
It turns out there’s a better way to manage multiple projects in Android Studio. The trick is to create separate Android Studio projects for your libraries and to tell gradle that the module for the library that your app depends on is located in the library’s project directory. If you wanted to use this method with the project structure I’ve described above, you would do the following:
Create an Android Studio project for the StickyListHeaders library
Create an Android Studio project for App2
Create an Android Studio project for App1
Configure App1 and App2 to build the modules in the StickyListHeaders project.
The 4th step is the hard part, so that’s the only step that I’ll describe in detail. You can reference modules that are external to your project’s directory by adding a project statement in your settings.gradle file and by setting the projectDir property on the ProjectDescriptor object that’s returned by that project statement:
The code one has to put in settings.gradle:
include ':library1'
project(':library1').projectDir = new File('../StickyListHeader/library1')
If you’ve done this correctly, you’ll notice that the modules referenced by your project will show up in the project navigator, even if those modules are external to the project directory:
This allows you to work on library code and app code simultaneously. Version control integration also works just fine when you reference modules externally this way. You can commit and push your modifications to the library code just like you can commit and push modifications to your app code.
This way of setting up multiple projects avoids the difficulties that plague Google’s recommended configuration. Because we are referencing a module that is outside of the project directory we don’t have to make extra copies of the library module for every app that depends on it and we can version our libraries without any sort of git submodule nonsense.
Unfortunately, this other way of setting up multiple projects is very difficult to find. Obviously, its not something you’ll figure out from looking at Google’s guide, and at this point, there’s no way to configure your projects in this way by using the UI of Android Studio.
Check out this link about multi project setups.
Some things to point out, make sure you have your settings.gradle updated to reference both the app and library modules.
settings.gradle: include ':app', ':libraries:lib1', ':libraries:lib2'
Also make sure that the app's build.gradle has the followng:
dependencies {
compile project(':libraries:lib1')
}
You should have the following structure:
MyProject/
| settings.gradle
+ app/
| build.gradle
+ libraries/
+ lib1/
| build.gradle
+ lib2/
| build.gradle
The app's build.gradle should use the com.android.application plugin while any libraries' build.gradle should use the com.android.library plugin.
The Android Studio IDE should update if you're able to build from the command line with this setup.
For Intellij IDEA (and Android Studio) each library is a Module. Think of a Module in Android Studio as an equivalent to project in Eclipse. Project in Android Studio is a collection of modules. Modules can be runnable applications or library modules.
So, in order to add a new android library project to you need to create a module of type "Android library". Then add this library module to the dependency list of your main module (Application module).
The simplest way for me to create and reuse a library project:
On an opened project file > new > new module (and answer the UI questions)
check/or add if in the file settings.gradle: include ':myLibrary'
check/or add if in the file build.gradle:
dependencies {
...
compile project(':myLibrary')
}
To reuse this library module in another project, copy it's folder in the project instead of step 1 and do the steps 2 and 3.
You can also create a new studio application project
You can easily change an existing application module to a library module by changing the plugin assignment in the build.gradle file to com.android.library.
apply plugin: 'com.android.application'
android {...}
to
apply plugin: 'com.android.library'
android {...}
more here
You can add a new module to any application as Blundell says on his answer and then reference it from any other application.
If you want to move the module to any place on your computer just move the module folder (modules are completely independent), then you will have to reference the module.
To reference this module you should:
On build.gradle file of your app add:
dependencies {
...
compile project(':myandroidlib')
}
On settings.gradle file add the following:
include ':app', ':myandroidlib'
project(':myandroidlib').projectDir = new File(PATH_TO_YOUR_MODULE)
Don't forget to use apply plugin: 'com.android.library' in your build.gradle instead of apply plugin: 'com.android.application'
Documentation Way
This is the recommended way as per the advice given in the Android Studio documentation.
Create a library module
Create a new project to make your library in. Click File > New > New Module > Android Library > Next > (choose name) > Finish. Then add whatever classes and resourced you want to your library.
When you build the module an AAR file will be created. You can find it in project-name/module-name/build/outputs/aar/.
Add your library as a dependency
You can add your library as a dependency to another project like this:
Import your library AAR file with File > New Module > Import .JAR/.AAR Package > Next > (choose file location) > Finish. (Don't import the code, otherwise it will be editable in too many places.)
In the settings.gradle file, make sure your library name is there.
include ':app', ':my-library-module'
In the app's build.gradle file, add the compile line to the dependencies section:
dependencies {
compile project(":my-library-module")
}
You will be prompted to sync your project with gradle. Do it.
That's it. You should be able to use your library now.
Notes
If you want to make your library easily available to a larger audience, consider using JitPac or JCenter.
Had the same question and solved it the following way:
Start situation:
FrigoShare (root)
|-Modules: frigoshare, frigoShare-backend
Target: want to add a module named dataformats
Add a new module (e.g.: Java Library)
Make sure your settings.gradle look like this (normally automatically):
include ':frigoshare', ':frigoShare-backend', ':dataformats'
Make sure (manually) that the build.gradle files of the modules that need to use your library have the following dependency:
dependencies {
...
compile project(':dataformats')
}
Purpose: Android library at single place - Share across multiple projects
http://raevilman.blogspot.com/2016/02/android-library-project-using-android.html
As theczechsensation comment above I try to search about Gradle Build Varians and I found this link: http://code.tutsplus.com/tutorials/using-gradle-build-variants--cms-25005
This is a very simple solution. This is what I did:
- In build.gradle:
flavorDimensions "version"
productFlavors {
trial{
applicationId "org.de_studio.recentappswitcher.trial"
flavorDimension "version"
}
pro{
applicationId "org.de_studio.recentappswitcher.pro"
flavorDimension "version"
}
}
Then I have 2 more version of my app: pro and trial with 2 diffrent packageName which is 2 applicationId in above code so I can upload both to Google Play. I still just code in the "main" section and use the getpackageName to switch between to version. Just go to the link I gave for detail.
There are two simplest ways if one does not work please try the other one.
Add dependency of the library inside dependency inside build.gradle file of the library u r using, and paste ur library in External Libraries.
OR
Just Go to your libs folder inside app folder and paste all your .jar e.g Library files there Now the trick here is that now go inside settings.gradle file now add this line "include ':app:libs'" after "include ':app'" It will definitely work...........:)
In my case, using MAC OS X 10.11 and Android 2.0, and by doing exactly what Aqib Mumtaz has explained.
But, each time, I had this message : "A problem occurred configuring project ':app'. > Cannot evaluate module xxx : Configuration with name 'default' not found."
I found that the reason of this message is that Android 2.0 doesn't allow to create a library directly. So, I have decided first to create an app projet and then to modify the build.gradle in order to transform it as a library.
This solution doesn't work, because a Library project is very different than an app project.
So, I have resolved my problem like this :
First create an standard app (if needed) ;
Then choose 'File/Create Module'
Go to the finder and move the folder of the module freshly created in your framework directory
Then continue with the solution proposed by Aqib Mumtaz.
As a result, your library source will be shared without needing to duplicate source files each time (it was an heresy for me!)
Hoping that this help you.

Categories

Resources