Gradle Javadoc fails with "error: package ... does not exist" - android

Our Gradle build script fails when building Javadocs for Android Studio project for a library we develop, with errors like:
/MY_LOCAL_PATH/MyClass.java:5: error: package com.google.gson does not exist
import com.google.gson.Gson;
Details:
We are building an Android library with Gradle, and want to build the Javadocs for it.
The Android Studio project is configured to require a Maven dependency for GSON, although I'm guessing it would happen with every dependency whose lib file is not provided explicitly.
One of our classes, of course, imports com.google.gson.
Here is our Gradle script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 19
buildToolsVersion '21.1.2'
defaultConfig {
minSdkVersion 19
targetSdkVersion 19
versionCode 8000
versionName "0.8.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.2.4'
}
And the task that's expected to build the docs (based on this question):
task generateJavaDocs(type:Javadoc) {
source = 'src/main/java/com'
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(source) + files(ext.androidJar)
options.memberLevel = JavadocMemberLevel.PUBLIC
include '**/*.java'
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
When reaching this task, Gradle fails with these errors:
/MY_LOCAL_PATH/MyClass.java:5: error: package com.google.gson does not exist
import com.google.gson.Gson;
/MY_LOCAL_PATH/MyClass.java:6: error: package com.google.gson does not exist
import com.google.gson.JsonParseException;
/MY_LOCAL_PATH/MyClass.java:7: error: package com.google.gson does not exist
import com.google.gson.JsonSyntaxException;
/MY_LOCAL_PATH/MyClass.java:8: error: package com.google.gson.reflect does not exist
import com.google.gson.reflect.TypeToken;
Anyone has a thoughts on how to solve this?
Thanks!

I was working on java-library project built with gradle and I had a similar issue with gradle javadocs task:
task javadocs(type: Javadoc) {
source = sourceSets.main.java.srcDirs
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
My library dependency was 'org.json:json:20180130' and my javadocs task was failing with error:
error: package org.json does not exist
I fixed it by adding classpath to javadocs task like this:
task javadocs(type: Javadoc) {
source = sourceSets.main.java.srcDirs
classpath = sourceSets.main.runtimeClasspath
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
If you are working with Android plugin instead of standard java library you can get the classpath like this:
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
I hope this helps someone.

Related

Android Studio after upgrade errors "Gradle sync failed: ..."

My app build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.marcus.exceltest"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'org.apache.poi:poi:4.1.1'
implementation 'org.apache.poi:poiooxml:4.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:multidex:1.0.3'
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
}
My Gradle build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
My Gradle.properties
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
The Error:
ERROR: Gradle DSL method not found: 'implementation()'
Possible causes:
The project 'Exceltest' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 3.5.2 and sync project
The project 'Exceltest' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file
The build file may be missing a Gradle plugin.
Apply Gradle plugin
The Event Log:
18:27 Gradle sync failed: Could not find method implementation() for arguments [androidx.appcompat:appcompat:1.1.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. (950 ms)
18:27 NDK Resolution Outcome: Project settings: Gradle model version=5.4.1, NDK version is UNKNOWN
18:28 Error running 'Exceltest': Gradle project sync failed. Please fix your project and try again.
Everything worked before I upgraded to Android Studio v3.5.2, tried all suggestions offered by AS but I'm having difficulty in trying to update manually, both the gradle upgrade and the tools upgrade. I'm very new to AS, I have looked at previous answers but they relate to much older versions or file systems do not exist in my version. Please can someone help?
You wrote it in wrong place implementation androidx.appcompat:appcompat:1.1.0 remove this line from your gradle build.gradle below gradle classpath
There is an error in your project's build.gradle file. It should look like this
// Top-level build file where you can add configuration options common to all sub- projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Everything seems fine but,
1. Some Library you migrated into Androidx and some not so i request you to migrate your project's all library into androidx.
2. Remove implementation line from your project level gradle file or move it to App level gradle file.

DSL method not found: implementation()

I tried to add the Fresco library by to my project by adding this to my app-level gradle file
implementation 'com.facebook.fresco:fresco:1.8.0'
Then I get the follwing error message:
Gradle DSL method not found: 'implementation()' Possible causes:
The project may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0). Upgrade plugin to version 3.0.1 and sync project
The project may be using a version of Gradle that does not contain the method. Open Gradle wrapper file
The build file may be missing a Gradle plugin. Apply Gradle plugin
When I run gradle --version it tells me that I am using gradle 4.5.
This is my top-level gradle file.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.google.gms:google-services:3.1.0'
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "http://dl.bintray.com/populov/maven"}
maven { url "https://jitpack.io" }
maven { url "https://maven.google.com" }
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This is my app-level gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.0'
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
defaultConfig {
applicationId "com.example.project1.3"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "state1.0"
renderscriptTargetApi 24
renderscriptSupportModeEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// lots of other dependencies
implementation 'com.facebook.fresco:fresco:1.8.0'
}
I also have this in my gradle-wrapper.properties file.
distributionUrl= https\://services.gradle.org/distributions/gradle-4.1-all.zip
I have basically tried everything from here https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html but the problem persists.
Any help would be greatly appreciated. Thank you!
My guess is that probably one of your other dependencies is causing the issue,
I run the exact same app & project gradle files you provided in a new project and synced with no problem.
You can try the following:
1) substitute implementation 'com.facebook.fresco:fresco:1.8.0' with compile'com.facebook.fresco:fresco:1.8.0' (deprecated but it will help with the troubleshooting) and see if it will sync properly.
2) start a new project and use the same gradle file and see if the problem still exists.
If nothing works please post the complete app level gradle file to check again.

android : cannot build project after upgrading Android Studio to 2.3

