Android Studio removes modules falsely identified as not backed by Gradle - android-studio

I have a problem with Android Studio 0.3.0 and my project folder containing a plain old java library project, two Android library projects, and three Android apps. It's all built with Gradle.
The problem is that the initial import into Android Studio works fine (using Android Studio's Import Project..., then choosing my settings.gradle file), but when I press the refresh button in the Gradle sidebar, I get the message "The modules below are not backed by Gradle anymore. Check those to be removed from the ide project too:", and then it lists ALL my modules for removal. Everything builds fine from the terminal.
Output of gradle projects is (with edited names):
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'root'
+--- Project ':android-lib1'
+--- Project ':android-app1'
+--- Project ':android-app2'
+--- Project ':android-app3'
+--- Project ':android-lib2'
\--- Project ':java-lib'
In the root folder, I have settings.gradle:
include ':java-lib'
include ':android-lib1'
include ':android-lib2'
include ':android-app1'
include ':android-app2'
include ':android-app3'
My build.gradle in the root folder:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
allprojects {
repositories {
mavenCentral()
ivy {
name 'repo'
artifactPattern 'http://repo.example.com:8081/artifactory/libs-release-local/[organisation]/[module]/[revision]/[type]s/[artifact].[ext]'
credentials {
username 'example'
password 'example'
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.8'
}
build.gradle for java-lib:
apply plugin: 'java'
apply plugin: 'eclipse'
compileJava.options.encoding = 'UTF-8'
group 'example'
version '0.1.0'
status 'release'
sourceCompatibility = '1.6'
targetCompatibility = '1.6'
dependencies {
compile 'com.google.protobuf:protobuf-java:2.5.0'
compile 'com.google.guava:guava:15.0'
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
uploadArchives {
repositories {
add project.repositories.repo
}
}
build.gradle for the two Android libs (they are the same apart from dependencies and version numbers:
apply plugin: 'android-library'
dependencies {
compile project(':java-lib')
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
versionCode 1
versionName '0.1.0'
minSdkVersion 9
targetSdkVersion 18
}
}
And finally, build.gradle for the Android apps (again, almost identical):
apply plugin: 'android'
dependencies {
compile project(':android-lib1')
compile project(':android-lib2')
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
versionCode 1
versionName '0.1.0'
}
signingConfigs {
release
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}
if (project.hasProperty('storeFile')) {
android.signingConfigs.release.storeFile = file(storeFile)
}
if (project.hasProperty('storePassword')) {
android.signingConfigs.release.storePassword = storePassword
}
if (project.hasProperty('keyAlias')) {
android.signingConfigs.release.keyAlias = keyAlias
}
if (project.hasProperty('keyPassword')) {
android.signingConfigs.release.keyPassword = keyPassword
}
Perhaps it's a bug in Android Studio 0.3.0? I didn't experience it in earlier versions, but I want to make sure it's not just something in my build files.
Thanks a bunch for reading!

This was a bug which has now been fixed (working for me since Android Studio 0.3.5): https://code.google.com/p/android/issues/detail?id=61453

Also got this or very similar error (don't know if I did refresh) I see you posted bug at code.google.com and agree it must be their bug. My work around fix was just to cancel out of "not backed by Gradle" message and then I ran gradle build from command line. Then everything worked.

Related

Android Studio build works, Gradle command line fails

I have ported an app from Eclipse to Android Studio, implemented my needed flavors and have it allow working in Android Studio. Due to the number of flavors I would prefer to build it using command line Gradle. However when I run the following command to build my release APKs
gradlew.bat assembleRelease
I get the following error:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\whatever\build.gradle' line: 1
* What went wrong:
A problem occurred evaluating project ':MyCompany'.
> java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
I have run with --stacktrace and --debug and it really does not offer any other information, other than pointing the error to be with the first line of the build.gradle:
apply plugin: 'com.android.application'
I have attempted the following based on other stackoverflow responses to this error:
Upgraded to JavaVersion.VERSION_1_8
Added Jack the build.gradle (and that slowed everything down, but it was required for 1.8, even though it is deprecated?)
Increased memory for heap (org.gradle.jvmargs=-Xmx4096m) and dex (javaMaxHeapSize "4g").
I am running Android Studio 2.3.3
Here is my build.gradle file. I have left only a couple of the flavors to save space, and renamed some of the stuff to protect the innocent. I have left in the dependencies in case that might be the problem? The only funkiness is that I am renaming the APK and pushing it to a different folder, but that is all working when I run the release build directly out of Android Studio. The build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
signingConfigs {
config {
keyAlias 'whatever'
keyPassword 'imnotgoingtotellyou'
storeFile file('C:/whereever/mycompany.keystore')
storePassword 'yeps'
}
}
compileSdkVersion 25
buildToolsVersion '25.0.3'
defaultConfig {
jackOptions {
enabled true
additionalParameters('jack.incremental': 'true')
}
applicationId "com.mycompany.default"
minSdkVersion 14
targetSdkVersion 23
versionCode 44
versionName "2.1.44"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8 }
signingConfig signingConfigs.config
}
dexOptions {
javaMaxHeapSize "4g"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.config
}
}
productFlavors.whenObjectAdded { flavor ->
flavor.ext.set('directoryPath', '')
flavor.ext.set('apkName', '')
}
productFlavors {
Flavor1 {
signingConfig signingConfigs.config
directoryPath = 'flavor1'
}
Flavor2 {
applicationId 'com.mycompany.flavor2'
signingConfig signingConfigs.config
directoryPath = 'flavor2'
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def path = "C:/AndroidBuilds/MyBuild.Build/" + variant.productFlavors[0].directoryPath + "/"
logger.error("Path = " + path)
def SEP = "-"
def apkName = variant.productFlavors[0].apkName
def flavor = variant.productFlavors[0].name
if (apkName != '')
flavor = apkName;
def version = variant.versionCode
def newApkName = path + version + SEP + flavor
logger.error("newApkName = " + newApkName)
output.outputFile = new File(newApkName + ".apk")
}
}
}
dependencies {
compile project(':androidpdfview100')
compile 'com.android.support:support-v13:25.3.1'
compile 'com.google.code.gson:gson:2.7'
compile 'joda-time:joda-time:2.5'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile files('libs/httpmime-4.2.5.jar')
compile files('libs/itextg-5.4.4.jar')
compile files('libs/js.jar')
compile files('libs/logentries-android-2.1.1.jar')
compile files('libs/universal-image-loader-1.8.6.jar')
compile files('libs/xmlworker-5.4.4.jar')
compile files('libs/zbar.jar')
compile('com.crashlytics.sdk.android:crashlytics:2.6.8#aar') {
transitive = true;
}
}
UPDATE
The project structure looks like this:
It was generated automatically by Android Studio when I ported the code from Eclipse:
When you drill into the App itself it looks like this:
For anyone facing this issue in the future, for the command line build make sure your JAVA_HOME is set to the same version as Android Studio is using. E.g. JDK v8:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112
Or wherever your 1.8 JDK is. It is a beast and sucks up memory, but it was the only way to get around this build error. I would NOT recommend that you build in Android Studio with 1.8 since it seems to require Jack which is just plain slow. And sadly it is deprecated to boot.
I work on macOS with Zsh as my shell and have just downloaded Android Studio 3.6.2.
I added the following to my ~/.zshenv file, and everything worked.
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"
I believe updating ~/.profile would do it as well, but I haven't tried it.

jitpack.io dependencies are not working

I cannot get jitpack.io dependencies to work with gradle. I know there are other questions but none of the suggested tips helped. The problem is, jitpack.io dependencies are not working at all. In theory it should be simple... add the mavel { url ... } and the compile declarative. But i keep getting an error that the dependency cannot be resolved.
I updated android-studio, which suggested to update and then updated gradle. Since every jitpack.io dependency is not working, it must be related to the configuration of gradle overall. Unfortunately i have no clou where to start.
I tried different repositories, so that should not be the problem. I also checked the corresponding build logs.
Can anybody help me?
This are my whole gradle files, they should be fine.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "http://jitpack.io" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
and
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "patrickstummer.com.bonario_internal"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.0.0-alpha2'
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
compile 'com.github.soroushjavdan:ApplicationLocker:62301ce0b4'
}
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add the dependency
dependencies {
compile 'com.github.User:Repo:Tag'
}

Import swagger-codegen project into existing Android project

Im trying to integrate a "module"-project generated by swagger-codegen, into my Android project.
Haven't worked that much with gradle before and the swagger-codegen creates a quite messy build.gradle from my point of view.
I have a hard time finding documentation on how to do this. And I feel a bit lost.
I used this method described in the FAQ
mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i http://petstore.swagger.io/v2/swagger.json \
-l java --library=okhttp-gson \
-o /var/tmp/java/okhttp-gson/
So fare I tried to copy the source from the project that was generated by swagger-codegen and merge the two gradle build files. I removed the Junit tests because I couldn't get the Junit dependency working (Implementing Swagger-codegen project - Error:(23, 17) Failed to resolve: junit:junit:4.12). But then I got stuck with some conflict between the plugins?
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Here's the build.gradle:
import static jdk.nashorn.internal.runtime.regexp.joni.ApplyCaseFold.apply
apply plugin: 'idea'
apply plugin: 'eclipse'
group = 'io.swagger'
version = '1.0.0'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// classpath 'com.android.tools.build:gradle:1.5.+'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
repositories {
jcenter()
maven { url 'http://repo1.maven.org/maven2' }
}
if(hasProperty('target') && target == 'android') {
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
// Rename the aar correctly
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "\u0024{project.name}- \u0024{variant.baseName}-\u0024{version}.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
dependencies {
provided 'javax.annotation:jsr250-api:1.0'
}
}
afterEvaluate {
android.libraryVariants.all { variant ->
def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
task.description = "Create jar artifact for ${variant.name}"
task.dependsOn variant.javaCompile
task.from variant.javaCompile.destinationDir
task.destinationDir = project.file("${project.buildDir}/outputs/jar")
task.archiveName = "${project.name}-${variant.baseName}-${version}.jar"
artifacts.add('archives', task);
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
} else {
apply plugin: 'java'
apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
install {
repositories.mavenInstaller {
pom.artifactId = 'XxxxXxxx'
}
}
task execute(type:JavaExec) {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
}
}
dependencies {
compile 'io.swagger:swagger-annotations:1.5.8'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
compile 'com.google.code.gson:gson:2.6.2'
compile 'joda-time:joda-time:2.9.3'
// testCompile 'junit:junit:4.12'
}
Am I doing something complete wrong here? What is the correct way to implement swagger-codegen code into my project?
The swift way to import it was to compile the swagger generated project then copy the .jar file to my android project and add its as a library.
I have a hard time finding documentation on how to do this. And I feel a bit lost.
You could clone the Android swagger-codegen example.
(which does use Junit, so I'm not sure what error you got)
Unless that's what you mean by
So far I tried to copy the source and merge the two gradle build files
To which, I ask, what two Gradle files? It looks like you merged an Android Gradle file with a Java Gradle file, which seems to causing more issues because you are getting...
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Which seems pretty self explanatory when you have this line
apply plugin: 'java'
It's not too clear what you are trying to do here other than check the build target
if(hasProperty('target') && target == 'android')

Gradle project sync failed. Basic functionality will not work properly

I updated to Android Studio 1.2 and now have a two errors.
When I first open my project I get this error at the top of the screen.
"Gradle project sync failed. Basic functionality will not work properly."
In the Messages Gradle Sync I get
My gradle files are:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
and
apply plugin: 'com.android.application'
android
{
signingConfigs {
config {
keyAlias 'ScoularAndroidKey'
keyPassword 'password'
storeFile file('C:/Users/bschmiedeler/.AndroidStudio/scoularkeystore.jks')
storePassword 'password'
}
}
name="Scoular"
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.scoular.scoular"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug{
debuggable true
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/commons-lang-2.3.jar')
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile files('src/main/assets/library-1.0.10.jar')
}
By looking through Stack Overflow I have found some potential solutions.
I have tried File ==> Invalidate and Restart and removing the files that you are supposed to. I am just stuck. Any help would be greatly appreciated.
This is a confirmed bug with Android Studio 1.2:
https://code.google.com/p/android/issues/detail?id=160899
The main solution is to wait for their fix.
I have had luck re-importing the project, but I have to do it every time I open the project.

Android Studio 0.5.3 cannot resolve Android classes

I've recently updated to Android Studio 0.5.3 and then to 0.5.4. From version 0.5.3 it cannot resolve Android classes. Compile is OK but IDE autocomplete is broken and big part of sources is now marked as error. Looks like this:
Root build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
Project's build.gradle:
buildscript {
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
}
dependencies {
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
}
}
apply plugin: 'android'
apply plugin: 'crashlytics'
repositories {
maven { url 'http://download.crashlytics.com/maven' }
}
android {
compileSdkVersion 14
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 14
targetSdkVersion 14
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.squareup.okhttp:okhttp:1.3.0'
compile 'com.google.code.gson:gson:2.2.+'
compile 'com.android.support:appcompat-v7:+'
compile 'com.squareup.picasso:picasso:2.2.0'
compile 'com.google.android.gms:play-services:4.0.30'
compile 'com.crashlytics.android:crashlytics:1.+'
}
I don't think it is a gradle issue, as build completes successfully. What can be incorrect? Or how can I fix this?
Fixed the issue by specifying following line in project's iml:
<orderEntry type="jdk" jdkName="Android API 14 Platform" jdkType="Android SDK" />
instead of:
<orderEntry type="jdk" jdkName="Maven Android 4.0 Platform" jdkType="Android SDK" />
Sometimes it reverts back. It's a bit annoying but better than having red sources and no code assist.

Resources