0

我最近在编译我的 Java 源代码以生成 C++ 头文件时遇到了一个错误:为某些文件javah吐出错误。package does not exist

tl;dr: javah适用于 Java 源代码,但不适用于另一个源 – 哪些 import 子句非常接近;gradle不处理某些库,但即使处理过的包也被标记为丢失。


我不知道为什么 javah 开始吐出这个错误,因为它直到最近才运行良好,而且我没有改变任何特别的东西。我正在使用gradleAndroid Studio 的gradle-wrapper. 我最近将包装器的发行版从 2.2.1 更新到 2.10,但我不认为这就是 javah 现在失败的原因。

根据日志和当前文件,javah 在编译时找不到我正在使用的库中的包,ClassB但没有找到ClassA.

让我们举两个例子:com.batch.androidcom.adjust.sdk

  • com.batch.android确实由 gradle 处理,因此它的 jar位于我的build/intermediates/exploded-aar文件夹中(在 com.batch.android/batch-sdk/1.5.3/jars/classes.jar 和 com.batch.android/batch-sdk/1.5.3/jars 下/libs/batch.jar)
  • com.adjust.sdk没有被 gradle 处理,所以它的 jar不在我的 build/intermediates/exploded-aar 中
  • 两者都在我的 ~/.gradle/cache/ 文件夹中
  • 两个包都因 javah 丢失而失败
  • 两个包都不是从java 文件ClassA中导入的ClassB

谢谢你的帮助!


gradle 日志(对不起,这太长了):

Executing tasks: [:app:clean, :app:generateDevAmazonDebugSources, :app:generateDevAmazonDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDevAmazonDebugUnitTestDependencies,     :app:assembleDevAmazonDebug]