So, happily updated my Android Studio from stable version 2.2 to canary channel 2.3.
But, unfortunately, it has serious bug. Project is not able to build.
I tried opening my previous projects and also tried creating new sample application, I got same below error:
Gradle 'MyApplication' project refresh failed
Error:A problem occurred configuring project ':app'.
build.gradle(project):
// 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:2.3.0-alpha1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.chint.myapplication"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
compile 'com.android.support:design:25.0.0'
testCompile 'junit:junit:4.12'
}
Am I the only one with this issue ?
So, be careful while this dialog appears:
Choose "Remind me later" and NOT "Update"
Stay with the build version you already have.
Incase, you pressed Update, so just rollback your Android Gradle Plugin classpath in build.gradle(project) to below one:
classpath 'com.android.tools.build:gradle:2.2.2'
This worked for me.
You should update your Android Studio gradle manually.
1° Step:
Follow this path and get your gradle version
C:\Users\[USER_NAME]\.gradle\wrapper\dists
Here you will get "gradle-X.X" (X- is version) in my case it was "gradle-3.3-all"
2° Step:
Go to the following link :
https://services.gradle.org/distributions/
Download your respective version zip file and just past it in the last folder with long random name.
C:\Users\[USER_NAME]\.gradle\wrapper\dists\gradle-x.x\55gk2rcmfc6p2dg9u9ohc31212 HERE
Now open the Android studio, I hope this will work.
I fought a similar issue and resolved it with the addition of 2 characters...
And after updating the gradle-wrapper.properties to target 3.2.1 everything was great for me
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip
If you import external libraries apply changes for "allprojects"
// 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:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

Android: Cannot include https://github.com/wealdtech/hawk as a module

I am trying out Hawk authentication by https://github.com/wealdtech/hawk. I would like to include this library by source in an empty project so that I can experiment with the apis. I do not want to use a jar or gradle dependency.
I import the project as a module and I run into this error:
Error:(2, 0) Gradle DSL method not found: 'compile()'
Possible causes:<ul><li>The project 'HawkTest' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file</li><li>The build file may be missing a Gradle plugin.
Apply Gradle plugin</li>
I tried solutions from these links but I could not derive any information that could help resolve this issue:
Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'.
Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'.
I have spent many hours on this problem but do not seem anywhere near a solution. Any direction or a solution would be greatly appreciated.
This my top-level build file:
// 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:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
The app level file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.test.android.hawktest"
minSdkVersion 16
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'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
The Hawk module's gradle file:
dependencies {
compile 'com.wealdtech:wealdtech-core:2.0.0'
compile 'com.wealdtech:wealdtech-configuration:2.0.0'
compile 'com.google.guava:guava:17.0'
}
uploadArchives {
repositories {
mavenDeployer {
pom.project {
pom.artifactId = 'hawk-core'
name 'Hawk Core'
description 'Java implementation of Hawk protocol - core'
}
}
}
}
And my directory structure:
hawk top level build.gradle contains all subproject configuration (check https://github.com/wealdtech/hawk/blob/develop/build.gradle)
For the whole process, from your project root directory :
git clone git#github.com:wealdtech/hawk.git
In your settings.gradle, this will configure the gradle modules :
include ':app',':hawk-core', ':hawk-server-jersey', ':hawk-client-jersey'
project(':hawk-core').projectDir = new File('hawk/hawk-core')
project(':hawk-server-jersey').projectDir = new File('hawk/hawk-server-jersey')
project(':hawk-client-jersey').projectDir = new File('hawk/hawk-client-jersey')
Then edit your top level build.gradle to specify the configuration for the Java project. Add the following :
configure(subprojects.findAll {it.name.startsWith('hawk')}) {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'signing'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testCompile 'org.testng:testng:6.8'
}
task copyLibs (type: Copy) {
into "$buildDir/output/libs"
from configurations.testRuntime
}
task testJar (type: Jar) {
classifier = 'tests'
from sourceSets.test.output
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from 'build/docs/javadoc'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives jar
archives javadocJar
archives sourcesJar
}
}
You cant add Java plugin (apply plugin: 'java') for your Android project . If you do so, you will have this error : The 'java' plugin has been applied, but it is not compatible with the Android plugins.. This is why I use the configure(subprojects.findAll and perform a filter on hawk modules. I've copied the remaining configuration from https://github.com/wealdtech/hawk/blob/develop/build.gradle

Using Maven Inside of Android Studios

I have been doing heavy searching on this for a few days now. I find results but it ether applies to an older version or doesn't apply to my situation.
I have a Maven Project That I built inside Eclipse. I've done a Maven install and Maven build via Eclipse. I use the Maven project with another Java Application and it works fine once I run the install.
I want to use the same maven project inside of Android Studio. I pretty sure I need to do something within build.gradle but there are 2 different ones and I'm not sure which one the dependencies need to be placed in or what needs to go where.
I get no errors when I'm writing the code and the problem happens during run-time with missing class definitions
I'm not sure how to tell the gradle to download the required dependencies
app (build.gradle):
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.soleo.api.sdk_demo"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'SoleoLocalSearchAPI:SoleoLocalSearchAPI:0.0.1-SNAPSHOT'
}
SDK_Demo (build.gradle)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
MainActivity.java:
package com.soleo.api.sdk_demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.soleo.LocalSearch;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String api = "XXXXXXXXXXXX";
String ani = "##########";
LocalSearch localSearch = new LocalSearch(api, ani);
}
}
I think the problem is related to your dependency SoleoLocalSearchAPI. Please ensure that this dependency has been installed to your local maven repository. If you want to download this dependency from another remote repository, you have to define it as follows :
buildscript {
repositories {
jcenter()
maven { url 'http://blablabla.com' }
}
.......
}

Categories

Resources