Android Studio: How to attach javadoc
Intellij IdeaGradleAndroid StudioIntellij Idea Problem Overview
It might be very trivial question, But I couldn't find any option to attach javadoc/source with local jar dependencies (in libs folder) in android project. I can't believe I have spent a hour on such simple stuff :(
Google search result just tells about adding android documentation or adding javadoc in eclipse. That's not What I am looking for!
Intellij Idea Solutions
Solution 1 - Intellij Idea
I found a solution just now since I can't find any other solution for a long time.
suppose:
- your lib name is: libxxx.jar
- javadoc name is docs.zip
under folder .idea/libraries, you can find libxxx.xml. replace JAVADOC with
<JAVADOC>
<root url="jar://C:/yourpath/doc.zip!/" />
</JAVADOC>
then ctrl+alt+y to sync the project. (Don't do "Sync Project with Gradle files", it will delete the changes)
Solution 2 - Intellij Idea
I've found & tested a way in which we can add the javadocs without loosing them after a gradle sync.
- Right click in Project panel > External libraries > lib-name
- Click Library properties
- Click specify documentation URL
- And specify an URL. For my machine it was
file:///opt/android-sdk/extras/google/google_play_services/docs/reference
- The result was:
- And after a project sync I could browse the javadoc of the google play services
Note:
This placed the same JAVADOC entry in the xml identified by Jason but doesn't disappear after doing a sync with gradle
Solution 3 - Intellij Idea
It is possible:
- Open
Project structure
- Navigate to dependency that you want
- Press button from the screenshot and provide folder or file
Another way to attach sources:
- Step into class from library (Hover mouse over class name and do CMD + Left click or CMD + B)
- You will see decompiled version of class there menu on the top right side of editor "Attach sources.."
Solution 4 - Intellij Idea
I had problem with answer from Matyas that I wasn't seeing my local .jar library in the External Libraries list, because it only show maven attached libs.
Solution is mentioned here: https://code.google.com/p/android/issues/detail?id=73087#c26
- Right click on the "Structure" tab and check "Split Mode" (so you can open both "Project" and "Structure" tabs at same time).
- Open both "Project" and "Structure" tabs at the same time.
- In "Project" tab select "Android" perspective and then select your module folder (e.g. "app")
- In "Structure" window you should now see list of all libraries, including your local *.jar
Continue as in answer from Matyas:
- Right click on wanted library and select "Library Properties..."
- If you have *.jar with javadocs locally, you can press "add" button (green "+") and search for the file on your disk (you don't have to type "file://" path manually).
Solution 5 - Intellij Idea
Still a current issue as of my posting it seems:
Solution 6 - Intellij Idea
After some investigation, I stumbled upon this:
https://github.com/xujiaao/AARLinkSources
Works like magic!
Solution 7 - Intellij Idea
I wasted so much time on this too...
Here's a gradle task which finds source and javadoc by location/naming convention, and registers them in the .idea files on sync. It belongs in the root gradle file's allProjects section. As-is, it expects to find [projectname]/libs/lib.jar next to lib-sources.jar and/or lib-javadoc.jar. Also, as noted in comments, if your javadocs not pathed at "/" inside the jar, you may need to change the script to add "docs/html" (for example) at the end of "jar://$doc!/".
allprojects {
task addJavaDoc {
afterEvaluate {
// Specify paths, this will be run per non-root project
def projectDir = project.getProjectDir().getCanonicalPath()
def rootDir = project.getRootDir().getCanonicalPath()
def lib = projectDir + '/libs'
// println lib // Uncomment this to troubleshoot
// Get any jar dependencies register in the lib folder
fileTree(include: ['*.jar'], exclude: ['*-source.jar', '*-javadoc.jar'], dir: lib ).each { File jar ->
def jarName = jar.getName()
def moduleName = jarName.substring(0, jarName.lastIndexOf("."))
// IntelliJ does this to file names when making the xml files
def escapedName = moduleName.replace("-", "_").replace(".", "_")
def xmlFile = "$rootDir/.idea/libraries/${escapedName}.xml"
// println xmlFile // Uncomment this to troubleshoot
if (new File(xmlFile).exists()) {
['javadoc', 'sources'].each {String docType ->
// Get sources or java doc by naming convention, (expects name-sources or name-javadoc
def doc = "$lib/$moduleName-${docType}.jar"
// println doc // Uncomment this to troubleshoot
if(new File(doc).exists()) {
def xml = new XmlParser().parse(xmlFile);
def xmlTag = docType.toUpperCase()
// Perform xml replacement by convention
xml.library[xmlTag].replaceNode {
"$xmlTag" {
root(url: "jar://$doc!/")
}
}
// Write out changes
new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
// Notify that changes worked
println "Fixed up reference to $doc"
}
}
}
}
}
}
}
Also, if you are using jcenter or mavencentral, javadocs and sources should work for downloaded jars without using that task, but you may have to add this in each non-root gradle file:
apply plugin: 'idea'
idea{
module {
downloadJavadoc = true
downloadSources = true
}
}
Solution 8 - Intellij Idea
There is a solution, This procedure take place through terminal ,
I have tested solution in MAC OS.
-
Move to your project folder
-
ls -al
(to show hidden files) -
Move to
.idea
folder , Command : cd .idea -
Move to
libraries
folder , Command : cd libraries/ -
Now you can see list of all xml files for your libs or jars. Edit it like ,
vi open androidasync_2_1_7.xml
-
In the editor screen , For inserting
Press i
Now you see <SOURCES />
tag we have to provide a path here like,
<SOURCES>
<root url="file://$PROJECT_DIR$/androidasync/src/main/java" />
</SOURCES>
For exiting
Press Esc
:wq //for exiting and saving
:q! //for exiting without saving
7) Restart Android studio (Sometime it needed also sync gradle).
Solution 9 - Intellij Idea
Personally tested successfully!
1.Project Structure(ctrl+alt+shift+s)
2.SDK Location
3.JDK Location
4.UnCheck "Use embedded JDK(recommended)"
5.Select your own jdk path(My Path:C:\Program Files\Java\jdk1.8.0_111)
6.Synchronized(ctrl+alt+y)
Solution 10 - Intellij Idea
in android studio if you use compileSdkVersion 23
in buidl.gradle and you have downloaded the SDK 23 document file and source file in SDK manager
, the android API and java API doc and source will all show auto, you don't need to manual set.