How to properly add `mock` package to Android Studio project? - android-studio

I want Android Studio to recognize my mock folder by putting mock at the end of the package name while in the Android pane.
How can I achieve this?
I've tried different combinations of:
right-clicking -> new package
right-clicking -> new Java Folder
declaring sourceSets in Gradle like so:
sourceSets {
mock {
java.srcDir 'src/mock'
}
}

add Mock as a flavor
productFlavors {
mock {
applicationIdSuffix = ".mock"
dimension "default"
}
prod {
dimension "default"
}
}
// Remove mockRelease as it's not needed.
android.variantFilter { variant ->
if (variant.buildType.name == 'release'
&& variant.flavors[0].name == 'mock') {
variant.ignore = true
}
}

Related

Android Studio Gradle: how to get variant/flavour in custom assets processor task

I have simple assets processing gradle task prepare_assets in Android Studio 3.1.3. It is linked to preBuild task:
preBuild.dependsOn(prepare_assets)
Now I have several flavors for different resolution versions and I want to let prepare_assets know what assets to process. Maybe I don't get the idea of Gradle, but I can't understand how to achieve this: I tried to set variable in config phase
applicationVariants.all { variant ->
buildType = variant.buildType.name // sets the current build type
}
but when I read variable in my task it always the same.
In flavours section declaration:
all { flavor ->
task("${flavor.name.capitalize()}_prepare_assets") {
println "*** conf TEST for ${flavor.name.capitalize()}***"
doLast {
println "*** action TEST ${flavor.name.capitalize()}***"
if (flavor.name.equals("fullhd"))
{
//WARNING: to call copy, javaexec closures here use project.copy and project.javaexec!
}
else
{
...
}
}
}
}
In the bottom of file adding dependenses for runtime created android tasks for all flavors:
tasks.whenTaskAdded { theTask ->
if (theTask.class.name.contains("AppPreBuildTask_Decorated"))
{
for (Iterator iterator = android.productFlavors.iterator(); iterator.hasNext();) {
String flv_name = iterator.next().name.capitalize();
if (theTask.name.contains(flv_name+"Debug") || theTask.name.contains(flv_name+"Release"))
theTask.dependsOn "${flv_name}_prepare_assets";
}
}
}

Why does Android Studio not run my generateConfig but command line does

I'm trying to generate identical assets for each of the subprojects in a project. Since exactly the same thing has to happen for each subproject, I'm trying to avoid having to add the generation code to each subproject.
What I've got so far is:
// in my outer build.gradle
allprojects { proj ->
gradle.projectsEvaluated {
def configTemplate = project.file('src/main/templates/configuration.json')
def android = proj.getProperties()['android']
if (configTemplate.exists() && android != null) {
task generateConfigFiles(type: ConfigureTask) {
template configTemplate
environments proj.rootProject.file('scripts/environments.json')
environmentName host
destination new File(project.buildDir, 'templates/configuration.json')
}
android.applicationVariants.all { variant ->
variant.mergeAssets.dependsOn generateConfigFiles
}
}
}
}
// each application build.gradle
android {
sourceSets {
main {
assets.srcDir 'build/templates'
}
}
}
This seems to work exactly as expected when I run gradle from the command line ./gradlew mergeQaDebugAssets runs the generateConfigFiles task before running mergeQaDebugAssets
If I attempt to run this from Android Studio using the gradle window, it never runs generateConfigFiles
I've also gotten it to work by just including the projectsEvaluated step in each of the project build.gradle files, but, as noted, I'm trying to avoid the code duplication and maintenance headaches that brings.

Edit property based on library variant gradle android

I have an android project with native part in it. I would like to build my native code with different flags, depending on buildType and productFlavor, for instance:
android {
project.ext.buildFlags = ['-j16', 'all']
buildTypes {
debug { project.ext.buildFlags.add('NDK_DEBUG=1')}
}
productFlavors {
pretty {project.ext.buildFlags.add('PRETTY')}
ugly {project.ext.buildFlags.add('UGLY')}
}
task buildNativeCode(type: Exec) {
commandLine 'ndk-build', project.ext.buildFlags
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
So, for uglyDebug I would like to build my native library with 'UGLY', and 'NDK_DEBUG' flags, prettyDebug with 'PRETTY' and 'NDK_DEBUG' flags and so on. However, they are always added, regardless of the chosen configuration. From what I gather, this is done during project evaluation and I don't know how to set it up properly. For now I ended up creating many buildNativeXXX tasks that have pretty much the same content. I would love to avoid code repetition.
You can use the following commands to modify any tasks afterwards:
afterEvaluate {
productFlavors.each { productFlavorName ->
buildTypes.each { buildTypeName ->
project.ext.buildFlags.add('NDK_DEBUG=1')
if (productFlavorName.equals("pretty")) {
//enter code here
project.ext.buildFlags.add('PRETTY')
} else {
project.ext.buildFlags.add('UGLY')
}
}
}
}
Now you have full control of how gradle adds the buildFlags for you. :)
If you want to use the buildConfig variables, you can set them using:
applicationVariants.all { variant ->
variant.buildConfigField "String", "COMMIT_HASH", "HASH123456789"
}

Re-publish gradle artifacts

We have an ivy repository, and we are using gradle for our dependency management and build framework. When an artifact is determined to be production-ready, we don't want to have to build it again, so we want to just "promote" an existing artifact via a web application that is leveraging Gradle and the tooling API to do most of the heaving lifting for us.
Currently, I'm copying the artifacts to a local folder and running another build.gradle that just re-publishes it. We are publishing it to a new folder in our existing repository, and a folder in the release repository.
In doing so, it is only publishing the ivy.xml to both locations.
I'm guessing this is due to where the artifacts are located.
PromotionService.groovy
void promote(Project project, Build build, String newVersion) {
def artifactLocation = "/path/to/repository"
// we are generating this build.gradle and copying it
def buildFileText = new File('promote.gradle').getText('UTF-8')
def artifacts = buildDao.findArtifactsByBuild(build)
def localBuildFolderPath = "/path/to/local/gradle/build"
def localBuildFolder = new File(localBuildFolderPath)
localBuildFolder.mkdirs()
// remove everything currently in the directory
def buildFiles = localBuildFolder.listFiles()
buildFiles.each {
it.delete()
}
def newFile = new File("/path/to/local/gradle/build.gradle")
newFile.mkdirs()
if (newFile.exists())
newFile.delete()
newFile << buildFileText
artifacts.each { VersionedArtifact it ->
def folder = new File("${artifactLocation}/${it.module}/${build.branch}/${it.version}")
def files = folder.listFiles()
files.each { File from ->
// remove version number from file name
String fromName = from.name
def matcher = fromName =~ /(.*?)-(\d)+\.(\d)+\.(\d)+(\.\d+)?\.(.*)/
fromName = "${matcher[0][1]}.${matcher[0][6]}"
File to = new File("${localBuildFolderPath}/${it.module}/${fromName}")
to.mkdirs()
if (to.exists()) to.delete()
// wrapper for Guava's Files.copy()
FileUtil.copy(from, to)
}
ProjectConnection connection = GradleConnector.newConnector().forProjectDirectory(new File("${workingDir}/gradle")).connect()
connection.newBuild()
.forTasks("publishReleaseBranchPublicationToIvyRepository", "publishReleaseRepoPublicationToReleaseRepository")
.withArguments("-PMODULE=${it.module}", "-PVERSION=${it.version}", "-PNEWVERSION=${newVersion}")
.run()
}
}
build.gradle
apply plugin: 'groovy'
apply plugin: 'ivy-publish'
publishing {
publications {
releaseBranch(IvyPublication) {
organisation 'our-organization'
module MODULE
revision VERSION
descriptor.status = 'release'
configurations { archive {
} }
}
releaseRepo(IvyPublication) {
organisation 'our-organization'
module MODULE
revision NEWVERSION
descriptor.status = 'release'
configurations { archive {
}}
}
}
repositories {
ivy {
name 'ivy'
url "/path/to/ivy/repo"
layout "pattern", {
ivy "[organisation]/[module]/release/[revision]/[module]-[revision].xml"
artifact "[organisation]/[module]/release/[revision]/[artifact](-[classifier])-[revision].[ext]"
}
}
ivy {
name 'release'
url "/path/to/release/repo"
layout "pattern", {
ivy "[organisation]/[module]/[revision]/[module]-[revision].xml"
artifact "[organization]/[module]/[revision]/[artifact](-[classifier])-[revision].[ext]"
}
}
}
}
Edit: Made it clearer we're writing a web application to promote artifacts.
It's not clear to me why the promotion is implemented using the tooling API, rather than as a regular Gradle task or plugin. Anyway, the IvyPublications are neither configured using IvyPublication#from, nor using IvyPublication#artifact. Hence they won't have any artifacts.

Apply configuration to specific projects in gradle build script

I have a gradle project that has java applications as well as android applications.
root/
build.gradle
settings.gradle
java1/
java2/
android1/
android2/
java3/
etc.
What is the best practice for structuring my build script? I am a total gradle novice and am migrating the project from maven to gradle.
I wanted to do something instead of
configure(subprojects) {}
to apply plugins and other specific things.
such as
configure(java1, java2, java3) { // java specifics }
configure(android1, android2) { // android specifics }
I am probably approaching this from the wrong way.
More explicitly I need to apply the plugin java only for the java projects and the android plugin for the android projects.
configure(subprojects.findAll {it.name == "java1" || it.name == "java2"}) {
Under the filtering section in the guide
Hope this helps someone else out.
There are multiple ways, depending on what you want... Some examples:
// Configures just project java1
project(":java1") { ... }
// Configures projects java1 and java2
["java1","java2"].each { name ->
project(":$name") { ... }
}
You can use normal groovy to find/iterate over all the projects.
Another option:
configure([ project(':sub1'), project(':sub2'), ... ]) {
...
}
The shortest and easiest option:
configure(subprojects.findAll()) {
if (it.name.equals('MY_PROJECT')) {
...
} else {
...
}
}
Another approach...
In the settings.gradle you do define your projects like this:
gradle.ext.javaProjects=[]
gradle.ext.androidProjects=[]
javaProject('java1')
javaProject('java2')
javaProject('java3')
androidProject('android1')
androidProject('android2')
def javaProject(String name) {
gradle.ext.javaProjects.add(name)
include ":$name"
}
def androidProject(String name) {
gradle.ext.androidProjects.add(name)
include ":$name"
}
Now you can reference those in your root build.gradle:
def javaProjects = subprojects.findAll {gradle.ext.javaProjects.contains(it.name)};
def androidProjects = subprojects.findAll {gradle.ext.javaProjects.contains(it.name)};
configure(javaProjects) {
...
}
configure(androidProjects) {
...
}
Maybe thats overkill... but i usually have the project definition method in my settings.gradle anyway. E.g. if you want to put all java projects under a java folder you could define things like this:
def javaProject(String name) {
gradle.ext.javaProjects.add(name)
include ":$name"
project(":$name").projectDir = file('java/'+ name)
}

Resources