问题创建一个输出jar的自定义gradle项目

时间:2014-01-08 03:49:14

标签: android gradle android-gradle

我目前有一个自定义的build.gradle,它可以生成一个SWIG c ++项目并生成一个jar文件,供我的android项目使用。 (jar是在cmake中生成的)

我目前的设置大部分都有效。 jar文件正确链接到我的android项目,它构建得很好。我唯一的问题是android studio无法识别库。没有自动完成工作,它强调我的库的所有用途都是错误。

c ++ project build.gradle

task cmakeMyLibrary(type: Exec) {
     inputs.file 'CMakeLists.txt'
     outputs.file 'Makefile'

     ext.jarFile = new File(parent.buildDir, "mylibrary.jar")

     commandLine 'cmake' ...
}

task makeMyLibrary(type: Exec, dependsOn: cmakeMyLibrary) {

    inputs.dir '.'

    outputs.file cmakeMyLibrary.jarFile

    commandLine 'make', '-j10'
}

task cleanMyLibrary(type: Exec) {
     commandLine 'make', 'clean'
}

configurations.create('default')
artifacts {
    it."default"(cmakeMyLibrary.jarFile) {
        builtBy makeMyLibrary
    }
}

android project build.grade

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile project(':mylibrary')
}

每次我执行“使用gradle文件同步项目”时,我都会遇到异常:

Gradle 'MyApplication6' project refresh failed:
exception during working with external system: java.lang.AssertionError 
at com.android.tools.idea.gradle.dependency.ModuleDependency.getModuleName(ModuleDependency.java:49) 
at com.android.tools.idea.gradle.dependency.ModuleDependency. (ModuleDependency.java:43) 
at com.android.tools.idea.gradle.dependency.Dependency.populate(Dependency.java:143) 
at com.android.tools.idea.gradle.dependency.Dependency.extractFrom(Dependency.java:106) 
at com.android.tools.idea.gradle.project.AndroidGradleProjectResolver.populateModuleDependencies(AndroidGradleProjectResolver.java:154) 
at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.doResolveProjectInfo(GradleProjectResolver.java:232) 
at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.access$000(GradleProjectResolver.java:60) 
at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver$1.fun(GradleProjectResolver.java:116) 
at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver$1.fun(GradleProjectResolver.java:112) 
at org.jetbrains.plugins.gradle.service.project.GradleExecutionHelper.execute(GradleExecutionHelper.java:190) 
at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.resolveProjectInfo(GradleProjectResolver.java:112) 
at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.resolveProjectInfo(GradleProjectResolver.java:60) 
at com.intellij.openapi.externalSystem.service.remote.RemoteExternalSystemProjectResolverImpl$1.produce(RemoteExternalSystemProjectResolverImpl.java:41) 
at com.intellij.openapi.externalSystem.service.remote.RemoteExternalSystemProjectResolverImpl$1.produce(RemoteExternalSystemProjectResolverImpl.java:37) 
at com.intellij.openapi.externalSystem.service.remote.AbstractRemoteExternalSystemService.execute(AbstractRemoteExternalSystemService.java:59) 
at com.intellij.openapi.externalSystem.service.remote.RemoteExternalSystemProjectResolverImpl.resolveProjectInfo(RemoteExternalSystemProjectResolverImpl.java:37) 
at com.intellij.openapi.externalSystem.service.remote.wrapper.ExternalSystemProjectResolverWrapper.resolveProjectInfo(ExternalSystemProjectResolverWrapper.java:49) 
at com.intellij.openapi.externalSystem.service.internal.ExternalSystemResolveProjectTask.doExecute(ExternalSystemResolveProjectTask.java:53) 
at com.intellij.openapi.externalSystem.service.internal.AbstractExternalSystemTask.execute(AbstractExternalSystemTask.java:130) 
at com.intellij.openapi.externalSystem.service.internal.AbstractExternalSystemTask.execute(AbstractExternalSystemTask.java:120) 
at com.intellij.openapi.externalSystem.util.ExternalSystemUtil$3.execute(ExternalSystemUtil.java:441) 
at com.intellij.openapi.externalSystem.util.ExternalSystemUtil$4$2.run(ExternalSystemUtil.java:504) 
at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:464) 
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178) 
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209) 
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212) 
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171) 
at com.intellij.openapi.progress.impl.ProgressManagerImpl$8.run(ProgressManagerImpl.java:373) 
at com.intellij.openapi.application.impl.ApplicationImpl$8.run(ApplicationImpl.java:435) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at java.lang.Thread.run(Thread.java:744) 
at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:152)

当我查看gradle源时,似乎断言传入getModuleName的路径为空。

2 个答案:

答案 0 :(得分:0)

可能无法回答您的问题,但请确保从18.1.1和0.7。+的版本升级到Build Tools 19.0.1和Android Gradle插件0.8.1(或其他任何最新版本)。过去使用gradle / IntelliJ时出现了一些错误,这些错误是由旧版本引起的。您需要确保Gradle包装器最多为0.10,但IntelliJ应该提示您。

可能相关:https://code.google.com/p/android/issues/detail?id=62391

或许可以发布更多关于您的项目结构...或者因为已经超过一个月,请告诉我们您是否找到了修复...:)

答案 1 :(得分:0)

如果它是纯C ++项目并且其中没有Java代码,您可以将其构建为静态或共享库[包括您想要支持的所有ABI],并让您的项目导入lib吗? Android studio支持使用gradle-experimental插件http://tools.android.com/tech-docs/new-build-system/gradle-experimental或cmake / ndk-build插件http://tools.android.com/tech-docs/external-c-builds

的导入本机lib。

这个回购https://github.com/googlesamples/android-ndk中有一些例子,master分支用于gradle-experimental,master-cmake用于cmake插件。与您的案例类似的特定样本将是hello-libs [出现在两个分支中]。

您将把您的应用程序项目的Java代码调用到其自己的本机[c / c ++]代码中;然后从那些本机代码调用您的SWIG C ++库。换句话说,应用程序项目需要一个包装器,因为它需要处理JNI代码[我假设],而JNI代码在其jni函数名称中有你的java类/包的签名。

我不确定普通的JAVA项目是否可以直接使用第三方库:lib函数名称中没有包和java类名。