Why does Android Studio don't recognize .jar library imports? - android

I'm trying to use some .jar files as libraries in my Android Studio project.
I've been looking on how to do it, and the regular way is to copy the .jar file in the libs folder, and then add it as library. I know the libs folder must be inside "app" module in the "project" view. But that option to add as library doesn't appear. So my approach is to add it manually doing right click on "app" and:
Open Module Setings/app/Dependencies/ and there add .jar files as file dependency.
Then, I go to my .class but the import suggestions don't let me select these libraries as import.
I have tried another approach from the "android" view. Doing right click on "app" and selecting "new module", there I select Import .JAR or .AAR Package and then I do the same as before from Open Module Setings, but this time I add it as Module Dependency.
In booth ways, the gradle.build file is updated adding these dependencies, but when going to the activity to select the import, it doesn't give me the chance to select the import from these ones.
I have spent a full day trying to solve this, but I don't get my activity to recognize the imports from these files, so I would appreciate if someone could tell me what I'm doing wrong or what I'm missing to do.
Top-level build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
app-level build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/values-v14'] } }
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':activation')
compile project(':mail')
}

My current project using gson-2-2.4.jar and YouTubeAndroidPlayerApi.jar and some others. To import those jar, I put it in my libs folder. After that, go to build.gradle and delete some characters in it, and rewrite (1). Finally, click "sync now" (2). I don't know why my sync button does not work when I add jar to libs, so I do that, and it work so well.
UPDATE
On Android 3.x.y we can use Sync Project with Gradle Files or Refresh all Gradle projects

Firstly, I import this JAR, AS can not recognize this JAR and I can not import.
And than I press right click in this JAR and choose "Add as Library". AS can recognize this JAR and solve the problem!!

With Respect to Android Studio 1.5.1:
You need to copy and paste the JAR file into:
APP_NAME/app/libs
Note that the libs folder will already exist. Once copied, change the project view from Android to Project files.
Open up the app/libs folder and right click the JAR file, then click Add As Library.
That should do it. I wasn't able successfully use the Add Module to import a JAR file.

After a chat with M D we arrived to the conclusion that my AS does not recognize the .jar files, so I have had to port the project back to eclipse, and it works fine there.

Related

Android studio gradle error, "configuration with name default not found" with aviary sdk

First of all, I know there are a lot of subjects about this problems, but I still can't fix it.
I'm just trying to customize aviary sdk by following the guide here : https://developers.aviary.com/docs/android/setup-guide# (section 4.2, not 4.1)
here are my root gradle files :
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
settings.gradle :
include ':app', ':Aviary-Android-Sample-App'
here is my app build.gradle file :
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.agaze.myapplication"
minSdkVersion 10
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:20.0.0'
compile project(':Aviary-Android-Sample-App')
}
Here my files tree:
Thanks in advance for your tips.
Antonin
Check your submodules and make sure each submodule in your project has its own build.gradle file.
Check this guide for more info
tools.android.com/tech-docs/new-build-system/user-guide
Each library you add to your project should have it's own build.gradle file. If it does not have you can get this error.
For a solution you can add your sdk as a module with Android Studio. To do this you can follow the path in Android Studio:
File > New > Import Module and select directory of your sdk and add to your project.
Select Gradle System and let Android Studio create gradle files for your sdk.
You can check the link below for more information and how to add a module to your project.
http://www.truiton.com/2015/02/android-studio-add-library-project/
I hope this'll help you.
From your screen shot.It's obvious that the Aviary-Android-Sample-App is an eclipse project(no gradle file there) but not a valid gradle module.
If you want to import it into android studio, in android studio, click File -> new module -import eclipse adt project and go on under the instruction.
And you are good to go.
Edit:
Also, it seems that you want to modified the Aviary sdk, but the module you want to import is a sample app. Maybe you have downloaded the wrong file. The correct link looks like this one https://creativesdk.adobe.com/downloads.html

Android how to create library (jar) containing Altbeacon library with Android studio and gradle

