V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
9527H
V2EX  ›  Android

android studio 把主模块(app)打成 jar 包,但是运行的时候报 Java .lang.NoClassDefFoundError

  •  
  •   9527H · 2022-10-31 12:30:23 +08:00 · 3954 次点击
    这是一个创建于 514 天前的主题,其中的信息可能已经有所发展或是发生改变。

    android studio 把主模块( app )打成 jar 包,但是运行的时候报 java.lang.NoClassDefFoundError 原因是因为主模块引用子模块中的类,但是打 jar 包的时候,jar 没有把子模块包含进去,请问该怎么解决?

    以下是 app 模块的 build.gradle

    dependencies { //包含的子模块 implementation project(':autojs') implementation project(':common') implementation project(':automator') }

    task makeJar(type: Copy) {

    def jarName = "testJar"
    
    //删除存在的
    delete 'build/libs/' + jarName + ".jar"
    //设置拷贝的文件
    from("build/intermediates/aar_main_jar/commonDebug")
    //打进 jar 包后的文件目录,将 classes.jar 放入 build/libs/目录下
    into('build/libs/')
    //要打包的 jar 文件
    include('classes.jar')
    
    //重命名
    rename('classes.jar', jarName  + ".jar")
    

    } makeJar.dependsOn(build)

    9 条回复    2022-10-31 21:54:27 +08:00
    cheng6563
        1
    cheng6563  
       2022-10-31 14:58:44 +08:00
    我没记错的话 Android 的 jar 包里面是没有.class 文件的只有一个.dex 文件的吧,你看看你自己弄过去的包里面有无.dex
    icetea12138
        2
    icetea12138  
       2022-10-31 15:25:08 +08:00
    老麻烦了,我做过一次,已经忘了怎么做的了
    HongJay
        3
    HongJay  
       2022-10-31 15:44:27 +08:00
    子模块单独打包依赖
    AoEiuV020CN
        4
    AoEiuV020CN  
       2022-10-31 16:17:52 +08:00
    依赖问题相当麻烦,几行代码搞不定,

    我的方案是给所有模块正确的 groupId, artifactId, version,

    然后配置 maven , 调用 publishToMavenLocal ,指定 mavenLocal 的位置,导出所有模块包括依赖树到 mavenLocal ,

    最后在需要依赖这一大坨的项目里指定同样的 mavenLocal 位置,添加 mavenLocal 仓库,
    再依赖一大坨中的主模块,其他子模块依赖就会自己依赖好了,
    9527H
        5
    9527H  
    OP
       2022-10-31 17:28:24 +08:00
    @AoEiuV020CN 好的,谢谢你
    Bown
        6
    Bown  
       2022-10-31 19:11:28 +08:00
    Jooooooooo
        7
    Jooooooooo  
       2022-10-31 19:17:28 +08:00
    fat jar
    closedevice
        8
    closedevice  
       2022-10-31 19:18:30 +08:00
    这种情况要么是需要 flataar ,要么上传到 maven
    unco020511
        9
    unco020511  
       2022-10-31 21:54:27 +08:00
    要么使用传 maven,用远程依赖,要么使用 fat-aar
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3395 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:15 · PVG 19:15 · LAX 04:15 · JFK 07:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.