Mr. Fish Mr. Fish - 4 days ago 6
Android Question

Android Studio console build cannot reuse cached gradle dependencies

Android Studio is running on version 2.2.2 with gradle 3.2.1 and gradle plugin 2.2.2

I am working on a project migration from Eclipse ADT to Android Studio. My Android Studio project has multiple ADT projects imported as modules. Android Studio runs in offline mode, gradle sources are available locally.

Top level build.gradle

buildscript {
repositories {
jcenter ()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
allprojects {
repositories {
jcenter()
}
}


This is an example module build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.my.project"
minSdkVersion 17
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
externalNativeBuild {
ndkBuild {
path 'jni/Android.mk'
}
}
}

dependencies {
compile 'com.android.support:support-v4:23.4.0'
compile files('libs/commons-exec-1.3.jar')
compile files('libs/commons-io-2.4.jar')
}


Gradle sync in Android Studio works fine, but any console build with option
--offline
fails, because of missing cached repository. I can't go online to test an online sync.

I've tried different build options, defined
--gradle-user-home
or
--project-cache-dir
without success.

This is the exception

* What went wrong:
A problem occurred configuring root project 'My-Project'.
> Could not resolve all dependencies for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:2.2.2.
Required by:
project :
> No cached version of com.android.tools.build:gradle:2.2.2 available for offline mode.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Is it not possible to reuse the cached repository of the Android Studio gradle sync? There are so many project folders which seems to contain the cached gradle files, but which one is it? I can see
PRJ_DIR/.gradle/caches/3.2.1
,
PRJ_DIR/caches
or
PRJ_DIR/gradle/caches
. Do I have to specify every single task on a console build?

Answer

I needed to get sure the path to the gradle cache has been defined and it is the same for Android Studio and the console build.

The easiest way is to define path to gradle home:

export GRADLE_USER_HOME=/path/to/gradle/local/cache

I created a dummy project on a computer with internet access, executed a gradle online sync. After that, switched into offline mode in Android Studio and executed ./gradlew --offline in the project folder. Build was successful.

Now, it is possible to create a new project still in offline mode (internet access can be switched off) and use the cached gradle build.

Comments