9527H
V2EX  ›  Android

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

  •  
  •   9527H · Oct 31, 2022 · 12990 views
    This topic created in 1300 days ago, the information mentioned may be changed or developed.

    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 replies    2022-10-31 21:54:27 +08:00
    cheng6563
        1
    cheng6563  
       Oct 31, 2022
    我没记错的话 Android 的 jar 包里面是没有.class 文件的只有一个.dex 文件的吧,你看看你自己弄过去的包里面有无.dex
    icetea66
        2
    icetea66  
       Oct 31, 2022
    老麻烦了,我做过一次,已经忘了怎么做的了
    HongJay
        3
    HongJay  
       Oct 31, 2022
    子模块单独打包依赖
    AoEiuV020CN
        4
    AoEiuV020CN  
       Oct 31, 2022
    依赖问题相当麻烦,几行代码搞不定,

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

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

    最后在需要依赖这一大坨的项目里指定同样的 mavenLocal 位置,添加 mavenLocal 仓库,
    再依赖一大坨中的主模块,其他子模块依赖就会自己依赖好了,
    9527H
        5
    9527H  
    OP
       Oct 31, 2022
    @AoEiuV020CN 好的,谢谢你
    Bown
        6
    Bown  
       Oct 31, 2022
    Jooooooooo
        7
    Jooooooooo  
       Oct 31, 2022
    fat jar
    closedevice
        8
    closedevice  
       Oct 31, 2022
    这种情况要么是需要 flataar ,要么上传到 maven
    unco020511
        9
    unco020511  
       Oct 31, 2022
    要么使用传 maven,用远程依赖,要么使用 fat-aar
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   969 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 23:01 · PVG 07:01 · LAX 16:01 · JFK 19:01
    ♥ Do have faith in what you're doing.