如何在不公开源代码的情况下使用 gradle 创建 Android Library Jar? [英] How to create an Android Library Jar with gradle without publicly revealing source code?
问题描述
我想用一个 Android 库项目创建一个 Jar.它的设置方式如下:
I would like to create a Jar out of an Android library project. It is set up the following way:
ProjectName
\- lib
| \- lib
| \- armeabi
| \- libNativeFirst.so
| \- libNativeSecond.so
\- src
\- main
\- java
\- com.package.sdk
\- PackageSDK.java
我希望所有这些都打包在一个 Jar 中,但不透露 PackageSDK.java
中的源代码.
I would like for all of this to be packaged in a Jar, but without revealing the source code present in PackageSDK.java
.
我像这样设置我的 build.gradle
文件:
I set up my build.gradle
file like so:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/../lib'
}
}
}
}
task jar(type: Jar) {
from android.sourceSets.main.allSource
}
当我在项目目录中运行 gradlew clean jar
时,会在 ProjectName\build\libs
中创建一个名为 ProjectName.jar
的 Jar 文件.其结构如下:
When I run gradlew clean jar
in the project's directory, a Jar file is created in ProjectName\build\libs
called ProjectName.jar
. It's structure is as follows:
ProjectName.jar
\- lib
| \- armeabi
| \- libNativeFirst.so
| \- libNativeSecond.so
\- com
\- package
\- sdk
\- PackageSDK.java
我希望在执行 jar
任务时包含已编译的 PackageSDK.class
而不是 PackageSDK.java
文件.我可以改变什么来实现这一目标?
I would like for the compiled PackageSDK.class
to be included instead of the PackageSDK.java
file when executing the jar
task. What can I change to achieve this?
根据 Ben Manes 的建议,我将 sourceSets
的配置更改为以下内容:
Per Ben Manes's suggestion, I changed the configuration of the sourceSets
to the following:
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/../lib'
}
output {
classesDir 'build/classes'
resourcesDir 'build/javaResources'
}
}
}
和jar
任务如下:
task jar(type: Jar) {
from android.sourceSets.main.output
}
Gradle 现在给我这个输出:
Gradle is now giving me this output:
在源集 main 上找不到参数 [build_25r1m0a3etn5cudtt5odlegprd$_run_closure2_closure9_closure10_closure13@138532dc] 的方法 output().
推荐答案
注意: 答案已被编辑.请参阅下面的 07/28/2014 更新.
Note: The answer has been edited. Please see the 07/28/2014 update below.
这是我最终想出的解决方案.可能有更好的方法,但我还没有找到.
Here is a solution I ended up coming up with. There may be a better way available, but I have not found it yet.
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/../lib'
}
}
}
}
task clearJar(type: Delete) {
delete 'build/libs/ProjectName.jar'
}
task makeJar(type: Copy) {
from('build/bundles/release/')
into('build/libs/')
include('classes.jar')
rename ('classes.jar', 'ProjectName.jar')
}
makeJar.dependsOn(clearJar, build)
运行 gradlew makeJar
在 build/libs
目录中创建一个 ProjectName.jar
.这个jar的结构如下:
Running gradlew makeJar
creates a ProjectName.jar
in the build/libs
directory. The structure of this jar is as follows:
ProjectName.jar
\- lib
| \- armeabi
| \- libNativeFirst.so
| \- libNativeSecond.so
\- com
\- package
\- sdk
\- PackageSDK.class
这正是我需要的结果.我现在可以在其他项目中成功使用 ProjectName.jar
.
This is the exact result I needed. I am now able to use ProjectName.jar
successfully in other projects.
虽然我能够在 Android Studio 中的项目中使用生成的 jar,但我无法在 ADT 中创建的项目中这样做,因为关于 jar 文件中存在本机代码的警告.据说有一个标志可以关闭此检查设置,但它无法正常工作.因此,如果要创建使用本机代码的库,使用 ADT 的人必须手动将 armeabi 目录复制到 libs/中.
While I am able to use the resulting jar in projects within Android Studio, I cannot do so in projects created in ADT due to a warning about native code being present inside a jar file. Supposedly there is a flag to turn off this check in settings, but it does not function correctly. Thus, if you want to create a library that uses native code, those using ADT will have to manually copy the armeabi directory into libs/.
从 Android Studio 0.8.0 开始,Gradle 输出目录已更改,上述配置将不起作用.我已将配置更改为以下内容:
As of Android Studio 0.8.0, Gradle output directories have been changed and the configuration outlined above will not work. I have changed my configuration to the following:
task clearJar(type: Delete) {
delete 'build/outputs/ProjectName.jar'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'ProjectName.jar')
}
重要提示:请注意,ProjectName.jar
现在将被放入 build/outputs/
而不是 build/libs/
.
IMPORTANT: Please note that ProjectName.jar
will now be placed into build/outputs/
and NOT into build/libs/
.
这篇关于如何在不公开源代码的情况下使用 gradle 创建 Android Library Jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!