Configuration on demand is an incubating feature.
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
Generating assets binaries
Incremental java compilation is an incubating feature.
Cleaning project...
:app:clean
:app:processAssets
:app:copyFiles
:app:preBuild
:app:preDevAmazonDebugBuild
:app:checkDevAmazonDebugManifest
:app:preDevAmazonReleaseBuild
:app:preDevGoogleDebugBuild
:app:preDevGoogleReleaseBuild
:app:preProdAmazonDebugBuild
:app:preProdAmazonReleaseBuild
:app:preProdGoogleDebugBuild
:app:preProdGoogleReleaseBuild
:app:prepareComAndroidSupportAppcompatV72311Library
:app:prepareComAndroidSupportCardviewV72320Library
:app:prepareComAndroidSupportMediarouterV72220Library
:app:prepareComAndroidSupportMultidex100Library
:app:prepareComAndroidSupportRecyclerviewV72311Library
:app:prepareComAndroidSupportSupportV42320Library
:app:prepareComBatchAndroidBatchSdk153Library
:app:prepareComCrashlyticsSdkAndroidAnswers136Library
:app:prepareComCrashlyticsSdkAndroidBeta114Library
:app:prepareComCrashlyticsSdkAndroidCrashlytics255Library
:app:prepareComCrashlyticsSdkAndroidCrashlyticsCore238Library
:app:prepareComCrashlyticsSdkAndroidCrashlyticsNdk112Library
:app:prepareComFacebookAndroidFacebookAndroidSdk4101Library
:app:prepareComGoogleAndroidExoplayerExoplayerR142Library
:app:prepareComGoogleAndroidGmsPlayServices780Library
:app:prepareComGoogleAndroidGmsPlayServicesAds780Library
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics780Library
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing780Library
:app:prepareComGoogleAndroidGmsPlayServicesAppinvite780Library
:app:prepareComGoogleAndroidGmsPlayServicesAppstate780Library
:app:prepareComGoogleAndroidGmsPlayServicesBase780Library
:app:prepareComGoogleAndroidGmsPlayServicesCast780Library
:app:prepareComGoogleAndroidGmsPlayServicesDrive780Library
:app:prepareComGoogleAndroidGmsPlayServicesFitness780Library
:app:prepareComGoogleAndroidGmsPlayServicesGames780Library
:app:prepareComGoogleAndroidGmsPlayServicesGcm780Library
:app:prepareComGoogleAndroidGmsPlayServicesIdentity780Library
:app:prepareComGoogleAndroidGmsPlayServicesLocation780Library
:app:prepareComGoogleAndroidGmsPlayServicesMaps780Library
:app:prepareComGoogleAndroidGmsPlayServicesNearby780Library
:app:prepareComGoogleAndroidGmsPlayServicesPanorama780Library
:app:prepareComGoogleAndroidGmsPlayServicesPlus780Library
:app:prepareComGoogleAndroidGmsPlayServicesSafetynet780Library
:app:prepareComGoogleAndroidGmsPlayServicesVision780Library
:app:prepareComGoogleAndroidGmsPlayServicesWallet780Library
:app:prepareComGoogleAndroidGmsPlayServicesWearable780Library
:app:prepareComMopubMopubSdk440Library
:app:prepareComZendeskBelvedere1011Library
:app:prepareComZendeskSdk1512Library
:app:prepareComZendeskSdkProviders1512Library
:app:prepareIoBranchSdkAndroidLibrary1112Library
:app:prepareIoFabricSdkAndroidFabric1310Library
:app:prepareDevAmazonDebugDependencies
:app:compileDevAmazonDebugAidl
:app:compileDevAmazonDebugRenderscript
:app:generateDevAmazonDebugBuildConfig
:app:generateDevAmazonDebugAssets UP-TO-DATE
:app:mergeDevAmazonDebugAssets
:app:processDevAmazonDebugManifest
:app:fabricGenerateResourcesDevAmazonDebug
:app:generateDevAmazonDebugResValues UP-TO-DATE
:app:processDevAmazonDebugGoogleServices
:app:generateDevAmazonDebugResources
:app:mergeDevAmazonDebugResources
:app:processDevAmazonDebugResources
:app:generateDevAmazonDebugSources
:app:preDevAmazonDebugAndroidTestBuild
:app:prepareDevAmazonDebugAndroidTestDependencies
:app:compileDevAmazonDebugAndroidTestAidl
:app:processDevAmazonDebugAndroidTestManifest
:app:compileDevAmazonDebugAndroidTestRenderscript
:app:generateDevAmazonDebugAndroidTestBuildConfig
:app:generateDevAmazonDebugAndroidTestAssets UP-TO-DATE
:app:mergeDevAmazonDebugAndroidTestAssets
:app:generateDevAmazonDebugAndroidTestResValues UP-TO-DATE
:app:generateDevAmazonDebugAndroidTestResources
:app:mergeDevAmazonDebugAndroidTestResources
:app:processDevAmazonDebugAndroidTestResources
:app:generateDevAmazonDebugAndroidTestSources
:app:mockableAndroidJar UP-TO-DATE
:app:preDevAmazonDebugUnitTestBuild
:app:prepareDevAmazonDebugUnitTestDependencies
:app:javahBuild_ClassA
:app:javahBuild_ClassB
Error: package com.adjust.sdk does not exist
Error: package com.adjust.sdk does not exist
Error: package com.amazon.ags.api does not exist
Error: package com.batch.android does not exist
Error: package com.batch.android does not exist
Error: package com.crashlytics.android does not exist
Error: package com.crashlytics.android.answers does not exist
Error: package com.crashlytics.android.answers does not exist
Error: package com.crashlytics.android.answers does not exist
Error: package com.mopub.common does not exist
Error: package com.zendesk.sdk.feedback.impl does not exist
Error: package com.zendesk.sdk.model.access does not exist
Error: package com.zendesk.sdk.model.access does not exist
Error: package com.zendesk.sdk.model.push does not exist
Error: package com.zendesk.sdk.network.impl does not exist
Error: package com.zendesk.sdk.network.impl does not exist
Error: package com.zendesk.sdk.network.impl does not exist
Error: package com.zendesk.sdk.network.impl does not exist
Error: package com.zendesk.sdk.network.impl does not exist
Error: package com.zendesk.sdk.requests does not exist
Error: package com.zendesk.sdk.storage does not exist
Error: package com.zendesk.sdk.support does not exist
Error: package com.zendesk.service does not exist
Error: package com.zendesk.service does not exist
Error: package org.joda.time does not exist
Error: package org.joda.time does not exist
Error: package org.joda.time does not exist
Error: package org.joda.time does not exist
Error: package org.joda.time.format does not exist
Error: package org.joda.time.format does not exist
Error: package io.branch.indexing does not exist
Error: package io.branch.referral does not exist
Error: package io.branch.referral does not exist
Error: package io.branch.referral does not exist
Error: package io.branch.referral.util does not exist
Error: package io.branch.referral.util does not exist
Error: package com.adjust.sdk does not exist
Error: package com.amazon.ags.api does not exist
Error: package com.amazon.ags.api does not exist
Error: package com.amazon.ags.api does not exist
Error: package com.amazon.ags.api does not exist
Error: package com.applovin.sdk does not exist
Error: package com.batch.android does not exist
Error: package com.crashlytics.android does not exist
Error: package com.crashlytics.android.ndk does not exist
Error: package io.branch.referral does not exist
Error: package io.branch.referral does not exist
Error: package io.fabric.sdk.android does not exist
Error: package com.google.android.gms.analytics does not exist
Error: package com.google.android.gms.analytics does not exist
Error: package com.google.android.gms.analytics does not exist
Error: package com.google.android.gms.ads.identifier does not exist
Error: package com.google.android.gms.common does not exist
Error: package com.mopub.common does not exist
Error: package com.mopub.common does not exist
Error: cannot find symbol
  symbol:   class MoPubErrorCode
  location: package com.mopub.mobileads
Error: cannot find symbol
  symbol:   class MoPubRewardedVideoListener
  location: package com.mopub.mobileads
Error: package com.chartboost.sdk does not exist
Error: package com.adjust.sdk does not exist
Error: package com.adjust.sdk does not exist
Error: package com.adjust.sdk does not exist
Error: package com.adjust.sdk does not exist
Error: cannot find symbol
  symbol:   class BaseZendeskFeedbackConfiguration
  location: class my.super.project.Renderer
Error: cannot find symbol
  symbol:   class BuildConfig
  location: package my.super.project
Error: cannot find symbol
  symbol:   class Tracker
  location: class my.super.project.Main
Error: ';' expected
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook does not exist
Error: package com.facebook.appevents does not exist
Error: package com.facebook.login does not exist
Error: package com.facebook.login does not exist
Error: package com.facebook.share.model does not exist
Error: package com.facebook.share.model does not exist
Error: package com.facebook.share.model does not exist
Error: package com.facebook.share.widget does not exist
Error: package com.facebook.share.widget does not exist
Error: package com.facebook.share.widget does not exist
Error: package twitter4j does not exist
Error: package twitter4j does not exist
Error: package twitter4j does not exist
Error: package twitter4j does not exist
Error: package twitter4j does not exist
Error: package twitter4j does not exist
Error: package twitter4j does not exist
Error: package twitter4j does not exist
Error: package twitter4j.auth does not exist
Error: cannot find symbol
  symbol:   class R
  location: package my.super.project
Error: cannot find symbol
  symbol:   class AmazonGamesClient
  location: class my.super.project.Main
Error: cannot find symbol
  symbol:   class MoPubRewardedVideoListener
  location: class my.super.project.Main
Error: cannot find symbol
  symbol:   class ChartBoostDelegate
  location: class my.super.project.Main
Error: cannot find symbol
  symbol:   class CallbackManager
  location: class my.super.project.ClassA
Error: cannot find symbol
  symbol:   class ProfileTracker
  location: class my.super.project.ClassA

 FAILED

FAILURE: Build failed with an exception.

build.gradle有以下任务:

android {
    <snip>
    ['Main', 'Renderer', 'ClassA', 'ClassB'].each {
        def targetName ->
            tasks.create(name: "javahBuild_$targetName", type: Exec) {
                Properties properties = new Properties()
                properties.load(project.rootProject.file('local.properties').newDataInputStream())
                String sdkDir = properties.getProperty('sdk.dir')
                String flavorFolder = getCurrentFlavorFolder()
                commandLine 'javah', '-classpath', "libs/:src/main/java/:$sdkDir/platforms/android-23/android.jar:build/intermediates/classes/$flavorFolder/$config:$sdkDir/platforms/android-23/optional/org.apache.http.legacy.jar/:$config/", '-d', 'src/main/jni/Main/', "my.super.project.${targetName}"
            }
    }

    task javahBuildAll(dependsOn: tasks.matching { Task task -> task.name.startsWith("javahBuild_") })

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn javahBuildAll
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')

    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
    compile('com.crashlytics.sdk.android:crashlytics-ndk:1.1.2@aar') {
        transitive = true
    }
    compile('com.mopub:mopub-sdk:4.4.0@aar') {
        transitive = true
    }

    compile group: 'com.zendesk', name: 'sdk', version: '1.5.1.2'

    compile('com.android.support:multidex:1.0.0')
    compile('com.google.android.gms:play-services:7.8.0')
    compile('com.google.android.gms:play-services-analytics:7.8.0')
    compile('com.google.android.gms:play-services-gcm:7.8.0')
    compile('com.android.support:appcompat-v7:23.1.0')
    compile('com.android.support:support-v4:23.1.0')
    compile('com.facebook.android:facebook-android-sdk:4.10.+')
    compile('io.branch.sdk.android:library:1.+')
    compile('com.batch.android:batch-sdk:1.5+')
    compile('com.adjust.sdk:adjust-android:4.2.3')
    compile('joda-time:joda-time:2.9.2')
}
4

2 回答 2

0

好的,它不明白为什么我会收到这些错误,但我刚刚意识到我可以忽略它们,因为 C++ 头文件仍在生成中。

所以我决定修改我的javahgradle 任务以忽略错误并将它们写入专用日志文件。我添加了以下行:

errorOutput = project.file("build/javah_error_${targetName}.log").newDataOutputStream()
ignoreExitValue = true

我的javah任务现在看起来像这样:

['Main', 'Renderer', 'ClassA', 'ClassB'].each {
    def targetName ->
        tasks.create(name: "javahBuild_$targetName", type: Exec) {
            Properties properties = new Properties()
            properties.load(project.rootProject.file('local.properties').newDataInputStream())
            String sdkDir = properties.getProperty('sdk.dir')
            String flavorFolder = getCurrentFlavorFolder()
            errorOutput = project.file("build/javah_error_${targetName}.log").newDataOutputStream()
            ignoreExitValue = true
            commandLine 'javah', '-classpath', "libs/:src/main/java/:$sdkDir/platforms/android-23/android.jar:build/intermediates/classes/$flavorFolder/$config:$sdkDir/platforms/android-23/optional/org.apache.http.legacy.jar/:$config/", '-d', 'src/main/jni/Main/', "my.super.project.${targetName}"
        }
}
于 2016-04-24T12:27:30.370 回答
0

经过几个小时的搜索、研究、反复试验和其他事情,我终于明白了。

javah没了,现在必须用javac […] -h <destdir> […]

我已经在我的顶层build.gradle(不是下面的那个app/!)中有以下片段来添加javac参数......</p>

tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:all"
}

……而且“只是”必须扩展它。(这部分进入allprojects {你已经在顶层的块build.gradle,我把它放在repositories { […] }块之后。)

在我的例子中,我只想让.h文件被创建,而不是#include在 C 代码中主动创建它们(因为我使用RegisterNatives方法来注册我的函数),这将是……棘手,因为除了这之外还必须:

  • 找出哪个(可能是多个)任务应该创建.h实际构建中使用的文件
  • 一个稳定的路径名
  • 将该路径添加到本机构建中的包含目录(再次棘手)

对于单纯的手动自省,我只是将javac -h <destdir>选项添加到所有 javac调用中,创建每个任务的结果目录,然后在 Midnight Commander 中查看它们:

tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:all"
    options.getHeaderOutputDirectory().value(project.layout.buildDirectory.dir("headers/" + getName()))
}

compilerArgs当然,更改是从上面复制的。)

于 2020-11-22T21:05:12.993 回答