简体   繁体   English

构建apk后找不到Java静态接口方法

[英]Java static interface method not found after build apk

I have a doubt about the use of static methods on interfaces.我对在接口上使用静态方法有疑问。

In my project, there are an application module and two android lib modules,在我的项目中,有一个应用模块和两个android lib模块,

their dependencies: app -> engine -> library它们的依赖项: app -> engine -> library

In 「library」 module,A piece of code seems like:在「库」模块中,一段代码看起来像:

public interface Base {

    void test();

    static Base create(){
        return new BaseImpl();
    }
}

public class BaseImpl implements Base {
    @Override
    public void test() {
        System.out.println("Library Base test");
    }
}

In 「engine」module,在「引擎」模块中,

public class Engine {

    public static void init(){
        Base.create().test();
    }
}

In application module,在应用模块中,

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        findViewById(R.id.btn_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Engine.init();
            }
        });
    }
}

app module build.gradle :应用程序模块build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }

}


repositories {
    flatDir {
        dirs 'libs'   // aar dir
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

   // implementation project(':engine')
    implementation name: "engine-release", ext: "aar"   //「engine」 lib aar
    implementation name: "library-release", ext: "aar"  //「library」 lib aar
}

I complie 「engine」and 「library」to aar.我将「引擎」和「库」编译到 aar。 and add them to the dependencies of app.并将它们添加到应用程序的依赖项中。

use Android Studio Build -> Build Apk(s) to build apk.使用 Android Studio Build -> Build Apk(s)来构建 apk。 Build process don't report any error,构建过程不报任何错误,

but I install apk and run the application, it error:但我安装 apk 并运行应用程序,它错误:

java.lang.NoSuchMethodError: No static method create()Lcom/example/library/Base; in class Lcom/example/library/Base; or its super classes (declaration of 'com.example.library.Base' appears in /data/app/com.example.test-Ppe6YDrBRQEbvwbWKx0Ppg==/base.apk)

I decompile the apk file,found:我反编译了apk文件,发现:

public interface Base {
    void test();

    /* renamed from: com.example.library.Base$-CC  reason: invalid class name */
    public final /* synthetic */ class CC {
        public static Base create() {
            return new BaseImpl();
        }
    }
}

public class Engine {
    public static void init() {
        Base.create().test();
    }
}

I don't kown what happen when compile about this situation.我不知道编译这种情况时会发生什么。

example source: https://github.com/KorionCN/StaticInterfaceMethodError示例来源: https ://github.com/KorionCN/StaticInterfaceMethodError

您可能会启用 multiDex 因为您的最低 SDK 版本低于 21(棒棒糖)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM