I'm migrating from Eclipse over to Android Studio and am looking at the Navigation Drawer example from Google. Right away, I see that I get this warning;
It looks like I need to use the v7 library rather than the v4. I can't figure out how to do this. In Eclipse, I just added a dependency from the build tools, but I see no such option in Android Studio. If I look at the build.gradle file, I can see this:
dependencies {
compile 'com.android.support:support-v4:21.0.3'
}
I just downloaded Android Studio yesterday, so I think that I should have this dependency somewhere, but I just don't know how to include it. Can someone tell me how to do this?
From within Android Studio, you can go to File, Project Structure, select your module, and go to the Dependencies tab - you'll see a list of dependencies - you can add new dependencies via the + sign near the bottom of that screen - it will automatically suggest the most popular libraries and all Google libraries including the one you want: appcompat-v7. This controls the build.gradle file and, in your case, adds the line:
compile 'com.android.support:appcompat-v7:21.0.3'
Note that you can also use sites such as Gradle, please to manually figure out what the dependency should be and add it to your build.gradle file yourself if you'd like.
Related
I have an Android project with the following dependencies:
-- Android App
---> MySDK.Jar
------> 'org.apache.commons:commons-lang3:3.5'
This is MySDK.jar that has a dependency on commons-lang3.
I'm working on Android Studio and I'm thus using Gradle.
Here is my problem:
I have shared "MySDK.Jar" to someone and he has built his own Android App on top of it.
It works but we have seen that the compiler doesn't notice the missing dependency on 'org.apache.commons:commons-lang3:3.5'. At run-time there will be a crash if the code using 'org.apache.commons:commons-lang3:3.5' is called. One may not notice the problem if he doesn't call the code using this library.
I know that we can solve this issue by adding the following line to Android App build.gradle file:
compile 'org.apache.commons:commons-lang3:3.5'
I'm wondering if there is a way to get a compile error indicating such missing dependencies? It is indeed better to see the dependency problem at compilation time rather than at runtime.
What are the recommended good practices for this?
Thanks!
commons-lang3 is a transitive dependency of Android App. As such, it is often not needed for compilation - there are exceptions, especially regarding multiple levels of inheritance. So at compile time you (usually) do not know whether you miss a transitive dependency that you need at runtime.
This is where Gradle comes in. Gradle can (as Maven) resolve dependencies transitively from a Maven repository (as MavenCentral). If you put MySDK into a Maven repository (like Nexus or Artifactory, which have open source versions), everyone using MySDK will automatically draw commons-lang3 so you will not miss anything at runtime.
If you are just adding the jar file in your project you can't warning about the missing dependencies.
To do it you have to publish the jar file in a maven repo.
In this way you have a pom file which describes the dependencies that gradle has to download.
Provide a method like MySDK.init() int your MySDK.jar,call a method whe is belong to org.apache.commons:commons-lang3:3.5' in the MySDK.init() method, then put init() into onCreate() of your Application,
Another way is,putorg.apache.commons:commons-lang3:3.5 into MySDK.jar,
Hope it helps you :)
The title is a little misleading but I honestly don't know how to word it in any other way. This is my project structure:
I want to use the highlighted (fasteranimationscontainer-master) in my current app that I'm working on. I imported it by putting the jar file in my library folder then adding it to my library through Android Studio. But I try to create an object of that imported library, it doesn't show up/import.
I'm still learning how to use android studio so any help would be much appreciated!
1. Library with only the Source Code
To include a library with only the source code (like FasterAnimationsContainer) to your project, you only need to import it as a module from File -> New-> Import Module in Android Studio:
There should be a dialog for the project folder, enter the path where your library reside.
Then you need to add the module to your app build.gradle as a dependency:
android {
...
dependencies {
...
compile project(':fasteranimationscontainer')
...
}
}
Now you can use it in your project.
2. Library from Maven or JCenter
To add dependencies to your project where the library has uploaded to maven or jcenter, you need to modify your app build.gradle file and add extra lines configuring the packages you require. For example, for certain Google or Android, dependencies look like:
android {
...
dependencies {
// Google Play Services
compile 'com.google.android.gms:play-services:6.5.+'
// Support Libraries
compile 'com.android.support:support-v4:22.2.1'
}
}
Try to always read the library README first.
Suggestion:
If you still learning using Android Studio, you can read Using Android Studio. Then read Getting Started with Gradle, because Android Studio is tightly related with Gradle.
eg.
`dependencies {
compile 'me.grantland:autofittextview:0.2.+'
}`
Where do i add this and how to make it work? i am trying to incorporate autofit textview in android
Find the build.gradle for your project, add the compile statement to the existing lines in the dependencies section. Android Studio will prompt you to "Sync Gradle Project Files", which will trigger everything required to find the library. After the sync you'll be able to import and use the classes as required.
If you want to use this then you need to have an Build-Management Tool like Maven, Ant or Gradle. Then this Tool downloads the dependacys for you.
But if you dont have something like this then just add this file to the project:
http://repo.maven.apache.org/maven2/me/grantland/autofittextview/0.2.0/autofittextview-0.2.0-sources.jar
I think I've tried everything now...
I've been all over stack overflow, I've even consulted the android doc
Most of the posts here show how to add the .jar file in Eclipse "android tools -> add support library" or something
So I've tried to do it how the doc tells me, by going to build.gradle, and adding compile 'com.android.support:support-v4:19.0.0' to the dependencies
but even if I add it, and rebuild the project, I still can't import android.support.v4.content.LocalBroadcastManager... it doesn't even know what android.support is...
So is there anyone who can help me with this? the issue is getting very annoying, and it's slowed me down by hours...
Edit: I have updated the Android SDK, and made sure that the library exists in the sdk folder, so I know it's there... I just can't make android studio add it for some reason...
Edit 2: Only by navigating to the exact file path, manually, and then copying said path I was able to access the file... Still, I'd very much like to know what I can do to make this a more painless process...
Make sure your library got added successfully.
Check the following things.
1.Your build.gradle should be like
dependencies {
compile 'com.android.support:support-v4:19.0.+'
}
2.File > Project Structure > Modules >Dependency Tab
There should not be any red mark in dialog showing error if they are fix them.
3.Check your Module_Name.iml file
It should have this entry in last section
<orderEntry type="library" exported="" name="support-v4-19.0.0" level="project"/>
If its there with no entry close you studio delete .iml file and open studio again wait for gradle sync to be complete.You .iml file will be created again check for entry.
If every thing is fine do sync Your project with gradle again using this lovely tiny gradle button in toolbar.
Try adding one of those dependencies to your app level build.gradle :
AndroidX
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
Support Library
implementation 'com.android.support:localbroadcastmanager:28.0.0'
Then sync project with Gradle files.
After lot of searching and R&D i found some solution. This is working for me i hope this is helping you.
implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'
If you not use Androidx please add this line
implementation 'android.legacy:legacy-support-core-utils:1.0.0'
First of all, I know how to add a local library to the build.gradle file, it was discussed in several questions here already (which are all basically the same), see here, here and here. But you have to hardcode the paths in the compile files('/path/to/lib.jar') statements in the build.gradle file, which isn't nice, not redistributable, etc, IF you use a library not within the project's folder structure. I prefer to maintain this library for all my projects in the same place (so it is always up to date for all projects etc.). So I would like to know how to add a library, which is not available via Maven, to an Android-Studio project using gradle, in a sane way, given that the library is added as a global library in AS's preferences.
What I have done so far:
I use Google's new Android-Studio, which uses gradle for the build management, to build an Xposed framework module. For that, I have to include an external library, XposedLibrary, which I downloaded from the respective Github repository to keep it up-to-date.
It contains the jar XposedLibrary/XposedBridgeApi.jar, which I added in AS as a global library (Ctrl+Shift+Alt+S -> Global Libraries -> green plus to add the folder XposedLibrary). The compilation failed, complaining that it doesn't know the imported classes. So I had to manually add the library to the build.gradle file, adding the respective line in the dependencies like so:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar')
}
I tried out to just add compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') but this didn't work.
So, what is a nice way to add an AS global library to the dependencies without using full paths? (I don't like the idea of symlinking to the jar file from within the lib/ folder ;) )
when referencing a file via
files("relative/path/to/a.jar")
the relative path is evaluated relative to the buildscript this snippet is in. so when your build.gradle file is located in let's say '/a/project/build.gradle' then the jar should be in '/a/project/relative/path/to/a.jar'. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via
rootProject.files("relative/to/root/a.jar")
hope that helps,
cheers,
René
This post describes how to get XposedBridgeApi.jar working with Gradle in Android Sudio: http://forum.xda-developers.com/showpost.php?p=41904291&postcount=1570
I think here is the proper way:
Import Xposed in Android Studio
Edit the /app/build.gradle like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided fileTree(dir: 'deps', include: ['*.jar'])
}
The best way is to use "provided files('src/XposedBridgeApi-54.jar')" as the lib isn't allowed to be included in the module, because the XposedBridge is already installed on the phone.
With Android Studio, you have to first understand that the IDE uses the same model for a project that your command line build (gradle) uses. That is why the Project Structure dialog has a pop up that says edits here will have no effect. So adding a global library will also have no effect.
The correct way to fix such issues is to edit your gradle build scripts so that the command line gradle build works properly. Then you should just have to click on "Tools | Android | Sync Project with Gradle files" menu item to refresh the project structure in the IDE.
Finally, if your dependencies are not going to be in Maven Central, then you'd have to create a local maven repository. Read the thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for background.