I have an Android Studio library project which uses library.aar and library.jar. When I export it as mylib.aar and use this new AAR into a different app/project, it cannot find the classes defined in the library.aar and library.jar.
My build.gradle looks like this:
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':library_dot_aar')
compile project(':library_dot_jar')
}
I imported the AAR and JAR files in Android Studio using New -> Module -> Import .JAR or .AAR Package and add them as dependencies in Project Structure -> Module Settings -> Dependencies -> Add Module Dependencies.
When I unzipped mylib.aar it was only 25KB (considering library.aar was over 300KB, I assume mylib.aar's size would be in the same range) and the included dependencies were not included in it. How do I export an AAR that includes all its dependencies inside itself?
You must check this answer:
There is no mechanism to combine library. It's a bit complicated as
you probably want to control which dependencies get merged (for
instance you probably don't want to include support-v4 in there). Also
you'd need to merge the resources and Android manifest.
At this time there's no way to easily hack something, unless you are
sure the resources have no conflicts between the two res folders (for
instance you could have strings_a.xml in one lib and strings_b.xml in
the other lib). This way you can just "merge" the two res folders by
copying them both into the same location (as opposed to do a merge at
the android res level).
For the Manifest it'd be more complicated, but doable with some custom
code.
Providing a built-in mechanism for this is very low on our priority so
don't expect it anytime soon.
Related
After updating Android Studio and gradle to 3.1, I changed all compile statements to implementation. But when I build, android studio cannot resolve imports found in 3rd party libraries.
Scenario: Main projects imports sub-module which also import a jar file.
When I try to import a class from the jar file into the main project, android studio is not able to resolve it.
How can I import the single file without having to add the jar file as a dependency in the main project?
You should use api instead, it is the new compile or have the dependency directly in your main project. Just changing implementation to api will fix the issue but you consider using implementation wherever possible to improve build time.
You can see the difference between api and implemenation here.
The answer by #nongthonbam-tonthoi is correct but he does not explain why.
Short version
Implementation - hide this dependency from other modules(that depend on this module). if B depends on A, it cannot use any dep declared in A using implementation.
api - Make this available to other modules that depend on this module.i.e if you add say GSON as a dep in module A using api rather than implementation, all other modules that depend A can use GSON without declaring it again.
Long version
implementation is a way of declaring dependencies for only a given module. What this means is that, the dependency can only be used in that particular module. compile on the other hand "leaks" the dependencies to other modules so you can import and use the classes that dep brings in other modules. If you want this behavior, the new way of doing it is to use api.
This change is particularly targeted at multi-module projects as it can help gradle avoid re-compiling a module during a build when it does not change.
However if you're migrating from an old project, chances are you are (ab)using compile to use dependencies declared in other modules without explicitly declaring them again.
You can keep using compile but remember that it's is deprecated and will be removed soon.
See here for a deeper explanation.
Edit build.gradle (Module:app) and change SDK version to 27.1.1
Eg:
defaultConfig {
applicationId "com.projectname"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
implementation 'com.android.support:appcompat-v7:27.1.1'
and rebuild the project and restart android studio
I would like to create two falvors for my project. I did this successfully in the past but with new ANdroidStudio I a little bit concerned how to do that. The official guide is out-dated.
In the past what we need to do is to create additional folders on the level of "src" folder. In the current version of AndroidStudio we have app->{/java;/manifests;/res} structure and we can create folder only under the same package and I don't see the way to create additional folder for "res"
Can you advise please how I can create flavors for my app?
The official guide is out-dated.
The core of the official guide's section on product flavors seems fine to me.
In the past what we need to do is to create additional folders on the level of "src" folder.
That would define a sourceset for a product flavor. You still need to declare the product flavor in build.gradle for the module, via a productFlavors closure or via the Project Structure dialog.
In the current version of AndroidStudio we have app->{/java;/manifests;/res} structure
Switch from the Android view of your project contents to the Project view, via the drop-down just above the project structure tree.
You will start with a tree like:
and change it to a tree like:
Can you advise please how I can create flavors for my app?
Add a productFlavors closure to your module's build.gradle file, manually or via the Project Structure dialog:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.commonsware.myapplication"
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
vanilla {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
If you want to have alternative source for that product flavor (Java, resources, manifest, etc.), create src/.../ in the project, where ... is the name of your flavor (src/vanilla/ would correspond with the sample build.gradle shown above). To create this directory, either:
in the Project view, create it as you would any other directory
in the Android view, just start defining a resource, and the dialog that appears will have a drop-down for the sourceset:
However, as I do not know how to put Java code in a particular sourceset this way, I always work in the Project view.
Maybe try with build types, somewhat similar to this post. You may change resource folders in that fashion
I'm using android studio 0.2.3 with gradle 0.5 and added the ormlite dependency to the build.gradle file as follows:
compile 'com.j256.ormlite:ormlite-android:4.9'
Gradle downloaded two jar files: ormlite-android.jar and ormlite-core.jar. The problem is, that the jar files contain identically named classes. So I get the following well known exception:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/j256/ormlite/dao/BaseDaoImpl$1;
Some other solution for the same problem with maven exists, suggesting to exclude the ormlite-core.jar. This should work if all classes from ormlite-core.jar are included in ormlite-android.jar - I didn't check this btw. In that case, I don't understand why the ormlite-core is in this ormlite android dependency package... I'm explicitly adding ormlite-android, as you can see in the snippet above.
But how to exclude the ormlite-core.jar in gradle. Everything I found was for gradle 1.6, but android studio uses gradle 0.5 - or is this just the version of the android gradle wrapper?
.:EDIT:.
To make the dependencies clearer, I add my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
compile 'com.google.android.gms:play-services:3.1.+'
compile 'com.j256.ormlite:ormlite-android:4.9'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
}
Why I think there are same classes in the two ormlite libs? ==> See the screenshot.
Aren't the opened packages identical? Even the source is. The only distinct classes I found were SqliteAndroidDatabaseType and those in the com.j256.ormlite.android package.
The ormlite-android jar you're using is definitely wrong. My guess is that someone built it incorrectly with ormlite-core exported, which is why you're getting the merge conflicts. If you look at the source for ormlite-android, it isn't supposed to have most of those packages/classes included.
I'm not sure how the ormlite-android versioning works, but it looks like 4.46 is the actual latest version (updated 29-Jul-2013), not 4.9 (updated 26-Jan-2011). I'd recommend using 4.46 instead (that's what works for me) with:
'com.j256.ormlite:ormlite-android:4.46'
Thanks to #cproinger for the answer in another related question.
WOW, now it works! I misunderstood the versioning (my fault). But anyway, the newer version (4.46) contains only the android specific classes in the ormlite-android.jar and everything else in the ormlite-core.jar. Great, thanks #cproinger!
I have a native lib in the
/libs/armeabi folder called libparser.so
and an associated jar file.
I changed the gradle build file to include the jar file, which seemsm to be easy (MYNEWJAR):
dependencies {
compile files('libs/android-support-v4.jar', 'libs/MYNEWJAR.jar')
}
But when I run the app, I think it cannot find the native lib:
E/AndroidRuntime(22569): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load parser from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.hybris.mobile.history-1.apk,libraryPath=/data/app-lib/com.hybris.mobile.history-1]: findLibrary returned null
E/AndroidRuntime(22569): at java.lang.Runtime.loadLibrary(Runtime.java:365)
E/AndroidRuntime(22569): at java.lang.System.loadLibrary(System.java:535)
E/AndroidRuntime(22569): at com.senstation.android.pincast.Pincast.<clinit>(Pincast.java:1299)
E/AndroidRuntime(22569): ... 17 more
Can you help me get the build file straight so it will include the native lib? This seems to be happening automatically on Eclipse, but i really want to use android studio.
Thx!
Sven
I found this answer from user Assaf Gamliel very useful.
And just made some changes to make it even more cleaner.
You don't need to rename the .zip file to .jar, just add it with a normal compile file dependency on build.gradle script. So, you would make a foo.zip file with a structure similar to this:
foo.zip ->
|--/lib
|--|--/armeabi
|--|--|--*.so
|--|--/x86
|--|--|--*.so
put it in your libs folder and then add it to gradle using compile files('libs/foo.zip'):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+' //gradle plugin update for Andoid Studio 0.2.+
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/foo.zip') //zip file should be in Module's /libs folder (not the Project's /lib)
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
while gradle does the build it just unzip the file you added preserving its structure.
To use an external jar library
Put the jar into the libs folder/drag the file onto libs from a file explorer
Right click it and select Add as library
Ensure that compile files('libs/your_jar.jar') is in your build.gradle file
To do this, modify build.gradle which is under [projectname]Project -> [projectname] in the project pane on the left.
For me, it was necessary to change
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
to
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile files('libs/universal-image-loader-1.8.5.jar')
}
Click Rebuild Project under the Build menu.
I did this today to get the Universal Image Loader library integrated with my project.
Create a directory called 'jniLibs' into 'app/src/main/' and put inside all the .so
app/src/main/jniLibs/
|---- armeabi-v7a/your.so
|---- armeabi/your.so
|---- x86/your.so
Hmm...I was HOPING that someone will provide a clear example, of
how to make a 3rd-party JAR file accessible to Android-Studio, by
showing the exact SYNTAX of what the resulting 'build.gradle' file's
dependency-clause would look like, after they've added their 'foobar.jar'
entry.
You know, something like:
=========
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'org.somedevs.foobar:Foobar.jar'
}
=========
[Otherwise, I don't stand a snowball's chance in hell of
guessing what the answers posted so far really mean. My
INTITAL clause contained the single 'compile' line...so my
GUESS would that one should add another such line!?!?]
EDIT: Yes, many THANKS, rebelious! I now, too, have it working.
[Instead of the 'drag/drop' onto the 'libs' in Studio, I have more
reliable results by just right-clicking on 'libs' in Studio and choose
"add as library...", after copying the JAR into that location, using cmd-line.]
The correct form for the dependencies clause is the form shown below:
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile files ('libs/Foobar.jar')
}
Due to security reason, it's not possible to reference a local jar/aar file in an application project with the gradle android plugin.
For the support library, with the Android SDK Manager, you have to install the extra named Android Support Repository which will expose the support library inside a maven repository. Then you can add the support library in your project via :
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
For external libraries, you have 2 possibilities :
Build an aar file and deploy it to your local maven repository, then reference it in your project like you did with the android support library.
Put the library sources beside your application project and create a settings.gradle at root which will define the modules. (see the docs for more info).
On my side I would prefer build aar files because it's more modular.
I just installed the new Android Studio and I'm looking for a way to import the support library for Android.
Where is the option for that? In Eclipse that are just two clicks. I googled for it but found nothing. Surely it is too new.
=============UPDATE=============
Since Android Studio introduce a new build system: Gradle. Android developers can now use a simple, declarative DSL to have access to a single, authoritative build that powers both the Android Studio IDE and builds from the command-line.
Edit your build.gradle like this:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 18
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 'com.android.support:support-v4:21.+'
}
NOTES: Use + in compile 'com.android.support:support-v4:21.+' so that gradle can always use the newest version.
==========DEPRECATED==========
Because Android Studio is based on IntelliJ IDEA, so the procedure is just same like on IntelliJ IDEA 12 CE
1.Open Project Structure (Press F4 on PC and Command+; on MAC) on your project).
2.Select Modules on the left pane.
3.Choose your project and you will see Dependencies TAB above the third Column.
4.Click on the plus sign in the bottom. Then a tree-based directory chooser dialog will pop up, navigate to your folder containing android-support-v4.jar, press OK.
5.Press OK.
I no longer work on Android project for a while.
Although the below provides some clue to how an android studio project can be configured, but I can't guarantee it works flawlessly.
In principle, IntelliJ respects the build file and will try to use it to configure the IDE project. It's not true in the other way round, IDE changes normally will not affect the build file.
Since most Android projects are built by Gradle,
it's always a good idea to understand this tool.
I'd suggest referring to #skyfishjy's answer, as it seems to be more updated than this one.
The below is not updated
Although android studio is based on IntelliJ IDEA, at the same time it relies on gradle to build your apk. As of 0.2.3, these two doesn't play nicely in term of configuring from GUI.
As a result, in addition to use the GUI to setup dependencies, it will also require you to edit the build.gradle file manually.
Assuming you have a Test Project > Test structure.
The build.gradle file you're looking for is located at TestProject/Test/build.gradle
Look for the dependencies section, and make sure you have
compile 'com.android.support:support-v4:13.0.+'
Below is an example.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
You can also add 3rd party libraries from the maven repository
compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'
The above snippet will add gson 2.2.4 for you.
In my experiment, it seems that adding the gradle will also setup correct IntelliJ dependencies for you.
This is way more simpler with Maven dependency feature:
Open File -> Project Structure... menu.
Select Modules in the left pane, choose your project's main module in the middle pane and open Dependencies tab in the right pane.
Click the plus sign in the right panel and select "Maven dependency" from the list. A Maven dependency dialog will pop up.
Enter "support-v4" into the search field and click the icon with magnifying glass.
Select "com.google.android:support-v4:r7#jar" from the drop-down list.
Click "OK".
Clean and rebuild your project.
Hope this will help!
You can simply download the library which you want to include and copy it to libs folder of your project. Then select that file (in my case it was android-support-v4 library) right click on it and select "Add as Library"
In Android Studio 1.0, this worked for me :-
Open the build.gradle (Module : app) file and paste this (at the end) :-
dependencies {
compile "com.android.support:appcompat-v7:21.0.+"
}
Note that this dependencies is different from the dependencies inside buildscript in build.gradle (Project)
When you edit the gradle file, a message shows that you must sync the file. Press "Sync now"
Source : https://developer.android.com/tools/support-library/setup.html#add-library
Android no longer downloading the libraries from the SDK manager, it has to be accessed through Google's Maven repository.
You will have to do something similar to this in your build.gradle file:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
dependencies {
...
compile "com.android.support:support-core-utils:27.0.2"
}
Find more details about the setting up process here and about the different support library revisions here.
AndroidX[About]
implementation 'androidx.appcompat:appcompat:1.0.2'