I want to use my android studio 6.0 without internet connection, to do this, I set this options:
Preferences > Gradle > Offline work
but I am still get the message:
Gradle project sync failed. Basic functionality (e.g. editing,
debugging) will not work properly
When I try to compile, I get the Error:
Error running app: Gradle project sync failed. Please fix your project
and try again.
Could anyone help me or got the same problem?
Thank you
While I don't know what the exact problem is, I can suggest a workaround.
Change build.gradle to start with that:
buildscript {
repositories {
if ('allow' == System.properties['build.network_access']) {
mavenCentral()
} else {
maven {
url 'dependencies'
}
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.0-beta2'
}
}
apply plugin: 'com.android.application'
So, when build.network_access=allow gradle system property is defined, online mavenCentral() repository will be used. Otherwise, build will use local offline maven repository inside dependencies folder.
The trick now is to generate that local offline maven repository. For that I use this script (fetch_dependencies.py):
#!/usr/bin/python
import sys
import os
import subprocess
import glob
import shutil
# Place this in build.gradle:
# repositories {
# if ('allow' == System.properties['build.network_access']) {
# mavenCentral()
# } else {
# maven { url 'dependencies' }
# }
# }
def main(argv):
project_dir = os.path.dirname(os.path.realpath(__file__))
repo_dir = os.path.join(project_dir, "dependencies")
temp_home = os.path.join(project_dir, ".gradle_home")
if not os.path.isdir(temp_home):
os.makedirs(temp_home)
subprocess.call(["gradle", "-g", temp_home, "-Dbuild.network_access=allow"])
cache_files = os.path.join(temp_home, "caches/modules-*/files-*")
for cache_dir in glob.glob(cache_files):
for cache_group_id in os.listdir(cache_dir):
cache_group_dir = os.path.join(cache_dir, cache_group_id)
repo_group_dir = os.path.join(repo_dir, cache_group_id.replace('.', '/'))
for cache_artifact_id in os.listdir(cache_group_dir):
cache_artifact_dir = os.path.join(cache_group_dir, cache_artifact_id)
repo_artifact_dir = os.path.join(repo_group_dir, cache_artifact_id)
for cache_version_id in os.listdir(cache_artifact_dir):
cache_version_dir = os.path.join(cache_artifact_dir, cache_version_id)
repo_version_dir = os.path.join(repo_artifact_dir, cache_version_id)
if not os.path.isdir(repo_version_dir):
os.makedirs(repo_version_dir)
cache_items = os.path.join(cache_version_dir, "*/*")
for cache_item in glob.glob(cache_items):
cache_item_name = os.path.basename(cache_item)
repo_item_path = os.path.join(repo_version_dir, cache_item_name)
print "%s:%s:%s (%s)" % (cache_group_id, cache_artifact_id, cache_version_id, cache_item_name)
shutil.copyfile(cache_item, repo_item_path)
shutil.rmtree(temp_home)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))
It will run the build in online mode to download all dependencies. After that, it will copy jars and poms into dependencies folder creating a local maven repository there. You can even push this dependencies folder into the source control.
After that you can build offline either with gradle --offline build (or just gradle build) or with Android Studio.
Related
Since I wanted to examine this source code, I imported it into Android Studio. I got a few other errors before and fixed them. This is annoying, now I have a new error.
I am getting this error:
Build file 'C:\Users\hange\Desktop\libgdx-demo-superjumper-master\desktop\build.gradle' line: 18
A problem occurred evaluating project ':desktop'.
> Could not get unknown property 'classesDir' for main classes of type org.gradle.api.internal.tasks.DefaultSourceSetOutput.
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
My "desktop\build.gradle" file looks like this. I specified line 18 there.
apply plugin: "java"
sourceCompatibility = 1.6
sourceSets.main.java.srcDirs = [ "src/" ]
project.ext.mainClassName = "com.badlogicgames.superjumper.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../android/assets");
task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}
task dist(type: Jar) {
from files(sourceSets.main.output.classesDir) // THIS IS 18th LINE.
from files(sourceSets.main.output.resourcesDir)
from {configurations.compile.collect {zipTree(it)}}
from files(project.assetsDir);
manifest {
attributes 'Main-Class': project.mainClassName
}
}
dist.dependsOn classes
EDIT: My "build.gradle" file looks like this. But I noticed that it can't resolve the json library. Probably the error is here because we are pulling the gradle version from a json file on the internet.
import groovy.json.JsonSlurper // Cannot resolve symbol 'json'
buildscript {
ant.get(src: 'https://libgdx.com/service/versions.json', dest: 'versions.json')
def versionFile = file('versions.json')
def json
if (versionFile.exists()) {
json = new JsonSlurper().parseText(versionFile.text)
} else throw new GradleException("Unable to retrieve latest versions, please check your internet connection")
ext {
gdxVersion = json.libgdxRelease
roboVMVersion = json.robovmVersion
roboVMGradleVersion = json.robovmPluginVersion
androidToolsVersion = json.androidBuildtoolsVersion
androidSDKVersion = json.androidSDKVersion
androidGradleToolsVersion = json.androidGradleToolVersion
gwtVersion = json.gwtVersion
gwtGradleVersion = json.gwtPluginVersion
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
google()
}
dependencies {
classpath "org.wisepersist:gwt-gradle-plugin:$gwtGradleVersion"
classpath "com.android.tools.build:gradle:$androidGradleToolsVersion"
classpath "com.mobidevelop.robovm:robovm-gradle-plugin:$roboVMGradleVersion"
}
}
I'm posting the solution in case anyone else has the same problem:
androidToolsVersion, androidSDKVersion, androidGradleToolsVersion, gwtGradleVersion
These 4 variables are pulled from a json file on the internet, but the value pulled from the json file may not always work correctly. Make the variables compatible.
For example this code is using classesDir.The classesDir property was removed in Gradle 5.x.
If you pull to Gradle 4.8.1, you will get the error "Minimum supported Gradle version is 6.5. Current version is 4.8.1.". To fix this error you have to assign 3.2.x value compatible with 4.8.1 gradle version to the androidGradleToolsVersion variable.
androidGradleToolsVersion = "3.2.1"
I'm trying to publish a project with multiple modules to artifactory (oss.jfrog.org). When I run artifactoryPublish I get a 403 error but I know it's not a permissions issue because it works with a single module. It only fails trying publish multiple modules.
Some modules are aars and others are jars and all include sources. I can publish them all to Bintray, but can't publish to artifactory (for snapshots).
So the question is, how do I configure a multi-module project to publish snapshots to oss.jfrog.org.
I've figured out that if I change it to publish a single module and make the artifact name the same as the last part of the group, it works, but a different name doesn't work (gives 403 error).
So if group is com.example.foo I can publish foo.jar (com/example/foo/foo/1.0.0-SNAPSHOT/foo-1.0.0.jar). But I can't publish bar.jar (com/example/foo/bar/1.0.0-SNAPSHOT/bar.jar).
This gradle is included in every project's build.gradle
afterEvaluate {
publishing {
publications {
mavenPublication(MavenPublication) {
artifact sourcesJar
if (project.plugins.hasPlugin("com.android.library")) {
artifact("$buildDir/outputs/aar/${project.name}-debug.aar")
} else {
artifact("$buildDir/libs/${project.name}-${version}.jar")
}
groupId "com.example.foo"
artifactId project.name // changing this to "foo" works for a single project
version version
pom {
name.set(project.name)
url.set(POM_URL)
packaging POM_PACKAGING
version version
licenses {
license {
name.set(POM_LICENSE_NAME)
url.set(POM_LICENSE_URL)
}
}
developers {
developer {
name.set(POM_DEVELOPER)
}
}
scm {
url.set(POM_SCM_URL)
connection.set(POM_SCM_CONNECTION)
developerConnection.set(POM_SCM_DEV_CONNECTION)
}
}
}
}
}
bintray {
user = project.findProperty('bintrayUser') ?: System.getenv('BINTRAY_USER')
key = project.findProperty('bintrayApiKey') ?: System.getenv('BINTRAY_API_KEY')
configurations = ['archives']
publish = true
dryRun = true
pkg {
name = project.name
repo = BINTRAY_REPO
userOrg = BINTRAY_ORG
licenses = [POM_LICENSE_NAME]
vcsUrl = POM_SCM_URL
version {
name = project.name
released = new Date()
}
}
}
artifactory {
contextUrl = 'http://oss.jfrog.org'
publish {
repository {
repoKey = 'oss-snapshot-local'
username = project.findProperty('bintrayUser') ?: System.getenv('BINTRAY_USER')
password = project.findProperty('bintrayApiKey') ?: System.getenv('BINTRAY_API_KEY')
}
defaults {
publications('mavenPublication')
publishArtifacts = true
publishPom = true
}
}
resolve {
repoKey = 'jcenter'
}
}
}
Artifactory returns a 403 whenever you're trying to publish an artefact that already exists. In your case, if you've previously published snapshot artefacts from your multi module build, whenever you will try doing that again, you will get a 403. I know you can configure the user access to provide delete permissions to the account you are using to deploy, as indicated here. However, overwriting history is not considered a good practice.
Regarding renaming your groups and artefacts, I don't think that will provide a solution, as it's not your GAV coordinates that are the issue, but rather the fact that artefacts with matching GAV already exist.
If I may ask, why do you want to use SNAPSHOT artefacts? Can you not achieve the same behaviour with dynamic dependencies and dependency locking?
Has anyone had much luck with the play gradle plugin? I'm trying to run the Play Framework 2.6 starter project. I can run from SBT fine but I get the following error when I run from gradle:
play.api.UnexpectedException: Unexpected exception[NoClassDefFoundError: java/sql/Date]
at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:190)
at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:124)
at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:202)
at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$1(AkkaHttpServer.scala:117)
at akka.stream.impl.fusing.MapAsync$$anon$25.onPush(Ops.scala:1194)
at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:519)
at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:482)
at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:378)
at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:
I'm using Java 1.8 with the following build.gradle:
plugins {
id 'play'
id 'idea'
}
def playVersion = "2.6.13"
def scalaVersion = System.getProperty("scala.binary.version", /* default = */ "2.12")
model {
components {
play {
platform play: playVersion, scala: scalaVersion, java: '1.8'
injectedRoutesGenerator = true
sources {
twirlTemplates {
defaultImports = TwirlImports.JAVA
}
}
}
}
}
dependencies {
play "com.typesafe.play:play-guice_$scalaVersion:$playVersion"
play "com.typesafe.play:play-logback_$scalaVersion:$playVersion"
play "com.h2database:h2:1.4.196"
playTest "org.assertj:assertj-core:3.6.2"
playTest "org.awaitility:awaitility:2.0.0"
}
repositories {
jcenter()
maven {
name "lightbend-maven-releases"
url "https://repo.lightbend.com/lightbend/maven-release"
}
ivy {
name "lightbend-ivy-release"
url "https://repo.lightbend.com/lightbend/ivy-releases"
layout "ivy"
}
}
You need to set up the java_home to use a Java 1.8 version. Apparently, the play gradle plugin expects a java home.
export JAVA_HOME=YOUR_JAVA_LOCATION
$ source .bash_profile
$ echo $JAVA_HOME
I found out the issue, my $JAVA_HOME was set to my Java 9 path but java/javac were set to Java 8. This was causing my gradle to use the wrong java version. After fixing my $JAVA_HOME, it works correctly.
I'll leave this up in case anyone else runs into this issue.
I have multimodule project in Gradle, where parent build.gradle is just an aggregator of subprojects, without any sources.
Structure:
parent with version in gradle.properties
- child1 inheriting version
- child2 inheriting version
Both children produce JARs with artifacts, while parent produces empty JAR which I don't want to upload anywhere at all.
Now I want to release such project. There should be one commit with version update and one tag in Git. However, all subprojects should be build and uploaded to a repository. How can I achieve it?
I've tried gradle-release plugin, but I struggle to configure it properly. I get either of the following:
apply plugin on parent, get proper commit and tag but get only root project uploaded
apply plugin on subprojects, get projects properly uploaded, but have separate commit and tag for every subproject
I had the same problem and ended up writing the necessary steps in Gradle with the help of the gradle-svn-tools plugin (https://github.com/martoe/gradle-svntools-plugin). Note that I'm no Groovy expert, so I guess a few things can be done more easily :)
My build.gradle in the parent project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'at.bxm.gradleplugins:gradle-svntools-plugin:1.6.2'
}
}
allprojects {
apply plugin: 'at.bxm.svntools'
//subversion login
svntools {
username = svnUsername
password = credentials.svnpwd
}
}
//task to write the version parameter given via command line into the "gradle.properties" files.
// Call with: gradle writeProjectVersion -PnewVersion=1.0.1-SNAPSHOT
task('writeProjectVersion') << {
if (project.hasProperty('newVersion')) {
//set version in gradle settings
project.version = project.newVersion
project.subprojects?.each {it.version = project.version }
//set version in all the gradle.properties files
new File(".").traverse(type : groovy.io.FileType.FILES, nameFilter: 'gradle.properties') {
project.ant.replaceregexp(file: it, byline: true) {
key = 'version'
version = project.version
regexp(pattern: "^(\\s*)$key((\\s*[=|:]\\s*)|(\\s+)).+\$")
substitution(expression: "\\1$key\\2$version")
}
}
println 'Successfully changed project version in gradle.properties to \'' + project.version + '\''
} else {
println 'No parameter \'newVersion\' provided, so not changing the project version.'
}
}
//task to commit the gradle.properties changes into SVN - also needs the new version as parameter for the commit message
// Call with: gradle svnCommit -PnewVersion=1.0.1-SNAPSHOT
task('svnCommit', type: at.bxm.gradleplugins.svntools.tasks.SvnCommit) {
if (project.hasProperty('newVersion')) {
def files = []
new File(".").traverse(type : groovy.io.FileType.FILES, nameFilter: 'gradle.properties') { files << it } //appends all matching files to the list
source = files
commitMessage = "[Jenkins Release Build] Changing project version to '" + project.newVersion + "'"
} else {
println 'No parameter \'newVersion\' provided, so SVN commit was not executed.'
}
}
//task to tag the current head - also needs the new version as parameter for the commit message
// Call with: gradle svnTag -PnewVersion=1.0.1-SNAPSHOT
task('svnTag', type: at.bxm.gradleplugins.svntools.tasks.SvnTag) {
if (project.hasProperty('newVersion')) {
tagName = "$project.newVersion"
localChanges = true
commitMessage = "[Jenkins Release Build] Tagging Release version $project.newVersion"
} else {
println 'No parameter \'newVersion\' provided, so SVN tagging was not executed.'
}
}
I use Jenkins for execution and run the following shell script:
# execute gradle tests
gradle test
# set version in gradle.properties and tag the current code (gradle.properties changes are commited in the tag)
gradle writeProjectVersion -PnewVersion=${releaseVersion}
gradle svnTag -PnewVersion=${releaseVersion}
# build the artifacts and upload them to Nexus without running the test cases again
gradle clean build uploadArchives -x test
# set version in gradle.properties and commit the files
gradle writeProjectVersion -PnewVersion=${newSnapshotVersion}
gradle svnCommit -PnewVersion=${newSnapshotVersion}
Hope this helps.
I'm having some real problems understanding how all the pieces in gradle to create and upload an artifact fit together.
My intention in this script is simple: I want to download a source tarball and possibly a bunch of dependencies, run a "build.sh" shellscript which will end up creating a binary tarball and have the gradle script publish it to an artifact repo.
The main idea is that I can use gradle's dependency management, maven artifact knowledge and build parallelization and avoidance to control the execution of the build scripts themselves, mainly to manage my set of third party binary dependencies...
The following script fails with a 400 error, I suspect it's because I'm not linking the artifact with the actual output file.
What's the right and proper way to go?
apply plugin: 'maven'
version 'testarch-4.2'
repositories {
maven {
url "http://nexus/..."
}
}
configurations {
sourceArchive
binaryArchive
}
dependencies {
sourceArchive "org.gnu:bash:4.2:src#tgz"
}
task buildFromSource(type: Exec) {
inputs.files configurations.sourceArchive.files
outputs.file file("${project.name}-${project.version}.tgz")
executable './build.sh'
def myArgs = configurations.sourceArchive.files.path
myArgs.add(0, outputs.files.asPath)
args myArgs
}
artifacts {
// Is this really the only way to transform a singleton collection
// into the singleton?
// def outputFile
// buildFromSource.outputs.files.each { outputFile = it }
// Nope: this is better magic:
def outputFile = buildFromSource.outputs.files.singleFile
println outputFile.path
binaryArchive file: outputFile, name: 'bash'
// binaryArchive file: file(buildFromSource.outputs.files.asPath), name: 'bash'
}
uploadArchives {
configuration = configurations.binaryArchive
repositories.mavenDeployer {
repository(url: "http://nexus/..") {
authentication(userName: "me", password: "secret!")
}
pom.groupId = 'org.gnu'
}
}
uploadArchives.dependsOn buildFromSource
The error I get is:
* What went wrong:
Execution failed for task ':uploadArchives'.
> Could not publish configuration 'binaryArchive'
> Error deploying artifact 'org.gnu:bash:tgz': Error deploying artifact: Failed to transfer file: http://nexus/.../org/gnu/bash/testarch-4.2/bash-testarch-4.2.tgz. Return code is: 400
Updated from comments, same error - trying to get access to the nexus logs for further debugging.
Error from Nexus is "missing entity", see: Missing Request Entity response to a PUT to Nexus
The root cause of my problem was I was testing with an empty file. Nexus doesn't like empty files. As soon as I put content in it, Nexus was happy and the code was working.