简体   繁体   English

Android:在JAR中添加资源文件

[英]Android:add resource file in JAR

I have planned to create an SDK in android, that means to share my class files and resource(layouts) to another application that requires them. 我计划在android中创建一个SDK,这意味着将我的类文件和资源(布局)共享给另一个需要它们的应用程序。 If I export my application as JAR it does not include the resource files and R.class. 如果我将我的应用程序导出为JAR,则它不包含资源文件和R.class。 I know currently Android does not support to packaging resource file via JAR but I searched the net and found one solution if compile our resource files with source code means we can access the resource in some other application. 我知道目前Android不支持通过JAR打包资源文件但是我在网上搜索并找到了一个解决方案,如果使用源代码编译我们的资源文件意味着我们可以访问其他应用程序中的资源。

So for that reason I did it in the following way: 因此,我以下列方式做到了:

  1. copied all res folders content into my source folder 将所有res文件夹内容复制到我的源文件夹中

    • src ->com.mypackege -> res--->here i put my res folder src - > com.mypackege - > res --->这里我把我的res文件夹
    • finally my src folder look like 最后我的src文件夹看起来像

    • src SRC

      com.mypackage com.mypackage

        >myfile.java drawable-hdpi drawable-ldpi drawable-mdpi drawable-xhdpi com.mypackage.drawable com.mypackage.layout com.mypackage.menu com.mypackage.values 
  2. and i build my project i got all class files including resource and r.class in my bin 我构建我的项目我得到了所有类文件,包括我的bin中的资源和r.class

  3. then i create jar manually by jar -cvf mysample.jar com/mypackage/* 然后我通过jar -cvf mysample.jar com / mypackage / *手动创建jar

  4. the final jar contain all classes and resource of my project in side mypackage. 最后一个jar包含我的项目在mypackage中的所有类和资源。

then I add mysample.jar into another application say testapp , here I put the JAR in asset folder(because of layout )and add to build path. 然后我将mysample.jar添加到另一个应用程序说testapp ,这里我把JAR放在资产文件夹中(因为布局)并添加到构建路径。 And also I configure the build path 我还配置了构建路径

In this application I call the myfile.class from mysample.jar which is needed to load the layout from that JAR. 在这个应用程序中,我从mysample.jar调用myfile.class ,这是从该JAR加载布局所需的。

The problem is here it doesn't see resource under the JAR file it only see the resource in current project res folder and though NULL pointer Exception 问题是在这里它没有看到JAR文件下的资源它只看到当前项目res文件夹中的资源而且是NULL pointer Exception

But it calls the R.java from JAR file. 但它从JAR文件调用R.java。 Ad also i tried to put xmlns:custom="http://schemas.android.com/apk/res-auto" in my manifest file it is also not working. 广告也尝试将xmlns:custom =“http://schemas.android.com/apk/res-auto”放入我的清单文件中,它也无效。 I hope I exactly come to close this issue, it may be a resource mapping problem. 我希望我能完全解决这个问题,这可能是资源映射问题。 do you have any idea to resolve this? 你有什么想法解决这个问题吗?

You can't store resources in jar. 你不能在jar中存储资源。 It's possible, but it's tricky, so for now Android provides just Android Library. 这是可能的,但它很棘手,所以现在Android只提供Android库。 Otherwise you can use compiled source code and resourses separately (by providing resources ID's to your library) 否则,您可以单独使用已编译的源代码和资源(通过向您的库提供资源ID)

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

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