I'm a newbie in Android especially for libs.
I'm using Android Studio 1.2.2
I'm trying to create a jar library with no activity, resources, etc (only code) that use the AltBeacon library. When complete I whould like to include this jar in my APP that has to use beacons through my lib.
Well, using AltBeacon instructions, I'm able configure gradle to have a "normal" (with activity etc) APP using AltBeacon library. But I'm not able to create a jar library that contains the AltBeacon one.
I google a lot especially in SO, I found a lot of questions about "library inside library" but that doesn't worked for me.
I made this:
I created an empty project with Studio (blank activity)
Then I created a new module -> Java Library and called it libmebeaconmanager and Studio created a new Java class that I called MeBeaconManager
I modified the build.gradle of the library in this way:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.altbeacon:android-beacon-library:2+'
}
This is the build.gradle of Module:app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"
defaultConfig {
applicationId "me.padovame.libbeacontest"
minSdkVersion 18
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
And this is the build.gradle of project:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
At this point, in the MeBeaconManager class of the lib, I tried to import AltBeacon classes without success because I got an undefined symbol error for import lines like this:
import org.altbeacon.beacon.Beacon;
I also tried, without success, inserting in lib folder of libmebeaconmanager the downloaded android-beacon-library-2.3.aar
What am I doing wrong?
Thanks in advance.
EDIT:
I tried to include a jar (tried with Picasso) library in my libmebeaconmanager and it worked. So the problem seems to be with aar libs.
I'm going crazy...
EDIT 2:
I extracted classes.jar from the AltBeacon aar lib and used as other jar and it worked.
But I consider this as a workaround: I still do not understand how to manage directly aar files. So thanks to whom can help me.
Seems like there is a problem in inserting library to project structure
You can have a look at answer provided by user Sam Rad here:
How do I add a library project to Android Studio?

Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'.

I am getting the following build error when I try and sync my project:
Error:(9, 0) Gradle DSL method not found: 'compile()'
Possible causes:The project 'AlexTest' may be using a version of Gradle that does not contain the method.
The build file may be missing a Gradle plugin.
link: Apply Gradle plugin
I have tried applying every single gradle plugin they link me to in that link on the bottom, yet same issue, so I conclude that the first error is the cause.
Here is the build.gradle file for AlexTest (the project directory):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.2'
compile 'com.google.android.gms:play-services:6.1.11'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
I think that was the gradle file it was having trouble with. But I'm not sure what method it is referring to.
Also here is the gradle-wrapper.properties which it also referred to:
#Mon Nov 10 01:06:12 PST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip
perhaps the gradle version in the distributionUrl needs to match the one in the dependency?
I also have a build.gradle file in the app directory itself - 1 level lower, though I don't think that is what it was referring to, but here it is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.snappiesticker.alextest"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:6.1.+'
}
I have tried applying every single gradle plugin they link me to in that link on the bottom, yet same issue, so I conclude that the first error is the cause.
Correct.
Here is the build.gradle file for AlexTest (the project directory):
You will notice that this file contains a code comment:
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Remove the compile 'com.google.android.gms:play-services:6.1.11' line from that file. Leave the compile 'com.google.android.gms:play-services:6.1.+' that you have in the other build.gradle file.
The dependencies closure in buildscript is for Gradle plugins only. The top-level dependencies closure, found in the module's build.gradle file, is for application dependencies.
Saw reports that the problem occurred for other reasons, but this solved for me.
If you carry out any changes in the Project Structure (Android Studio) and press OK, Gradle will be synchronized, but this type of synchronization, if the dependency block has something like:
This block will stay this way after synchronization:
One way to solve the problem is to insert a line break to stay as it was before the synchronization or put a semi-colon between the two statements.
I hope it helps.
Just add foolwoing statement in your dependencies
apply plugin: 'jetty'
Hi everyone for me it was a "couple days consuming job" to make my app run in Android Studio (I migrated from Eclipse and had this problem too ) . Finally I found that very simple way of it .
Create libs folder under src/main/java/ it is App/java/libs in left pane .
Copy and paste all your external jars into here.
Goto left pane and right click on your App then click Open Module Settings
Then Project Structure window will appear .
Then move to Dependencies tab .
Final Step : Add all your jars located in App/java/libs (You will find them in src/main/java/libs) one by one .
That is all Enjoy it.
Declare dependencies in Module's build.gradle file, not in AlexTest's build.gradle file

Freshly imported Android Studio project errors with zxing module

