how to include jar as classpath when building android_library

586 views
Skip to first unread message

Huapeng Li

unread,
May 12, 2016, 1:18:11 AM5/12/16
to gn-dev
I am building an APK. I have a java jar file which is only needed for compile. When building java sources as android_library, I want to use the jar only as a classpath parameter instead of including the .jar file's contents into the APK. However, proguard doesn't work here.

In GYP, we can do
{
      'target_name': 'my_java',
      'type': 'none',
      'variables': {
            'java_in_dir': 'android/java',
            'input_jars_paths': [ '../some/api.jar' ],
      },
      'includes': [ '../../build/java.gypi', ],
},
In build/javac.gypi, it will use input_jars_paths as classpath arg
      'action': [
        'python', '<(DEPTH)/build/android/gyp/javac.py',
        '--bootclasspath=<(android_sdk_jar)',
        '--classpath=>(input_jars_paths)',
        '--src-gendirs=>(generated_src_dirs
        ..
      ]


I look around GN, but the similar option does't seem available. It is reading from the generated build_config.
Does such option exist in GN? I can patch android_library template to accept additional argument input_jars_paths and forward the arg to compile_java template. Do you think it is the right approach?


I also tried to remove the .class from the jar file using the jar_excluded_patterns option in android_library template, 
but it doesn't seem to work. I can still see ClassOne.class and ClassTwo.class in the final jar.

The jar file has
some/api.jar
   com/hello/api/ClassOne.class  (package com.hello.api)
   com/hello/api/ClassTwo.class


android_java_prebuilt("my_jar"){
      jar_path ="some/api.jar"
}

android_library("my_java") {
      deps = [ ":my_jar" ]
      java_files = [...]
      
      jar_excluded_patterns = [
            "*/ClassOne.class",
            "*/ClassTwo.class",
      ]

}

Is this the right usage?


Thanks

Andrew Grieve

unread,
May 12, 2016, 10:02:54 AM5/12/16
to Huapeng Li, gn-dev
I think your understanding is spot on. I don't think there's a way to do that right now, and adding it as an arg to compile_java is probably the way to go.

It's a bit of an odd thing to want to do though. What is the use-case for it?

Huapeng Li

unread,
May 12, 2016, 4:54:01 PM5/12/16
to gn-dev, matth...@gmail.com

I need to call some APIs from the android platform. Those service APIs are declared on our AndroidManifest.xml file. I only have the jar of those APIs(it is stubbed and it does not do anything). I don't need to have the implementation of those APIs,  but I just need it to compile. 

For some of the classes, I can use java reflection to invoke the call; however, reflection won't work when I need to extend some of the abstract classes.

I just patched it with the extra arg and it seems to work. 

Andrew Grieve

unread,
May 16, 2016, 10:12:52 PM5/16/16
to Huapeng Li, gn-dev
Makes sense to me. If you'd like to contribute your patch, I'd be happy to review.

ik...@ludei.com

unread,
Oct 7, 2016, 7:47:39 AM10/7/16
to gn-dev, matth...@gmail.com
Hello,

I am in need of a similar solution. I need to compile against the android-support-v4.jar file but not include it as it seems Chromium also includes it somewhere else and there is a multiple dex files error when the final build is performed.

I have included a android_java_prebuilt dependency and the compilation works but the final APK generation fails.

I do not know how to modify the android_apk template to add the extra arg as you mentioned in your posts (I do not fully understand how .gni files are written).

Any advise on either:

1) How to compile against the android-support-v4.jar file while creating the webview APK officially. If Chromium is including it, there might be a way of doing it.
2) How to include an arg in a GN template to be able to specify just the jar file in the classpath but not include it in the final APK.

Thank you very much.

Regards,
Iker.

Andrew Grieve

unread,
Oct 11, 2016, 2:21:06 PM10/11/16
to Iker Jamardo, gn-dev, Huapeng Li
I'd guess you just want to depend on: //third_party/android_tools:android_support_v4_java

ik...@ludei.com

unread,
Oct 11, 2016, 3:21:15 PM10/11/16
to gn-dev, ik...@ludei.com, matth...@gmail.com
El martes, 11 de octubre de 2016, 20:21:06 (UTC+2), Andrew Grieve escribió:
> I'd guess you just want to depend on: //third_party/android_tools:android_support_v4_java
>

