We created an Android app with a webview which shows a local website from the assets folder.
The project has different Product Flavors to generate diffent apps with different styles and content but with the same codebas (native Java and HTML / JS).
For each flavor we want to define a diffent sass file with the colors and tweaks for that specific flavour.
I know that I need to create a task in gradle which builds the CSS files but I have no idea where to start:
How do I get the url of the assets folder of a specific flavour?
Can I use a special gradle plugin for building sass or do I have to create a task which executes the "sass" command?
When I use another gradle plugin like compass, how do I configure the right folders for each flavour? The plugin settings are in the top level and not in the Android plugin level.
I finaly have the solution!
Add this to your build.gradle in the main folder (not of your app):
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url 'http://dl.bintray.com/robfletcher/gradle-plugins' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.github.robfletcher:compass-gradle-plugin:2.0.6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Add this the build.gradle of the app module:
apply plugin: 'com.android.application'
apply plugin: 'com.github.robfletcher.compass'
android {
[..]
android.applicationVariants.all { variant ->
for (output in variant.outputs) {
def assetsDir = output.packageApplication.assets;
tasks["merge${variant.name.capitalize()}Assets"].doLast() {
println "Assets folder: " + assetsDir
def _ccsDir = file("$assetsDir/css")
def _sassDir = file("$assetsDir/sass")
def _imagesDir = file("$assetsDir/images")
def _javascriptsDir = file("$assetsDir/js")
def _fontsDir = file("$assetsDir/fonts")
project.compass {
cssDir = _ccsDir
sassDir = _sassDir
imagesDir = _imagesDir
javascriptsDir = _javascriptsDir
fontsDir = _fontsDir
}
//compileSass
project.compassCompile.execute()
}
}
}
}
I never thought it would work out but it works!
Related
I am in the process of migrating buildSrc convention plugins into standalone plugins. There are a lot of examples for creating Gradle plugins for Project objects, but a real dearth for Settings and Gradle. I want to centralize the list of repositories that we use in our gradle.settings.kts files, so I've created a Gradle Settings plugin: RepositoriesPlugin.
It is implemented in the same manner at a Project plugin, however, I am unsure about how to interpret the following quote from Gradle's docs: "A plugin can instead receive a parameter of type Settings, in which case the plugin can be applied in a settings script.", as stated in the documentation at this link: Gradle 7.2 Doc
The following example shows how I've applied my settings plugin in a settings.gradle.kts file. Is this how a settings plugin is applied, as per the documentation?
I've included the basic settings plugin code, below, too.
I'd appreciate your help to clarify that I am doing this properly. I am sure that this posting will help others who stray away from just Project plugins.
Thanks for your time and interest..
// settings.gradle.kts
pluginManagement {
plugins {
id("com.abitofhelp.gradle.plugins.repositoriesplugin") version "1.0.0-1"
}
settings.extensions
getByType(RepositoriesPluginExtension::class).apply {
localRepoName = "local-repo"
localRepositoryPath = "../../local-repo"
}
// Set the plugin repositories for all projects.
//repositories {
// maven { name = "localRepo"; url = uri(file("./local-repo")) }
// gradlePluginPortal()
// mavenCentral()
//}
}
// repositoriesplugin.kt
open class RepositoriesPlugin: Plugin<Settings> {
override fun apply(settings: Settings) {
val extension: RepositoriesPluginExtension =
settings.extensions.create("repositoriesPlugin", RepositoriesPluginExtension::class.java)
val localRepositoryName = extension.localRepositoryName?.let { it }?: "../../localRepo"
val localRepositoryPath = extension.localRepositoryPath?.let { it }?: "../../local-repo"
settings.pluginManagement.repositories.apply {
// Set the PLUGIN REPOSITORIES for all subprojects.
maven { repository ->
repository.name = localRepositoryName
repository.url = URI.create(localRepositoryPath)
}
gradlePluginPortal()
mavenCentral()
}
settings.dependencyResolutionManagement.repositories.apply {
// Set the DEPENDENCY REPOSITORIES for all subprojects.
maven {
it.name = localRepositoryName
it.url = URI.create(localRepositoryPath)
}
mavenCentral()
gradlePluginPortal()
}
}
}
I just had to figure this out myself. You weren't far off with your initial attempt but the plugins block actually has to go after the pluginManagement block. The basic layout of my working implementation looks like this:
// settings.gradle.kts
rootProject.name = "myProject"
pluginManagement {
repositories {
maven {
url = uri("/tmp/maven-local")
gradlePluginPortal()
}
}
}
plugins {
id("com.example.my-plugin") version "1.0-SNAPSHOT"
}
I believe you need to add the plugin to settings.gradle classpath via buildscript closure. pluginManagement.plugins makes the plugin availabile to the root (& sub) project build.gradle classpath, but I think that is a different classpath from the settings.gradle script.
To add to settings gradle, give this a try:
// settings.gradle
buildscript {
repositories {
maven { url "http://repo.where.plugin.is.published" }
}
classpath("complete.maven.coordinates:your-settings-plugin:version")
}
pluginManager.apply(your.settings.plugin.id)
As Justin Warkentin already noted, you first need to add your repo in the pluginManagement section and then you can apply your plugin like any other.
If you want to bypass the repository, you can also add the plugin project as composite build via includeBuild in the pluginManagement section.
In my example I have combined both methods. If the plugin project exists locally, this one is used, otherwise the version from the repo is used. This way you can (temporarily) add your plugin project to develop on it and test it directly in including projects.
pluginManagement {
repositories {
// your plugin repo; maybe...
mavenLocal()
}
// use local copy of your plugin if available,
// otherwise load plugin from repo
def name = 'your_plugin_project'
if (file(name).isDirectory()) {
includeBuild(name)
}
}
plugins {
// apply your plugin
id 'your.plugin.id'
}
I have a kotlin multiplatform project A setup for iOS and Android, it works well. It has a common module for sharing business logic, and platform-android and platform-ios module for implementing the platform API.
After I adding the common and platform-android module from project A to another Android project B, the Android Studio IDE reports tons of syntax error, but the codes build and run from Android studio without a problem.
The syntax looks like the kotlin-stdlib is not there while it's indeed in the build.gradle, otherwise it won't build.
For instance:
val filterMap = mutableMapOf<String, MenuFilter>()
Android studio will say Unresolved reference: mutableMapOf
Some facts:
common module has the problem
no problem for platform-android.
and of course, no problem when I use IDEA to edit project A
my build.gradle for common module looks like this:
apply plugin: 'kotlin-platform-common'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
}
sourceSets {
main.kotlin.srcDirs += 'main/'
test.kotlin.srcDirs += 'test/'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.kotlin
}
artifacts {
archives sourcesJar
}
kotlin {
experimental {
coroutines "enable"
}
}
The rootProject build.gradle in Android Studio is:
buildscript {
ext{
kotlin_version = '1.2.41'
anko_version = '0.10.4'
dagger_version = '2.15'
support_lib_version = '27.1.1'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And this is the settings.gradle file for Android project B
def projectA_path = "path/to/projectA"
include(":common")
project(":common").projectDir = new File("$projectA_path/common")
include(":platforms:android")
project(":platforms:android").projectDir = new File("$projectA_path/platforms/android")
Even the two are from different projects, according to the setup here. Shouldn't the two just work? What am I missing here?
IDE version:
IDEA Ultimate 2018.1
Android studio 3.1.2
Try adding this in build.gradle
apply plugin: 'org.jetbrains.kotlin.multiplatform'
I'm trying to generate .java files from the .proto files I have stored under my SRC folder in Android studio. I put the below code in my gradle file by it doesn't seem to work
apply plugin: 'com.squareup.wire'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.squareup.wire:wire-maven-plugin:2.1.1'
}
}
There is a gradle plugin for wire here: https://github.com/square/wire-gradle-plugin. However, it seems like it's not quite ready for primetime yet. I had some trouble getting it working.
But, here's a way to do it that automates generation of java code from the *.proto files using the wire compiler directly and a simple gradle task. I've provided a snippet below with the modifications to your build.gradle. Change the protoPath and wireGeneratedPath based on your source layout.
def protoPath = 'src/proto'
def wireGeneratedPath = 'build/generated/source/wire'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.squareup.wire:wire-compiler:2.2.0'
}
}
android {
sourceSets {
main {
java {
include wireGeneratedPath
}
}
}
}
dependencies {
compile 'com.squareup.wire:wire-runtime:2.2.0'
// Leave this out if you're not doing integration testing...
androidTestCompile 'com.squareup.wire:wire-runtime:2.2.0'
}
// This handles the protocol buffer generation with wire
task generateWireClasses {
description = 'Generate Java classes from protocol buffer (.proto) schema files for use with squareup\'s wire library'
delete(wireGeneratedPath)
fileTree(dir: protoPath, include: '**/*.proto').each { File file ->
doLast {
javaexec {
main = 'com.squareup.wire.WireCompiler'
classpath = buildscript.configurations.classpath
args = ["--proto_path=${protoPath}", "--java_out=${wireGeneratedPath}", "${file}"]
}
}
}
}
preBuild.dependsOn generateWireClasses
So instead of using a gradle plugin I just ended up using the square wire compiler jar. Here are the steps.
Download compiler-jar-with-dependencies from http://search.maven.org/#artifactdetails%7Ccom.squareup.wire%7Cwire-compiler%7C2.1.1%7Cjar
Put jar file into root directory of android app
Go to the directory and paste this command
java -jar wire-compiler-2.1.1-jar-with-dependencies.jar --proto_path=directory-of-protofile --java_out=app/src/main/java/ name-of-file.proto
Should work. Make sure to replace the directory-of-protofile and name-of-file with whatever you have.
What is the best way to add LibGDX to an existing Android Studio project?
Because Android Studio already has Gradle built in, I was assuming that I could change some code in the build.gradle file to add LibGDX to my existing project. The only gradle instructions I found on LibGDX was the automated tool for making a new project with LibGDX already baked in.
All idea of libgdx is writing one code for all platforms. So Android specific code should be added in properly way and if you will just add libgdx dependencies to your project it will not help you a lot. You will be able to use only very small part of libgdx features. If you want to do it in any case here is a example of build.gradle which adds libgdx dependencies to regular Java project, compare it with your build.gradle and add needed lines to ext, repositories and dependencies sections.
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "java"
apply plugin: "maven"
sourceCompatibility = 1.6
targetCompatibility = 1.6
version = '0.1'
group = 'com.gdx.bream'
ext {
appName = 'bream'
gdxVersion = '1.3.1'
}
repositories {
mavenCentral()
mavenLocal()
maven { url 'https://github.com/steffenschaefer/gwt-gradle-plugin/raw/maven-repo/' }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
sourceSets {
main {
java { srcDirs = ['src']}
resources {
srcDirs = ['src']
}
}
}
task sourcesJar(type:Jar){
from sourceSets.main.allSource
classifier = 'sources'
}
artifacts { archives sourcesJar }
dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" }
P.S If you want create game only for Android and looking for engine which can be easily integrated to existing project, take a look at AndEngine
What is the preferred way to share some code (E.g. a Utils class) between two projects when building two apps using Gradle to build?
Can I do this without creating extra jar files? I just want my code to sit outside the app projects, be imported/compiled into both app projects. Or is this simply not possible?
I'm familiar with the approach that uses jars or Android library projects, but both seem a bit unwieldy.
My favorite way of doing this is by keeping it in a local Maven repo. The repo can even live in your SCM so it's the same across workspaces.
Create a new Android Studio project and then set it as a maven project your build.gradle config:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
apply plugin: 'maven'
repositories {
mavenCentral()
}
configurations {
archives {
extendsFrom configurations.default
}
}
group = 'com.mypackage.mylibrary'
version = '1.0.0'
uploadArchives {
configuration = configurations.archives
repositories {
mavenDeployer {
repository(url: uri("relative/path/to/localrepo"))
pom.project {
artifactId 'mylibrary'
name 'My Library'
packaging 'aar'
}
}
}
}
android {
// copy old android config here
}
You'll need to deploy the library before you can use it. Do this by using the uploadArchives task [./gradlew uploadArchives]
Now you should be able to use this library in any project by doing this:
repositories {
maven { url 'relative/path/to/localrepo' }
}
dependencies {
compile ('com.mypackage.mylibrary:1.0.0')
}
When you make changes to your library, you'll have to re-deploy (uploadArchives) with a new version, then update the dependency reference in whatever project needs the new version.