I want to use view pager indicator library in android studio. As it is available for the maven and not for the gradle project yet so I am adding library folder in android studio and including it in app build.gradle file. Problem arises, when I compiled the project so it wont compile as both the app and library gradle files are using android-support-v4.jar files. I need to know how can I use the library in android studio.
settings.gradle
include ':app'
include ':library'
include ':sample'
app/build.gradle
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':library')
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
library.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android-library'
android {
sourceSets {
main {
java.srcDirs = ['src']
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
}
}
compileSdkVersion 19
buildToolsVersion '19.0.3'
}
dependencies {
compile files('libs/android-support-v4.jar')
// compile 'com.android.support:support-v4:19.+'
// compile fileTree(dir: 'libs', include: ['*.jar'])
}
Don't include the support library by adding a direct dependency to its jar file as you have in your library's build.gradle file. If you include it everywhere via the Maven-coordinate-style include, like your commented out line, the build system won't have trouble with duplicates:
dependencies {
compile 'com.android.support:support-v4:19.+'
}
Select "Projects" module in android studio.
download and unzip the ViewPagerIndicator-Library.
Create the libs folder under root folder and copy paste the library into that folder.
go to settings.gradle and add this line:
include ':libs:ViewPagerIndicator-Library'
add the compile project(':libs:ViewPagerIndicator-Library') in app's build.gradle model
Restart the studio and Have Fun ...
Related
I created an Android library named "core" that use the Logger library (https://github.com/orhanobut/logger).
Here its build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
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'])
compile 'com.orhanobut:logger:1.15'
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
}
Then I build a .aar of core library.
I add this library as a dependency into my application, by putting the .aarcore files in the libs folder.
That's the build.gradle of my application :
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.package.test"
minSdkVersion 16
targetSdkVersion 25
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile 'com.mypackage:core:1.0#aar'
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'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:multidex:1.0.1'
}
As you can see I have multidex activated.
It compile just fine but at runtime, at the first call to Logger I get an exception:
stack=java.lang.NoClassDefFoundError: Failed resolution of: Lcom/orhanobut/logger/Logger;
Even if I set transitive = true
compile (com.mypackage:core:1.0#aar) {
transitive=true
}
it doesn't work.
Thank you !
I had to do a couple of things in concert to get it to work.
1) In your library project
Add the following to your project-level gradle file:
buildscript {
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}
Be sure to use the right version of the plugin for your gradle version, check here.
Add the following to your module-level gradle file:
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.YourPackage'// Doesn't have to be github, just an example
Also in this file, make sure your dependencies (the ones you want to be transitive) use api instead of implementation if you are using Gradle 3.4+. If you are using a version of Gradle <3.4, compile is the way to go. Example:
dependencies {
api 'com.google.android.gms:play-services-location:15.0.1'
}
2) In your app project (which uses the library)
Add the following to your module-level gradle file:
dependencies {
implementation('com.github.YourPackage:yourRepo:version#aar') {
transitive=true
}
}
Add the following to you project-level gradle file (but this will depend on where your library is served from, for me it's jitpack):
allprojects {
repositories {
...
maven {
url 'https://jitpack.io'
credentials { username 'yourAuthKey' }// Only for private repositories
}
}
}
Note: You shouldn't add the auth key as a string in build.gradle, put it in a property in your gradle.properties file.
Note 2: JitPack allows you to use for example development-SNAPSHOT as a version number for a gradle dependency. Android Studio caches these dependencies and will not re-download it when you push to your development branch. To overcome this, use commit hashes as version numbers during development or clear (delete) the cache files, located on Windows at ~/.gradle/caches/modules-2/metadata-x.xx/descriptors/com.github.YourPackage/yourRepo. (Needles to say, I learned this the hard way).
I have started with greendao 3.2. I added greendao jar in libs folder and added it as library. I generated my entities using greendao-generator. It generates entities but the generated entities java some annotations and android studio is giving error on these annotations.It is giving an error cannot find "org.greenrobot.greendao.annotation.*". How to resolve this?
Probably you didn't add the library in your app module . First of all rather than using jar , you can use gradle dependency . In your generator project add the following dependency
compile 'org.greenrobot:greendao-generator:3.2.0'
So the gradle file of your generator module , should be look like this
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.greenrobot:greendao-generator:3.2.0'
}
And in your gradle file (module app) add the following
compile 'org.greenrobot:greendao:3.2.0'
Your main gradle file (module app) should be like this
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.tcs.a1003548.greendao"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'org.greenrobot:greendao:3.2.0'
}
Why are you putting .jar in libs folder and not using Gradle for dependency management? This quickly leads to dependency hell. It is a lot easier.
Inside main build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
}
}
Inside application build.gradle:
apply plugin: 'org.greenrobot.greendao'
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
}
I have a project in Eclipse with gradlle.
I want to run the project on a physica device, but I do not see the option.
I select run tab, project just runs gradle.
Check below build.gradle file
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile files ('libs/twitter4j-core-4.0.4.jar')
compile files ('libs/gson-2.2.2.jar')
compile files ('libs/agilio_rtmp-debug.jar')
compile files ('libs/facebook-android-sdk-4.14.1.jar')
compile files ('libs/agilio_rtmp-debug.jar')
}
When I select run:
I want to run the project on a physical device.
The task you want is app:installDebug, but that is assuming you have the correct project structure.
Since it isn't clear what the structure your project has, here is the recommended structure from the Android Gradle documentation.
settings.gradle
build.gradle # Top-level
app/ # A module named 'app'
build.gradle # Module-level
libs/
library-1.jar
library-2.jar
...
src/main/
AndroidManifest.xml
java/ # application package in here, then Java files in that
res/
This is the settings.gradle
include ':app'
Here is a top-level build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
allprojects {
repositories {
jcenter()
}
}
Then, the module build.gradle with adjustments for your dependencies.
Most importantly - The top line here tells Gradle this is an android app, not a java app. This means you can run the various Android-related Gradle tasks like installDebug, which is the one you are looking for.
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "xxxxxxxx"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
ext {
// Variables to keep libraries consistent
supportLibrary = '23+'
// Support Libraries dependencies
supportDependencies = [
design : "com.android.support:design:${supportLibrary}",
appCompatV7 : "com.android.support:appcompat-v7:${supportLibrary}"
]
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
// This line already compiles all jar files in the libs/ directory
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
// recommended
compile supportDependencies.design
}
I create a project on IntelliJ IDEA using Gradle and I'm trying to add AndroidSlidingUpPanel to it (https://github.com/umano/AndroidSlidingUpPanel).
What exactly do I have to do to configure it?
I have the following folder structure:
RootFolder
|_libraries
|_ AndroidSlidingUpPanel
|_ library
|_ build.gradle
|_ settings.gradle
|_ build.gradle
|_app
|_ build.gradle
|_ settings.gradle
|_ build.gradle
The files are like this:
RootFolder/settings.gradle
include ':app'
include ':libraries:AndroidSlidingUpPanel:library'
RootFolder/build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
RootFolder/libraries/AndroidSlidingUpPanel/settings.gradle
include ':library'
include ':demo'
RootFolder/libraries/AndroidSlidingUpPanel/build.gradle
allprojects {
group 'com.sothree.slidinguppanel'
version '1.0.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
}
}
dependencies {
repositories {
mavenCentral()
}
}
}
RootFolder/libraries/AndroidSlidingUpPanel/library/build.gradle
apply plugin: 'android-library'
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
RootFolder/app/build.gradle
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.2"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:gridlayout-v7:19.0.1'
compile 'com.android.support:support-v4:19.0.1'
compile 'com.android.support:appcompat-v7:19.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You aren't clear about what build errors you may be having or what your exact question is, though since you haven't got a dependency to SlidingUpPanel in your app/build.gradle, the question is how to include it?
If so, the first thing to bear in mind is that a project can't have more than one settings.gradle. To be more precise, it only looks in the root folder for it and uses it if it's there. So the settings.gradle in RootFolder/libraries/AndroidSlidingUpPanel/ will be ignored. It looks like you've properly set up an include to the SlidingUpPanel library in RootFolder/settings.gradle, so that's good:
include ':libraries:AndroidSlidingUpPanel:library'
to include a dependency on it in your app, you use a compile project statement and give it the same path:
dependencies {
compile 'com.android.support:gridlayout-v7:19.0.1'
compile 'com.android.support:support-v4:19.0.1'
compile 'com.android.support:appcompat-v7:19.0.1'
compile project(':libraries:AndroidSlidingUpPanel:library')
compile fileTree(dir: 'libs', include: ['*.jar'])
}
If you're using Android Studio (it should also work with Gradle-based projects in IntelliJ Community Edition), you can set up dependencies on modules from the Project Structure dialog via Modules > Your module > Dependencies > + > Module dependency:
If you've added the module to your settings.gradle file as you already have and have synced up your project to the Gradle files, then the SlidingUpPanel module should appear in the Project view in the IDE and should appear as a module choice in that dependency list.
Here is my build.gradle file
when I compile it always says 'cannot find symbol class HtmlFetcher' which is inside the de.jetwick.snacktory dependency
jar file of snacktory library is downloaded into local gradle cache
but it is not including it into project.
what might be a problem?
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {url "https://github.com/karussell/mvnrepo/raw/master/releases"}
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:+'
compile 'de.jetwick:snacktory:1.1'
compile fileTree(dir: 'libs', include: '*.jar')
}
Upgrade to Android Studio 0.4.3. There are bugs with adding dependencies that have been fixed since 0.4.0.
compile fileTree(dir: 'libs', include: '*.jar')
That is the same line I use to compile my libs directory. I'm not sure if it's the version, as I've been using the above gradle line since 0.3 or so, and it's worked fine.
The only other thing to check would be the location of your libs folder.
Your libs folder needs to be inside of your Module.
Android studio projects are the same as IntelliJ projects meaning that the directory structure should be: ProjectName/ModuleName/libs
That means you need a libs directory on the same level as your src directory and build.gradle file. Double check it's in the right location.