built flavors of a project - android

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.

Related

Include generated Jar and aar as a dependency in Android project

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

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.

Gradle DSL Method not found: 'multiDexEnabled'; using gradle that does not contain the method

This question has been asked before, however, I have followed the directions carefully and am still receiving this issue. I need some help with figuring out what I am missing with my particular situation. Here is what I have done.
1) Add android-support-multidex.jar: Added android-support-multidex.jar to projected libs folder
2) Edit project build.gradle: I modified my build.gradle file located in my project's folder.
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
.....
compile 'com.android.support:multidex:1.0.0'
}
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.mypackagename"
minSdkVersion 14
targetSdkVersion 21
// Enabling multidex support.
multiDexEnabled true
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.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']
}
// 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')
}
}
3) Modify Project Application class: I have a class that extends Application. I added the following to it.
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
This was originally an Eclipse project. I exported the Eclipse project, and then imported the build.gradle file in Android Studio. I have not been able to successfully run the project since. Any help with steps I have missed would be appreciated. Thanks in advance!
Edit: Here is the contents of my top-level build file
// 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.12.+'
}
}
Replace:
classpath 'com.android.tools.build:gradle:0.12.+'
with:
classpath 'com.android.tools.build:gradle:1.0.0'
as multidex support was not offered in that 7-month-old beta release of the Gradle for Android plugin.

"No tests were found" for Android Studio

I'm just migrated from Eclipse. My project keeps the old structure like below
projRoot\
src\
res\
...
AndroidManifest.xml
tests
build.gradle
Here's content of build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.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']
}
androidTest.setRoot('tests/src')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
defaultConfig {
testApplicationId 'com.xxx.yyy.test'
testInstrumentationRunner 'android.test.InstrumentationTestRunner'
testHandleProfiling true
testFunctionalTest true
}
}
I'm on Android Studio 0.8 beta, Gradle 1.12, I tried every possible solution found all over SO and google without any success.
Anyone can help out? Thanks.
In addition, where can I know the current version of Android Gradle plugin?
For test source set structure, follow this link. It is mentioned that test source sets should be setup as follows:
for local unit tests: module-name/src/test/java/
for android instrumentation tests: module-name/src/androidTest/java/
In case it's still doesn't work. Android Studio may encounter sometimes issues with the recognition of the tests. And the workaround would be to run local tests by using the command line:
./gradlew testVariantNameUnitTest
Once the command is run. You can go back to Android Studio and run the tests and it should work. You can find more on running tests using command line on this link.
Hope this helps.

Gradle + AndroidAnnotations generates duplicate class errors - need to clean project before every build

