I'm trying to set up Double Espresso, but that's probably not relevant here. What I'm trying to do is to set up a project in Android Studio using Gradle.
I'm very new to Gradle and build tools in general, though I've successfully used Maven before. Despite an hour of searching I can't find an answer to a very simple question.
In Jake Wharton's instructions it says
No more fumbling with local jars or dependency conflicts. Pull it in with one line:
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r3'
Pull in where? Where do I put/execute that command to import the project? In the command prompt? Do I put it in one of the scripts?
Thanks for any help.
You have a build.gradle file inside your app folder. In that file you can configure your project, "dependencies" and other options. It's very similar to maven. You have another build.gradle file in your root folder from your project. This conf file is more general and call the other build.gradle file.
e.g I have in one of my projects
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.acostela.example"
minSdkVersion 17
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:20.+'
compile "com.android.support:gridlayout-v7:18.0.+"
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'commons-net:commons-net:3.3'
compile 'net.sf.opencsv:opencsv:2.3'
}
Dependencies here are similar to maven and the use in that tool of "/".
Gradle take libs from repositories in the same way of maven. In fact you can use the maven repository. You have a tab with the gradle sentence to download libraries.
http://mvnrepository.com/artifact/com.squareup.assertj/assertj-android/1.0.0
Related
i was working on a project, everything was fine but when i tried to run the app, i couldn't,i guess because i'm using a bundled version of google play services. so i i made sure that i update android studio and install the lastest google repository.
but it still wouldn't work. And when i click on install Repostiroy and sync project, still nothing happens.
please help me : this is my gradle ;
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.nefissa.pfe2"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.google.android.gms:play-services:25.1.0'
testCompile 'junit:junit:4.12'
}
please where did i go wrong?
a screenshot;please what can i do? the click on install wouldn't work
replace it with compile 'com.google.android.gms:play-services:10.2.0'
and add apply plugin: 'com.google.gms.google-services' on the bottom of your gradle,
and make sure you added classpath 'com.google.gms:google-services:3.0.0'
in the Top-level build.gradle file
There might one of these problems can cause this issue, try any one of them.
Solution 1:
Try to updated the gradle plugin.
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
Then just Build->Clean project and everything works again.
Solution 2:
Go to the main module in your project, it's usually have the name app.
Then go to > Open Module Settings > in Properties change the Build Tools Version to 25.0.1
Then to make sure open the build.gradle file, and change compileSDKVersion & buildToolsVersion to 25 and 25.0.1 respectively.
If the above solution doesnt work.
Solution 3:
From this answer
"build cache" is enabled by default in build Android Plugin since 2.3.0. It creates files in build cache outside of project folder (e.g. in \Users\%username%.android\build-cache). Theese files are intended to be common between your projects.
And then Android Studio is unable to navigate in theese files.
You can disable Build Cache in gradle.properties file. just add android.enableBuildCache=false
I got a project which I imported from Eclipse to Android Studio. In Eclipse everything worked well.
It contains a main module (a project in Eclipse) which uses packages from a library module (library project in Eclipse). Since the migration did not went well, I have created a library module manually and just copied all the source code to the newly created module.
The problem is that the main module doesn't seem to find the packages from the library module and when I rebuild the project I get errors like "package bla bla does not exist".
Here is the main module gradle.build:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.pointer.mamagoose"
minSdkVersion 9
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':linphoneclean')
compile 'com.android.support:support-v4:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.google.android.gms:play-services:9.4.0'
compile files('libs/firebase-client-android-2.5.0.jar')
compile files('libs/apache-httpcomponents-httpclient.jar')
compile files('libs/apache-httpcomponents-httpcore.jar')
compile files('libs/android-support-v7-recyclerview.jar')
}
linphoneclean is the library module.
The entire project's settings.gradle:
include ':linphoneclean'
include ':tigris'
This is the build.gradle of the library module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.6.2'
compile files('libs/commons-lang3-3.4.jar')
compile files('libs/linphone.jar')
compile files('libs/firebase-client-android-2.5.0.jar')
}
The structure of the library module includes for example folder:
src/main/com/pointer/linphone (and inside there are all the java files with a deceleration of package com.pointer.linphone, Yet I still get an error saying >"package com.pointer.linphone does not exist).
What am I doing wrong?
After fighting with the same issue for hours here is what worked for me.
I've created a fresh project with blank activity, added a library module with a dummy class, defined the dependency. Verified that it works by importing the dummy class in the app. Then I copied all my relevant code from the real project.
My thinking is that it was probably issue with IDE's iml files, since starting from scratch and copying stuff over worked.
See properly source file contains both java folder and res folder,add java files in java packages and res in res folder.Add Activity name in Manifest file see here ,add necessary libraries files in Gradle file.Clean and rebuild the project in the Android studio.
https://developer.android.com/guide/topics/manifest/manifest-element.html
Maybe you can check if the library's AndroidManifest has defined the package correctly, like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.pointer.linphone">
I am trying to build an app which is composed out of separate library projects.
To do this, I'm trying to make a proof of concept which is supposed to be as following:
I tried to keep the project as simple as possible. The projects contents do not matter!
All that matters is the dependencies between the projects!
The result should be that MainProject will print out Something Another String!
I have tried all from .JAR files to .AAR files, but the best I got was
with the dependency in red. I added the StringExtender.aar file to StringReturner, and then the StringReturner.aar file to the MainProject.
When I do this I get the following Exception:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/erik/stringextender/StringExtender;
What is the right way to setup a simple proof of concept like this? I can't seem to find anything related to a library project having a dependency. It's all 1 level deep!
Any help is welcome!
EDIT SHOWING GRADLE BUILD FILES
StringReturner:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
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:23.2.1'
compile project(':StringExtender-lib-debug')
}
MainProject:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.erik.erikpoc10"
minSdkVersion 15
targetSdkVersion 23
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:23.2.1'
compile 'com.android.support:design:23.2.1'
compile project(':StringReturnerLib-debug')
}
Use the following tutorial - Creating libraries for Android applications
See a working example project on my Github
The most important steps taken were:
8.3. Create library module.
8.6. Define dependency to the library project.
Do not forget to import the library class you want to use, for example:
In MainProject: import com.example.stringreturner.StringReturner.
In StringReturner: import com.example.stringreturner.StringExtender.
The info below is based on the image you provided:
The library methods are not static so don't forget to make an actual object.
So StringReturner should make a StringExtender object, And MainProject should make a StringReturner object first!
And finally, I think it's a typo but both libraries have the class StringReturner. This will not work in the StringReturner library for obvious reasons.
I should also note that I used Android Studio 2.0 and I did not touch .JAR files nor .AAR files. I merely created two library modules and one app. Then configured the Project Structure -> Dependencies by adding Module Dependencies.
We are moving our years old Android project to Android Studio and away from eclipse.
The problem is that we have some jar files that use code specific to the devices that we work on. (I know! Don't say it. I'm working with the whole team to move past this.) Since these JAR files override some of the bluetooth code, they have to go ahead of the SDK in the dependency list for the thing to build. (These libraries are for compile time only.)
While on eclipse, Android SDK was a full blown plugin and I could control the libraries absolutely and make sure these libraries were first. With Android Studio, I don't seem to be able to control the position of the SDK in the dependency path.
I have edited the app.iml file and changed the order of the orderEntry tags at the bottom. This appears to have fixed the problem inside the IDE and it appears to be building fine now.
I am still unable to build the project using gradle.
Based on feedback here, here is my current build.gradle
apply plugin: 'com.android.application'
configurations { razrBuild }
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
razrBuild fileTree(dir:'assets',include: ['*.jar'])
provided files('assets/BluetoothGattService.jar')
provided files('assets/BluetoothGatt.jar')
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.1.1'
compile files('libs/android-support-v4.jar')
compile files('libs/myapp-util.jar')
}
project.afterEvaluate {
project.android.applicationVariants.all { v ->
v.javaCompile.classpath += configurations.razrBuild
}
}
Any ideas?
Put jars you want to compile time only into a folder(For my example : extlib)
Then add some configurations to build.gradle
configurations { providedCompile }
dependencies {
providedCompile fileTree(dir:'extlib',include:['*.jar'])
}
project.afterEvaluate {
project.android.applicationVariants.all { v ->
v.javaCompile.classpath += configurations.providedCompile
}
}
This question is a sequel to this one.
In an existing Eclipse solution I have a library project which is used as a Android Dependency in the main app project. That means that if I edit this library, and compile, the main project is updated with the latest from that library.
I'm trying to do the same in Android Studio. That means not importing a library, but using it as a reference or dependency in the app project. I've been trying for two days now with no luck. Help is HIGHLY appreciated.
build.gradle (of the app project - there is nothing interesting her as I didn't change it much)
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
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:21.0.3'
}
You can try this steps:
Download library
File/import module
Select your library and rename :LibraryName
Go to build.gradle and write.
build.gradle
dependencies {
......
compile project(':LibraryName')
.....
}
Then if you import module you can edit library and proyect its updated
Say you have 2 projects, 1 application and 1 library project, you want to push all your external dependencies to your library project, so your application project only depends on the library project:
settings.gradle
include ':app'
include ':library'
app/build.gradle
apply plugin: 'com.android.application'
dependencies {
compile project(':library')
}
library/build.gradle
apply plugin: 'android-library'
dependencies {
compile 'external-dependencies-1'
compile 'external-dependencies-2'
...
}