Add .jar dependency to Android build

963 views
Skip to first unread message

MinhHoang To

unread,
Jul 17, 2015, 5:21:04 AM7/17/15
to chromi...@chromium.org
What is the canonical way to add a .jar dependency into Chromium build?

I got that there is 'third-party' directory but it is for adding third-party project with full source code.  There is no .gyp (or .gypi) file where i can declare a pre-built .jar from Maven repository.

I noticed that there is 'lib.java' under out/Debug and out/Release but the build/packaging process of Chromium does not taken into account my .jar when i added it into that folder.


Anthony Berent

unread,
Jul 17, 2015, 5:43:43 AM7/17/15
to hoang...@gmail.com, chromi...@chromium.org
third_party also contains some pre-built jars. Have a look at, for example, third_party/android_tools/android_tools.gyp, which creates targets for various android sdk jars.

Remember to add your jars to GN build as well as the GYP build.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

MinhHoang To

unread,
Jul 18, 2015, 4:34:01 AM7/18/15
to chromi...@chromium.org, hoang...@gmail.com
https://code.google.com/p/chromium/codesearch#chromium/src/build/java_prebuilt.gypi

@aberent:

It is sufficient to create a folder containing .jar from Maven repo, and a .gyp file including java_prebuilt.gypi

Anton Vayvod

unread,
Jul 19, 2015, 8:45:19 AM7/19/15
to MinhHoang To, chromium-dev
Yes, if your .jar file doesn't come with extra resources, you add a build target like this:

    {
      # This jar contains the library I need to use.
      'target_name': 'name_of_your_library_javalib',
      'type' : 'none',
      'variables': {
        'jar_path': 'path/to/your/library.jar',
      },
      'includes': ['../../build/java_prebuilt.gypi'],
    },

If it contains resources, however, you need a two step process currently (there's a tracking bug to make it easier):

    {
      # This jar contains the library I need to use.
      # This library doesn't contain the resources needed for the library to work.
      'target_name': 'my_library_javalib_no_res',
      'type' : 'none',
      'variables': {
        'jar_path': 'path/to/your/library.jar',
      },
      'includes': ['../../build/java_prebuilt.gypi'],
    },
    {
      # This target contains my library with the resources needed.
      'target_name': 'my_library_javalib',
      'type': 'none',
      'variables': {
        'java_in_dir': 'path/to/directory/containing/res/directory',
        'R_package': ['my.library.package'],
        'R_package_relpath': ['my/library/package'],
        'has_java_resources': 1,
        'res_v14_skip': 1,
        'run_findbugs': 0,
      },
      'dependencies': [
        'my_library_javalib_no_res',
      ],
      'includes': [ '../../build/java.gypi' ]
    },

Your directory structure usually would be like this:

src/third_party/my_library/my/library/package/
- libs/ # where the .jar file(s) are
- res/ # where the resources are
- src/ # an empty src directory with a hidden .readme file to guarantee directory existence

Note that build/java.gypi requires src/ directory at the same level as the res/ directory.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

MinhHoang To

unread,
Jul 20, 2015, 10:51:17 AM7/20/15
to chromi...@chromium.org, ava...@chromium.org
@Anton:

Is there any process (either direct or hack) for .aar file?

In my case, i need to integrate Fabric SDK (shipped as .aar file) and what i am trying to do is to add its .jar
and its resource (file containing API key, secret)

Anton Vayvod

unread,
Jul 20, 2015, 11:24:22 AM7/20/15
to MinhHoang To, chromium-dev
No, not that I know of. You could unarchive the .aar into a jar and folders and use the approach I described or create a new "java_archived" build rule.

Himanshu Srivastava

unread,
Jan 12, 2016, 2:14:13 AM1/12/16
to Chromium-dev, hoang...@gmail.com, ava...@chromium.org

Thanks for the help.
I added jar file of exoplayer  in file android_tools.gyp
   {
      # Exoplayer implementation
      'target_name': 'exoplayer_java',
      'type' : 'none',
      'variables': {
        'jar_path': '<(android_sdk_root)/exoplayer.jar',

      },
      'includes': ['../../build/java_prebuilt.gypi'],
    },

Also , I placed the exoplayer.jar file .

Can I now call function of exoplayer in chromium code.
Actually I want to call exoplayer for media playback of mpeg-ts format from chromium
Thanks

Anton Vayvod

unread,
Jan 12, 2016, 5:59:35 PM1/12/16
to Himanshu Srivastava, Chromium-dev, MinhHoang To
On Tue, Jan 12, 2016 at 7:14 AM, Himanshu Srivastava <28him...@gmail.com> wrote:

Thanks for the help.
I added jar file of exoplayer  in file android_tools.gyp
   {
      # Exoplayer implementation
      'target_name': 'exoplayer_java',
      'type' : 'none',
      'variables': {
        'jar_path': '<(android_sdk_root)/exoplayer.jar',
      },
      'includes': ['../../build/java_prebuilt.gypi'],
    },

Also , I placed the exoplayer.jar file .

Can I now call function of exoplayer in chromium code.

Well, can you? Did you try to regenerate ninja files and compile? You need to add a dependency on your new target in some of the existing targets that will use it (e.g. track where MediaPlayerBridge.java is included). Then you can try using the package the same way other packages are used in Java.

Anton Vayvod

unread,
Jan 13, 2016, 6:41:24 AM1/13/16
to Himanshu Srivastava, chromium-dev
Please, keep chromium-dev in the loop.

On Wed, Jan 13, 2016 at 4:28 AM, Himanshu Srivastava <28him...@gmail.com> wrote:
Hi Anton ,

I added classpath ( runtime_classpath) of my jar ( exoplayer.jar) in the file: build/gyp/javac.py.
After this code is compiling fine.

This sounds like a hack. One shouldn't need to modify javac.py for this. What error was it failing with before?
 

But the jar is not going to chromium apk.

What does it mean? Can you share the error you see?
 
How would I ensure that exoplayer.jar is also going to apk.

As I said before, you need to add the dependency in the gyp/gn files for the appropriate module that will use your jar. In this case I suspect it will be media_java

Check out how gyp and gn work. Just adding a new target (exoplayer_java) doesn't make it part of the target you're building (chrome_public_apk). One need to depend on the other directly or via some other dependency.
 
Also do I need to generate .dex and .md5 file for this jar also.

No, I don't think so.
 

Thanks & Regards
Himanshu

vipin...@gmail.com

unread,
Oct 24, 2018, 11:42:18 AM10/24/18
to Chromium-dev
  I added jar file in build.gn. After that what will be do? I don't know.
  I am new in Android-Chromium.

  Thnaks in advance.
Reply all
Reply to author
Forward
0 new messages