I have a problem with migrating my IntelliJ IDEA project to Android Studio using a gradle build. I have set up the AndroidAnnotations library like recommended in various other posts, and it works just fine. But, when compiling my project multiple times without executing the :clean task in between, I get following error messages:
/project-dir/build/source/apt_generated/flavor1/release/com/example/app/MyActivity_.java:15: error: duplicate class: com.example.app.MyActivity_
[more errors here...]
I believe that multiple builds in series fail because AndroidAnnotations re-creates the *_.java files always before the :compile tasks (without checking whether it is necessary or not) and the :compile task recognizes the new files (e.g using timestamp) but already finds those as pre-compiled *.class files, thus throwing the error. Is this possible? How can I prevent this behavior? Can I add a necessity-check for AndroidAnnotations? Or is this some other problem?
UPDATE 1: It seems that the error is thrown from AndroidAnnotations itself, since :compile works when I delete the *.java files inside the apt_generated folder manually.
UPDATE 2:
I removed following line from my build.gradle:
// Automatically add the generated source code to the source set
android.sourceSets[getSourceSetName(variant)].java.srcDirs += aptOutput
I don't exactly know why it works without this line. Since I did not add the folder using Android Studio's Mark Directory as > Sources Root. Probably this is some result of caching? Or does gradle somehow add my generated java files automatically to the classpath?
I would be thankful for any comments nonetheless.
UPDATE 3 / SOLUTION
After removing the line and synchronizing the gradle build file with Android Studio, the auto-generated source code was removed as Source Root, causing the IDE to show errors of missing classes.
Eventually, I found the solution on the Android Annotations github issues: https://github.com/excilys/androidannotations/issues/676
I re-added the statement for adding it to the source sets (allowing Android Studio to show it as source root). Furthermore, I removed the files from the variants source collection using this:
variant.javaCompile.source = variant.javaCompile.source.filter { p ->
return !p.getPath().startsWith(aptOutputDir.getPath())
}
Now the generated files are recognized, and the duplicate class error is gone.
Best regards,
David
Here is my final build.gradle. I hope this helps some of you:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
configurations {
// This is the annotations processor dependency configuration.
apt
}
def getSourceSetName(variant) {
return new File(variant.dirName).getName();
}
android {
compileSdkVersion 18
defaultConfig {
versionCode 10
versionName "1.0.2"
targetSdkVersion 17
minSdkVersion 10
}
buildToolsVersion "18.0.1"
buildTypes {
release {
zipAlign true
}
}
productFlavors {
flavor1 {}
flavor2 {}
}
// This has to go after the productFlavors command (otherwise moving the flavor source set root fails).
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// We move the root of our flavors to support our legacy structure.
flavor1.setRoot('flavors/flavor1')
flavor2.setRoot('flavors/flavor2')
}
applicationVariants.all { variant ->
def aptOutputDir = project.file("${project.buildDir}/source/apt_generated")
def aptOutput = new File(aptOutputDir, variant.dirName)
println "****************************"
println "variant: ${variant.name}"
println "manifest: ${variant.processResources.manifestFile}"
println "aptOutput: ${aptOutput}"
println "****************************"
android.sourceSets[getSourceSetName(variant)].java.srcDirs+= aptOutput.getPath()
variant.javaCompile.doFirst {
println "*** Running AndroidAnnotations for ${variant.name}"
aptOutput.mkdirs()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-s', aptOutput
]
}
variant.javaCompile.source = variant.javaCompile.source.filter { p ->
return !p.getPath().startsWith(aptOutputDir.getPath())
}
}
dependencies {
// Android-Annotations
apt 'com.googlecode.androidannotations:androidannotations:2.7.1'
compile 'com.googlecode.androidannotations:androidannotations-api:2.7.1'
// Include libraries only in flavor1
flavor1Compile fileTree(dir: 'libs', include: '*.jar')
}
Here is my (initial) build.gradle (I stripped of non relevant parts):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
configurations {
// This is the annotations processor dependency configuration.
apt
}
def getSourceSetName(variant) {
return new File(variant.dirName).getName();
}
android {
compileSdkVersion 18
defaultConfig {
versionCode 10
versionName "1.0.2"
targetSdkVersion 17
minSdkVersion 10
}
buildToolsVersion "18.0.1"
buildTypes {
release {
zipAlign true
}
}
productFlavors {
flavor1 {}
}
// This has to go after the productFlavors command (otherwise moving the flavor source set root fails).
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// We move the root of our flavor to support our legacy structure.
flavor1.setRoot('flavors/flavor1')
}
applicationVariants.all { variant ->
aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
println "****************************"
println "variant: ${variant.name}"
println "manifest: ${variant.processResources.manifestFile}"
println "aptOutput: ${aptOutput}"
println "****************************"
// Automatically add the generated source code to the source set
android.sourceSets[getSourceSetName(variant)].java.srcDirs += aptOutput
variant.javaCompile.doFirst {
println "*** Running AndroidAnnotations for ${variant.name}"
aptOutput.mkdirs()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-s', aptOutput
]
}
}
}
dependencies {
// Android-Annotations
apt 'com.googlecode.androidannotations:androidannotations:2.7.1'
compile 'com.googlecode.androidannotations:androidannotations-api:2.7.1'
// Include libraries only in flavor1
flavor1Compile fileTree(dir: 'libs', include: '*.jar')
}
I would appreciate any help.
Thanks,
David
If you export the build.gradle from Eclipse, it includes .apt_generated in the gradle file and it shouldn't. Take this out and these errors should go away.
Eventually, I found the solution on the Android Annotations github issues: https://github.com/excilys/androidannotations/issues/676
I re-added the statement for adding it to the source sets (allowing Android Studio to show it as source root). Furthermore, I removed the files from the variants source collection using this:
variant.javaCompile.source = variant.javaCompile.source.filter { p ->
return !p.getPath().startsWith(aptOutputDir.getPath())
}
Now the generated files are recognized, and the duplicate class error is gone.
Best regards, David

Categories

Resources