简体   繁体   English

如何在没有r.java的情况下构建jar?

[英]How to build a jar without the r.java?

We can't put resource ids (R.java) directly in a jar file. 我们不能将资源ID(R.java)直接放在jar文件中。 If we do there is no way to guarantee they will be unique. 如果我们这样做,就无法保证它们将是唯一的。 But how to compile the jar without the R.java ? 但是如何在没有R.java的情况下编译jar? in my code I do often call like R.style.xxx to obtain the id of xxx but if I don't have the r.java then it's will not compile :( 在我的代码中,我经常调用R.style.xxx类的R.style.xxx来获取xxx的ID,但是如果我没有r.java,它将无法编译:(

i construct my jar like this 我这样构造我的罐子

%JDK_PATH%\javac -cp^
 %ANDROID_PLATFORM%\android.jar;^
 -d source\output^
 source\java\com\mylib\util\r.java^
 source\java\com\mylib\view\myCustomView.java

r.java just a dumy file like this one : r.java只是一个像这样的伪文件:

package com.mylib;

public final class R {

  public static final class dimen {

     public static final int al_floating_toolbar_horizontal_margin=0;
     public static final int al_floating_toolbar_vertical_margin=0;
     public static final int al_floating_toolbar_preferred_width=0;
     public static final int al_floating_toolbar_minimum_overflow_height=0;
     public static final int al_floating_toolbar_maximum_overflow_height=0;
     public static final int al_content_rect_bottom_clip_allowance=0;
     public static final int al_floating_toolbar_height=0;
     public static final int al_floating_toolbar_menu_button_minimum_width=0;

  }

}

then i delete all the R.class after and then i create the jar : 然后我删除所有R.class之后,然后创建jar:

%JDK_PATH1_7%\jar cf lib\jar\mylib.jar -C source\output com\mylib\

If we do there is no way to guarantee they will be unique 如果我们这样做,就无法保证它们将是唯一的

More importantly, there is no guarantee that they will be correct . 更重要的是,不能保证它们将是正确的 Those aren't just random numbers. 这些不仅仅是随机数。 They tie into how the APK is packaged and how to read the resources. 它们涉及APK的打包方式以及如何读取资源。

But how to compile the jar without the R.java ? 但是如何在没有R.java的情况下编译jar?

If this is a library module, you can eliminate all resources from the project (and all references to them from Java). 如果这是一个库模块,则可以消除项目中的所有资源(以及Java中对它们的所有引用)。 Then, you can create a JAR using a custom Gradle task . 然后,您可以使用自定义Gradle task创建一个JAR。

Your only alternative is to avoid all R references in the Java code, using getResources().getIdentifier() to look them up at runtime. 唯一的选择是避免Java代码中的所有R引用,而使用getResources().getIdentifier()在运行时查找它们。 Then, ship the JAR, with the resources outside of the JAR. 然后,使用JAR之外的资源运送JAR。 Apps using the JAR would need to merge the library's resources with the app's own resources. 使用JAR的应用程序需要将库的资源与应用程序自身的资源合并。 Frankly, I would rather that you publish an AAR, as that is easier and safer for both the publisher and the consumer. 坦白说,我希望您发布AAR,因为这对于发布者和消费者而言都更加容易和安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM