Include generated Jar and aar as a dependency in Android project - android

I have a AndroidStudioSupport Project which has modules.
1. module 1 takes care of Android UI part
2. module 2 is a library project which generates .aar file
3. module 3 is a java project which generates jar file
Now I want to build the project in a way that
1. First build jar project
2. Then copy generated jar into libs of library project as a dependency
3. Then build aar file
4. Then copy generated aar into libs of UI project as a dependency
5. Finally generate .apk file
Please suggest me what changes do I have to do in my gradle files
jar project gradle file
apply plugin: 'java'
sourceSets {
main.java.srcDirs = ['src/main/java']
main.resources.srcDirs = ['src/main/java']
test.java.srcDirs = ['tests/java']
test.resources.srcDirs = ['tests/resources']
}
jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it)}
}
}
library project gradle file
apply plugin: 'com.android.library'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':java_project')
}
task copyToLib(type: Copy) {
from configurations.compile
into 'libs'
}
build.dependsOn(copyToLib)
android {
publishNonDefault true
compileSdkVersion 21
buildToolsVersion '23.0.2'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/java']
renderscript.srcDirs = ['src/main/java']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('test')
}
buildTypes {
debug {
jniDebuggable true
}
}
}
This current implementation doesn't include jar from my java project

Related

How to use buildship for android?

I have a dummy project, which I succeed to compile through buildship plugin in eclipse IDE.
This is my local.properties file:
sdk.dir=C:/Asta/altro/adt-bundle/sdk
This is settings.gradle file
rootProject.name = 'testgradle'
This is my build.gradle file
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.testgradle"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
lintOptions {
abortOnError false
}
}
sourceCompatibility = 1.6
targetCompatibility = 1.6
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.10'
}
Despite I got the apk fully compiled, eclipse is not integrated: still seeing missing libraries and giving more than a 100 errors! All the libs are perfectly managed by gradle in \build\intermediates\ and assembled into apk, but the eclipse IDE is not "live". I would like to use gradle to download and explode the libraries and then to inform eclipse and let it to make the apk with its own builder.
Buildship can be used only to run Android tasks (assembleDebug).
During the build process it will load dependencies and tell if there're some errors.
To load dependencies into Java classpath, to see errors in Eclipse and resolve imports you can either manually add .jar files to your Java Build Path or use this Gradle plugin: https://github.com/greensopinion/gradle-android-eclipse.
It generates classpath for Eclipse project and you just import it into the IDE.
To run Gradle tasks you can create a Run configuration (named "Gradle Project") and put there your task and Working directory.

Android Studio - java.lang.RuntimeException: Unable to instantiate application

I am working on a project in eclipse, but after sometime it giving me error like "Unable to execute dex: method ID not in" as single DEX file having 65k method limitations.
To overcome the issue I come into the picture of Gradle plugin v0.14.0 for Android adds support for Multi-Dex. So I moved to Android Studio.
I have updated SDK and exported project from eclipse as per Migrating from Eclipse.
Now I have project structure required in Android Studio.Their is dependency of project on ABS, Google Play Service lib and android multidex support lib. I have added these libraries to my project folder named "libraries".
Project Gradle file looks something below:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.1'
}
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':libraries:ABS')
compile project(':libraries:GooglePlayServiceLib')
compile (project(':libraries:android-support-multidex')) {
exclude group: 'com.android.support', module: 'support-v4'
}
compile "com.android.support:support-v4:21.0.0"
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
dexOptions {
preDexLibraries = false
}
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')
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
}
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = ['--multi-dex']
} else {
dx.additionalParameters += '--multi-dex'
}
}
}
When I clean and build project it builds successfully. But when I run project as android application giving me following error:
java.lang.RuntimeException: Unable to instantiate application java.lang.ClassNotFoundException: Didn't find class on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.example-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example-2, /vendor/lib, /system/lib]]
I am thinking that application is not compiling AndroidManifest.xml OR I am something missing ?
Any help is going to be appreciated. :)

Android Gradle - Building library module generates unexpected .aar