Hello Andrew,

Thank you very much. did not know there was a construction already available (makes much sense).

I have added it and I am getting the following errors:

Regenerating ninja files from GN...
ERROR Unresolved dependencies.
//android_webview/test:android_webview_apk__java__lint(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__create_incremental__package_resources(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__process_resources(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__create__package_resources(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__create_incremental__package(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__create__package(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__build_config(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java__build_config(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__create_incremental_generate_manifest(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__java__compile_java__javac(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)
//android_webview/test:android_webview_apk__java__compile_java__process_prebuilt__copy(//build/toolchain/android:clang_arm)
needs //third_party/android_tools:android_support_v4_java(//build/toolchain/android:clang_arm)

Sorry if this is very obvious, I am still learning about GN.

Your help is much appreciated!

Regards,
Iker

Huapeng Li

unread,
Oct 11, 2016, 3:28:32 PM10/11/16
to ik...@ludei.com, gn-dev
Is your workspace an android workspace? does your .gclient (root of your workspace) file have
    target_os = ["android"]


Iker Jamardo

unread,
Oct 11, 2016, 3:32:16 PM10/11/16
to Huapeng Li, gn-dev
Yes. I have been building Chromium for Android regularly.

Regards,
Iker.
--
Iker Jamardo
CTO @ Ludei
http://ludei.com
156 2nd Street
San Francisco
CA, 94105

Huapeng Li

unread,
Oct 11, 2016, 3:58:36 PM10/11/16
to Iker Jamardo, gn-dev
Is your workspace out of sync? 
android_tools:android_support_v4_java was introduced in this commit

Can you check if the target is in your build/secondary/third_party/android_tools/BUILD.gn?

--
You received this message because you are subscribed to a topic in the Google Groups "gn-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/a/chromium.org/d/topic/gn-dev/Fbr-3WwG4OA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gn-dev+unsubscribe@chromium.org.

Iker Jamardo

unread,
Oct 11, 2016, 8:27:54 PM10/11/16
to Huapeng Li, gn-dev
You are completely right, as I am using the tag 54.0.2796.3 as my baseline, the android_support_v4 has not been added yet.

There is the android_support_v13 though and for what I need it seems that is enough.

Thank you very very much for your help.

Regards,
Iker.

To unsubscribe from this group and all its topics, send an email to gn-dev+un...@chromium.org.

cqpeng...@gmail.com

unread,
May 25, 2017, 6:08:52 AM5/25/17
to gn-dev, matth...@gmail.com
在 2016年5月13日星期五 UTC+8上午4:54:01,Huapeng Li写道:
Hi
I also need this request and have not found the solution,can you please help share the way to include an arg in a GN template to be able to specify just the jar file in the classpath but not include it in the final APK,thanks a lot

huap...@amazon.com

unread,
May 26, 2017, 12:48:23 AM5/26/17
to gn-dev, matth...@gmail.com, cqpeng...@gmail.com
You can do this to include the jars.

android_library("some_java") {
deps =[....]

java_files = [ ...]

input_jars_paths =
rebase_path([
"//jar location"
])

cqpeng...@gmail.com

unread,
May 26, 2017, 5:12:22 AM5/26/17
to gn-dev, cqpeng...@gmail.com, huap...@amazon.com
thanks for your reply but it still can't work

The previous version,
java_prebuilt("test") {
jar_path = "xxx/xxx.jar"
}

android_library("target") {
java_files = [...]

deps = [...
":test"]
}

now exclude the part of java_prebuilt and include the jars in android_library by input_jars_paths

android_library("target") {
java_files = [...]

deps = [...]

input_jars_paths = rebase_path("xxx/xxx.jar")
}

is this the right way as you mentioned?but still fail to find the package in the included jar when compiling.

moha...@gmail.com

unread,
Jun 10, 2019, 4:42:24 PM6/10/19
to gn-dev, cqpeng...@gmail.com

Have you got any solution to your problem? I am also struck with same issue.

Reply all
Reply to author
Forward
0 new messages