I have been using zxing module as an android library for some time without problems. I have imported my Eclipse project into Android Studio (which I use for everything else!) and the zxing library is added as a module.
In one of my classes in the main module I reference the com.google.zxing.android.client.Intents class which is in the zxing module. I can write code in Android studio and these references have the correct imports selected.
When I run a gradlew clean build I am getting messages saying 'cannot find symbol class Intents'.
In settings.gradle I have includes for both my main module and the zxing module.
In the build.gradle of my main module I have 'compile project(':zxing'). These were both added during the import from Eclipse. I can also see that the zxing module is being built as part of the gradlew clean build.
I have tried deleting the zxing module and references then trying to add it manually. I have compared the way that this module is implemented to another project with a similar module that works. All looks fine.
Is anyone able to make a suggestion?
Edit:
settings.gradle file
include ':app'
include ':captureActivity'
build.gradle of main module
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.wigglyamps.littlegreenbutton"
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile project(':captureActivity')
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
Java class file in main module
public class test {
public void hello () {
String i = Intents.Scan.ACTION;
}
}
I don't really know the zxing framework, but have you tried to use the maven artifact instead of referencing a module?
If you want to use the artifact add this to the dependencies of the project/module that uses zxing: compile 'com.google.zxing:core:3.1.0'
Edit: You said you imported the project from eclipse; I'm in the middle of transitioning to gradle and the import function hardly produces anything useful if the eclipse project contained references to library projects (not jar's). I rebuild my projects from scratch by creating a new app(with modules) in Android Studio and copy/paste code and resources into the new app's directories

Import Facebook SDK on Android Studio 0.5.1

I've been searching around for a while now and tried every answer I could find with no success. I am starting to believe that the problem is in the android studio version.
However here is what I've done:
1 - I've downloaded the facebook sdk
2 - Copied the sdk into my libs folder so the project looks like
following:
MyProj
-app
--libs
---facebook
----build.gradle (2)
--build.gradle (1)
-settings.gradle
3 - I modified settings.gradle:
include ':libs:facebook', ':app'
4 - I modified build.gradle (1) to:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'me.dm7.barcodescanner:zxing:1.0'
compile 'com.koushikdutta.ion:ion:1.2.4'
compile 'com.google.code.gson:gson:2.2.+'
compile 'com.squareup.picasso:picasso:2.1.1'
compile project(':libs:facebook');
}
5 - Lastly edited build.gradle (2):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:+'
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
Now when syncing I get the annoying warning:
Gradle 'MyApp' project refresh failed:
Configuration with name 'default' not found
And I am not able to use the facebook library.
I guess I'm doing something wrong with the gradle files. Any ideas?
On Mac with Android Studio 0.5.8, this is what worked for me:
Click on the top level project and select project structure:
Click the + to add another module
Click on "Import Existing Project" and "Next"
Select the facebook directory from your SDK folder and click next
The facebook module should now be shown in addition to your existing module
Click on your project, select the Dependencies tab and click '+' to add a dependency.
Select "Module Dependency" as dependency type.
Select the Facebook module that we just added
Note that it shows up under dependencies (of your app)
And you're all set!
The directory structure of your project doesn't match the dependency specs you're using; it's not clear what's in your settings.gradle. That error you get with "Configuration name 'default' not found" is terribly unintuitive, but it's the message you get when Gradle is looking for a module in a certain directory and it's not finding it. If you have a dependency spec (and settings.gradle include) like :libs:facebook, it will look in MyProj/libs/facebook, not MyProj/app/libs/facebook where you've placed it.
I would recommend this structure:
MyProj
-app
--build.gradle (1)
-libs
--facebook
---build.gradle (2)
-settings.gradle
i.e. move the libs directory one level up so it's alongside app directory instead of underneath it.
Your settings.gradle file should be:
include ':app', ':libs:facebook'
(which is probably how you already have it set up) and a dependency on facebook should look like:
compile project(':libs:facebook')
(also like how you already have it set up)
Follow this step to add Facebook SDK to your Android project.
1) Open your fresh Android project
2) Go to File -> Project Structure (or) Alt+Ctrl+Shift+S
3) Go to module click + on second row of window then import module
4) After Facebook SDK imported click + on third row and select Module Dependency select Facebook SDK Apply and press ok
I made a Facebook Module version from scratch https://github.com/cesarferreira/Facebook-Module
If you want to use this module for your projects you should follow the next steps:
You should create a new 'libs' folder in your project root
You should copy this project into the 'libs' folder, you must manually copy the project, because if you copy it using the drag and drop action the project doesn't work because android studio throw an error
Add the next line into settings.gradle file: include ':libs:facebook'
You should rebuild the project
THAT'S IT!

Categories

Resources