I am currently working on setting up a multi-module project, with the following structure:
Project Root
|- app/
|- src/
|- build.gradle
|- lib
|- src/
|- build.gradle
|- build.gradle
|- gradle.properties
|- settings.gradle
While the library module builds as expected, I get the following build error for the app module:
A problem was found with the configuration of task ':app:prepareProjectRootLib10Library'.
> File 'C:\projects\Project Root\lib\build\outputs\aar\lib-1.0.aar' specified for property 'bundle' does not exist.
As far as my build script goes, I do not think I am doing anything wrong with my build scripts. At the same time, when I look for the generate .aar for my library project, I instead find a lib-1.0-SNAPSHOT.aar file instead, which explains the problem. While manually renaming that file to match what the build script expects fixes the issue, I need a solution that does require manual work. Is there a way to modify the build script such that the application module can get the library project files it needs?
If it helps, below is app/build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
minSdkVersion 16
targetSdkVersion 20
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
androidTest {
java.srcDirs = ['src/androidTest/java']
resources.srcDirs = ['src/androidTest/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile project(':lib')
compile 'com.android.support:support-v4:20.0.0'
}
And this is lib/build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
minSdkVersion 16
targetSdkVersion 20
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
androidTest {
java.srcDirs = ['src/androidTest/java']
resources.srcDirs = ['src/androidTest/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile 'com.android.support:support-v4:20.0.0'
}

Gradle compile error "No Signature of Method"

When I try to run ./gradlew build.gradle from terminal, I have been getting following error:
> No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (org.gradle.api.internal.project.DefaultProject_Decorated) values: [project ':TestMobile']
Possible solutions: module(java.lang.Object)
I have tried applying java plugin at root build.gradle file and also on sub projects. but still I get same error.
I have the following project structure:
ProjectRepos
->build.gradle
->settings.gradle
->TestMobile (Android plugin)
->build.gradle
->ThirdParty/SlidingMenu (Android library)
->build.gradle
Where top level build.gradle contains:
// Top-level build file where you can add configuration options common to all sub- projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
compile project(':TestMobile')
compile project(':ThirdParty:SlidingMenu')
}
}
settings.gradle contains following:
include ':ThirdParty:SlidingMenu'
include ':TestMobile'
TestMobile -> build.gradle contains following:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':ThirdParty:SlidingMenu')
}
android {
compileSdkVersion 18
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']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
ThirdParty/SlidingMenu -> build.gradle contains following:
apply plugin: 'android-library'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
sourceSets {
main {
......
......
}
}
You can't have compile dependencies in the buildscript block, only classpath dependencies. Also you can't have project dependencies there. You probably just need to get rid of these two lines.
For me, it was a simple syntax error (missing commas) when adding a string resource:
Incorrect:
resValue "string" "app_name" "My App Dev"
Correct:
resValue "string", "app_name", "My App Dev"

built flavors of a project

I want to build different flavors of a project (only the res folders have different contents), but it doesn't work.
So here is my build.gradle file (like in this question Custom old Android project structure in Gradle):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project('<path>:actionbarsherlock')
}
android {
compileSdkVersion 17
buildToolsVersion "17.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']
}
}
buildTypes {
debug {
packageNameSuffix ".debug"
}
release{
packageNameSuffix ".release"
}
android.sourceSets.flavor1 {
res.srcDir = ['res_flavor1']
}
android.sourceSets.flavor2 {
res.srcDir = ['res_flavor2']
}
productFlavors {
flavor1 {
packageName "androidstudio.test.flavor1"
}
flavor2 {
packageName "androidstudio.test.flavor2"
}
}
}
}
Structure of my folders (I want to change just one layout for each flavor):
my_project
src
res
res_flavor1
res_flavor2
I don't see any changes in the build variants, so when I export an apk its of the main directory (androidstudio.test).
Whats wrong with my file?
My Android Studio Version: 0.1.3.
If you need more information, say what and I will post it.
Thanks!
[EDIT]
#Greg:
I changed the res-folder in my question above, but I still get just two APKs in my project "out" folder (path: "out/production/androidstudio.test"): androidstudio.test.apk and androidstudio.test.unaligned.apk (the project and module name is: android.studio - I edited this names in my question above, otherwise its a bit confusing :)).
The different buildTypes should be generated automatically, shouldn't they? And to generate them I go to: Run -> Run "androidstudio.test".
I also looked in the log files of android-studio, but there is no error.
Here is a Screenshot of my Package Explorer, perhaps I made here a mistake?
I really don't understand why the flavors aren't build.
You want to be setting res.srcDirs = ['res_flavor1'] instead of resources.srcDir = ['res_flavor1']. The naming is a bit confusing, but the "resources" directory isn't the same as the Android "res" directory.

Categories

Resources