4

I wish to use jsoup library in Android Open Source Project. For this I did two things:-

Step 1:

  1. Made a directory jsoup in common as follows: [android]prebuilts/misc/common/jsoup/
  2. In this jsoup folder I added jsoup-1.13.1.jar downloaded from : https://jsoup.org/download
  3. In this same jsoup folder I added another file Android.mk {as it is name and code below:}

    LOCAL_PATH := $(call my-dir)  
    include $(CLEAR_VARS)  
    LOCAL_MODULE := jsoup
    LOCAL_MODULE_TAGS := optional
    LOCAL_SRC_FILES := jsoup-1.13.1.jar
    LOCAL_MODULE_CLASS := JAVA_LIBRARIES
    LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
    LOCAL_DEX_PREOPT := false
    include $(BUILD_PREBUILT)

Step 2:

  1. Go to another make file as follows : [android]/frameworks/base/Android.mk
  2. In this Android.mk file I added following line shown below (after the LOCAL_JAVA_LIBRARIES variable is set): LOCAL_STATIC_JAVA_LIBRARIES += jsoup

Problem:

  1. I have added a file sample.java as follows- [android]frameworks/base/services/core/java/com/android/server/am/sample.java
  2. In this sample.java file I have import statements as follows-
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.select.Elements;
  1. When I build this whole modified android code, I get the following error and the build fails:
frameworks/base/services/core/java/com/android/server/am/sample.java:48: error: package org.jsoup does not exist  
frameworks/base/services/core/java/com/android/server/am/sample.java:49: error: package org.jsoup.nodes does not exist  
frameworks/base/services/core/java/com/android/server/am/sample.java:50: error: package org.jsoup.select does not exist
  1. I even tried using a method mentioning Android.bp instead of Android.mk but same build error.
  2. Also I am confused in naming of jar file as import statement includes org.jsoup... but jar file itself is just jsoup. (Do I have to change import statement like import jsoup ... or change name of jar file to org.jsoup...)
  3. Any other method other than involving Android.mk or Android.bp is also highly acceptable.

Please, I have no idea of including external jar files in AOSP. The code works fine locally in IntelliJ IDE Java otherwise. Any help is deeply appreciated. Thanks in advance.

Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53
Aditya Gupta
  • 63
  • 1
  • 6

2 Answers2

3

You can base your solution on some other jar already added in AOSP.
For example see ZXING library's Android.bp in external/zxing/:

java_import {
    name: "zxing-core-1.7",
    host_supported: true,
    installable: false,
    jars: ["core.jar"],
}

Then see the usage on another Makefile: :

LOCAL_STATIC_JAVA_LIBRARIES := \
    androidx-constraintlayout_constraintlayout-solver \
    androidx.lifecycle_lifecycle-runtime \
    androidx.lifecycle_lifecycle-extensions \
    guava \
    jsr305 \
    carsettings-contextual-card-protos-lite \
    carsettings-log-bridge-protos-lite \
    carsettings-logtags \
    statslog-settings \
    zxing-core-1.7

Lastly, based on the path of the file you're adding frameworks/base/services/core/java/com/android/server/am/sample.java, you're adding the library to the wrong makefile.

The one you changed (frameworks/base/Android.mk) is for making framework.jar, whereas you're trying to add a class on services.jar

The correct makefile to modify would be: frameworks/base/services/core/Android.bp

Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53
  • Thank you so much @RickSanchez for guidance and correcting post being first at Stack Overflow. Based upon your answer I was able to come up with proper solution and a successful build, which I will post "How I did it" in thread below. – Aditya Gupta Nov 02 '20 at 10:51
  • You're welcome. Glad the problem got solved. But adding your own answer ( which is almost the same as this ) and accepting that instead of this is bad form. Besides, you won't get the ++score for your own answer. – Rick Sanchez Nov 02 '20 at 13:37
  • 1
    I do understand your query and am learning in process. I have rectified my mistake in both accepting the solution as well as modification of my answer post by crediting the real solution. Apology for inconvenience caused and Thanks for help. – Aditya Gupta Nov 03 '20 at 06:50
  • Your solution is working. But: In my case I'm adding a dependency x (.jar) which also depending on other dependencies. For example dependency y and z. So, do I have to name all of those (x,y,z), like in your example? In my case, following your example, it builds without error, but on runtime it gives an error message telling me dependency y is missing/not found. – icouldin Jan 11 '21 at 13:26
2

Based ENTIRELY upon Answer provided by @RickSanchez, I followed these steps as guided by his answer to bring up a successful build:

  1. Made a directory: [android]prebuilts/misc/common/jsoup/
  2. In this directory jsoup folder, I included a blueprint file Android.bp and jsoup-1.13.1.jar downloaded from https://jsoup.org/download
  3. With the help of Blueprint Soong Flag descriptions info here, I created my Android.bp talked above as follows:

Android.bp

    java_import {
    name: "jsoup-1.13.1",
    host_supported: true,
    installable: false,
    jars: ["jsoup-1.13.1.jar"],
    }
  1. Next task was to give ref of above created Local Blueprint file to Android.bp present in frameworks/base/services/core/Android.bp

  2. To this, I made the following edit: In Android.bp either add the following if present or modify the following block -

     static_libs: [
     "jsoup-1.13.1", /*use jsoup lib */
     "android.hardware.authsecret-V1.0-java",
     ....
     ....
     ],
    
  3. The build is now successful and the sample.java is successfully able to use jsoup library

Aditya Gupta
  • 63
  • 1
  • 6