I am new to Gradle. I have been looking for a solution to my problem for several hours now but cannot get my build to work correctly. Basically I have an Android project that I am trying to build using Gradle. I have a libs directory in my project that contains dependencies. It appears the build knows that they are there because it uses them when compiling the application. However, when the apk is assembled the dependencies are not in the apk. I am trying to get this to generate an apk file that is similar to the apk that is generated when you perform an Export from MyEclipse. The apk generated from the MyEclipse export has a lib directory as well as an org directory so I know the dependencies are in that apk. The apk generated from Gradle has neither. Here is my build.gradle file.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 17
buildToolsVersion "23.0.2"
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Related
I am using android studio with Gradle as build system. I have a library project and application project.
Library - library project.
Application - application project
Gradle and android studio generates .aar files instead of .jar files.
So I wanted to add generated .aar file as a dependency to my application project.
Since there is no support from android studio and gradle wrapper to add .aar files as a dependency for application project, I have tried to upload the .aar file to local maven repository.
The build.gradle code to upload the .aar file to local maven repository is below.
apply plugin: 'maven'
apply plugin: 'android-library'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
repositories {
mavenLocal()
}
configurations {
resultArchives
}
uploadResultArchives {
repositories {
mavenDeployer {
repository(url: 'C:/Users/test/.m2/repository')
}
}
}
artifacts{
resultArchives file: file('build/output/aar/Library-debug.aar')
}
Setup info: Android studio1.1.0
Gradle wrapper: 2.2.1
Thanks in advance for your help!!!
I'm not a gradle pro. But I think this will work:
group = 'Library-debug'
uploadArchives{
repositories{
mavenDeployer{
repository(url: uri('C:/Users/test/.m2/repository'))
}
}
}
Your aar will be available on "C:/Users/test/.m2/repository/[your package name]/Local-debug.aar"
Using this guide I want to build an existing project in Eclipse with Gradle.
build.grale contains:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.0'
}
}
apply plugin: 'android'
android {
buildToolsVersion "19.1.0"
compileSdkVersion 16
}
repositories {
mavenCentral()
}
dependencies {
compile files('libs/android-support-v4.jar')
}
But I get:
How to resolve this issue? I tried for several hours different approaches but nothing seemed to work.
The default project structure has changed, so unless you either tell the Gradle plugin where to find your manifest (and the rest of your code) or switch to the new structure, the Gradle plugin will look in the wrong place.
In your case, it is looking for the manifest at\src\main\AndroidManifest.xml, which is the default for new Gradle projects. The old project structure (used by Eclipse + ADT) puts the manifest in the root of your project at \AndroidManifest.xml.
You can specify an alternate location in your build.gradle using a sourceSets closure like so:
android {
// ...
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
This will configure the Android Gradle plugin to use the old project structure for your manifest, Java sources, and resources.
If you use Android Studio's import tool, it should take care of all of this for you.
That guide has nothing to do with building an existing Eclipse project with Gradle. The word "Eclipse" appears nowhere in the guide.
If you are trying to move to Android Studio, use the Android Studio project import wizard.
If you are trying to use Eclipse, but also offer Gradle builds, you can run the Eclipse export wizard to generate a build.gradle file, though it will require some additional tweaking, as that wizard has not been updated in ages.
Or, start with this build.gradle file and adjust to suit:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
dependencies {
compile 'com.android.support:support-v4:21.0.0'
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
I'm trying to import an Android project build on Eclipse into my Android Studio IDE. I'm following the steps to import properly the project with a build.gradle file but when I'm trying to compile, I get this weird error: Plugin with id 'android' not found. I have this version of Android Studio: Android Studio (Preview) 0.4.3. Here is my build.gradle:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:DesignDialogLibrary')
compile project(':workspace:HoloColorPickerLib')
compile project(':workspace:MbAdBleamSdkReaderLibrary')
compile project(':workspace:ViewPagerLibrary')
compile project(':workspace:PullToRefreshLibrary')
compile project(':workspace:SlidingMenuLib')
}
android {
compileSdkVersion 17
buildToolsVersion "18.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
You're missing this block in your build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
This block tells Gradle where to find the "android" plugin that you're missing.
It's standard in projects created in newer versions of Android Studio to put this in the build.gradle that's in your project's root directory and omit it from the one that's in your module's directory, so look there and see if looks like it's in order.
If you don't have that root-level build.gradle file and you've only got one directory and one build file, then add this block to that file. Be aware, however, that Android Studio currently has a few bugs that prevent the Project Structure dialog from working properly with projects in this single-module format, though I believe they'll otherwise build and run fine.
For two days now im trying to create 2 different flavors in my Android app.
I imported my app to Android studio from Eclipse.
I created another 2 res folders (res_testing and res_distribution) and another 2 src folders (src_testing and src_distribution).
This is how my build.gradle looks like:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
task wrapper(type: Wrapper) {
gradleVersion = '1.6'
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "18.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
productFlavors {
flavor1 {
packageName = 'com.comp.4u.testing'
}
flavor2 {
packageName = 'com.comp.4u'
}
}
android.sourceSets.flavor1{
java.srcDirs = ['src_testing']
resources.srcDirs = ['res_testing']
}
android.sourceSets.flavor2{
java.srcDirs = ['src_distribution']
resources.srcDirs = ['res_distribution']
}
instrumentTest.setRoot('tests')
}
}
But when i change the Build Varients, Although the app builds ok, still its using the same DataManager file as in the main src folder (Though i do get two different packages according to the names i gave them).
Its driving me crazy!!
I looked in 100 forums and posts, with no luck.
Each DataManager is using a different url to the server. This is the only thing i want.
I had a similar problem.
What happens when you remove the data from your main folder that is overriden in your flavors?
Do you get the error that the url is not found? If yes, than I think its the same error I get.
I solved this problem by creating a new project and copying all files to it.
I think that there are still some problems by exporting projects from eclipse and importing it to as.
I'm new to gradle and trying to figure out how to set it up on our Jenkins build machine. Our app builds perfectly from within Android Studio, but it doesn't ever generate a build.gradle file, which I believe I need in order to run the gradle build on Jenkins.
Any help appreciated!
I ended up creating these build files from scratch.
Here's some guidelines:
If you have a project with multiple modules, create a settings.gradle file at the root of your project (at the same level as all the modules). Your settings.gradle file should look something like this:
include 'ActionBarSherlock'
include ':facebook-android-sdk-3.0.1:facebook'
include 'google-play-services_lib'
include ':SlidingMenu-master:library'
include 'Tinder'
include 'ViewPagerIndicator'
include 'volley'
include 'windowed-seek-bar'
Within each module, create a build.gradle file. For library projects, your build.gradle file should look something like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
android {
buildToolsVersion "17.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
For your main project, your build.gradle file should look like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
dependencies {
compile project(':facebook-android-sdk-3.0.1:facebook')
compile project(':google-play-services_lib')
compile project(':SlidingMenu-master:library')
compile project(':ViewPagerIndicator')
compile project(':volley')
compile project(':windowed-seek-bar')
compile files('compile-libs/androidannotations-2.7.1.jar', 'libs/Flurry_3.2.1.jar', 'libs/google-play-services.jar', 'libs/gson-2.2.4.jar', 'libs/picasso-1.1.1.jar')
}
android {
buildToolsVersion "17.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}