Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help in Using Java or Kotlin source files as plug-ins

Discussion in 'Android' started by lntoan123, Jun 16, 2021.

  1. lntoan123

    lntoan123

    Joined:
    Apr 1, 2019
    Posts:
    15
    Hi All!
    I am working with project which need use source java file as plugin for android, I followed like this document:
    https://docs.unity3d.com/Manual/AndroidJavaSourcePlugins.html

    When I tested with empty project: copy empty activity from android project to unity, I got this error when build android:

    D:\Programing\All about Unity\PROJECT\TaggleCo\PROJECT\taggle-ihealth-project\Ref\unity-project-test-androidLib\unity-test-javaLib\Temp\gradleOut\unityLibrary\src\main\java\com\taggle\testlib2\MainActivity.java:3: error: package androidx.appcompat.app does not exist
    import androidx.appcompat.app.AppCompatActivity;
    ^
    D:\Programing\All about Unity\PROJECT\TaggleCo\PROJECT\taggle-ihealth-project\Ref\unity-project-test-androidLib\unity-test-javaLib\Temp\gradleOut\unityLibrary\src\main\java\com\taggle\testlib2\MainActivity.java:7: error: cannot find symbol
    public class MainActivity extends AppCompatActivity {
    ^
    symbol: class AppCompatActivity
    D:\Programing\All about Unity\PROJECT\TaggleCo\PROJECT\taggle-ihealth-project\Ref\unity-project-test-androidLib\unity-test-javaLib\Temp\gradleOut\unityLibrary\src\main\java\com\taggle\testlib2\MainActivity.java:9: error: method does not override or implement a method from a supertype
    @override
    ^
    D:\Programing\All about Unity\PROJECT\TaggleCo\PROJECT\taggle-ihealth-project\Ref\unity-project-test-androidLib\unity-test-javaLib\Temp\gradleOut\unityLibrary\src\main\java\com\taggle\testlib2\MainActivity.java:11: error: cannot find symbol
    super.onCreate(savedInstanceState);
    ^
    symbol: variable super
    location: class MainActivity
    D:\Programing\All about Unity\PROJECT\TaggleCo\PROJECT\taggle-ihealth-project\Ref\unity-project-test-androidLib\unity-test-javaLib\Temp\gradleOut\unityLibrary\src\main\java\com\taggle\testlib2\MainActivity.java:12: error: package R does not exist
    setContentView(R.layout.activity_main);
    ^
    Note: D:\Programing\All about Unity\PROJECT\TaggleCo\PROJECT\taggle-ihealth-project\Ref\unity-project-test-androidLib\unity-test-javaLib\Temp\gradleOut\unityLibrary\src\main\java\com\unity3d\player\UnityPlayerActivity.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    5 errors
    FAILURE: Build failed with an exception.
    * What went wrong:
    Execution failed for task ':unityLibrary:compileReleaseJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    * Get more help at https://help.gradle.org
    BUILD FAILED in 1s
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
    UnityEngine.GUIUtility:processEvent (int,intptr)


    upload_2021-6-16_15-13-41.png
    So I don't know exactly which library need integrating to unity? Anyone can help me solve this issue?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,653
    You probably need to change your target sdk or include additional libraries in gradle files.
    The easiest way to figure this out is to start with project that builds fine, export Android Studio project and create Java file there and figure out if something needs to be added to gradle file.
     
  3. lntoan123

    lntoan123

    Joined:
    Apr 1, 2019
    Posts:
    15
    Many thanks for your reply!
    Do I have to copy gradle file from android project to unity project? I also tried to do that, but it's still error same as above.

    I just use empty android project to test, gradle file I copied to unity :

    plugins {
    id 'com.android.application'
    }

    android {
    compileSdkVersion 29
    buildToolsVersion "30.0.3"

    defaultConfig {
    applicationId "com.taggle.testlib2"
    minSdkVersion 21
    targetSdkVersion 29
    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 JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    }
    }

    dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    }
     

    Attached Files:

  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,653
    If you need to modify Gradle files, you have to put them into Unity project as well. Check Player Settings, we have fields there for specifying gradle templates. Also have a look at our manual.