如何创建一个Android库JAR与摇篮不公开透露来源$ C ​​$ C?摇篮、创建一个、不公开、来源

2023-09-12 02:45:42 作者:二货vs二逼

我想创建一个JAR出一个Android库项目。它是建立在以下方式:

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

我想所有这一切在一个罐子里就可以打包,但没有透露来源$ C ​​$ C present在 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干净的瓶子在项目的目录,JAR文件的项目名创建\建立\库名为 ProjectName.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

我想为编译 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?

每本鬃毛的建议下,我改变配置的 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'
        }
    }
}

任务如下:

task jar(type: Jar) {
    from android.sourceSets.main.output
}

摇篮,现在给我这样的输出:

Gradle is now giving me this output:

找不到方法输出()的参数[build_25r1m0a3etn5cudtt5odlegprd $ _run_closure2_closure9_closure10_closure13 @ 138532dc]源设置为主。

推荐答案

注意:答案进行了编辑。请参阅下面的2014年7月28日更新。

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 创建一个 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.

编辑:虽然我能够使用所产生的罐子的Andr​​oid Studio中的项目,我不能在ADT创建的项目,由于有关本机code是$ P $内的jar文件psent警告这样做。据说有一个标志,以关掉该检查的设置,但它并没有正常工作。因此,如果你想创建一个使用原生code库,那些使用ADT将不得不手动复制armeabi目录复制到库/.

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的工作室0.8.0中,摇篮输出目录已经改变,上面列出的配置将无法正常工作。我已经改变了我的配置如下:

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 现在将被放置到建立/输出/ 和不进建立/库/

IMPORTANT: Please note that ProjectName.jar will now be placed into build/outputs/ and NOT into build/libs/.