How to add a jar in External Libraries in android studio

前端 未结 14 2339
北海茫月
北海茫月 2020-11-22 16:17

I am new to Android Studio. What I need to do is add a few jar files in the External Libraries below the < JDK > folder.

14条回答
  •  被撕碎了的回忆
    2020-11-22 16:51

    The "official way" to add a jar into your android project as an external library, is to add the jar in dependencies { } section in build.gradle.

    If you've done all of the above, and none of the above works, then there are two other possibilities:

    1. If Android Studio or some other IDE doesn't give red underlined errors in editor but runs in error in the Java Compiler, CHECK YOUR JAR. Your jar may not have a proper Manifest included therefore the compiler doesn't know what the jar can provide/don't know it's package name
    2. Maybe it's folder structure is messed up. The folder structure doesn't match with its package name. package a.b.c; should be matching to folder a > folder b > folder c.
    3. The package name has some conflict. (Trust me, this happened to me and it took hours to figure it out.)

    However, if you are going with cordova, here are some tips of adding external jars.

    "build-extras.gradle" is the better way to manage your gradle file.

    Here are the steps to manage extra settings in cordova-based android project:

    1. Adding your jar into build-extras.gradle:
      //Other settings go here, e.g.: buildscript { ... }
      ext.postBuildExtras = {
      // you may have some other settings, e.g.: android { ... }
              dependencies {
                      compile files('libs/abc.jar')
              }
      }
      

    (More detailed steps here: Extending the cordova gradle file to include google services )

    1. In terminal, do:

    cordova build android

提交回复
热议问题