Joh*_*ley 4 android android-studio
引用库的传统设置是 Android Studio将库导入到我们主项目的根目录中。这并不是我们真正想要的,因为库的基本好处是它可以在许多项目之间共享。如果我们更新导入库中的代码,这不会反映在之前导入到另一个项目的库的代码中。
相反,我们的目标是创建一个 Main Android 项目,该项目可以引用存在于 Android 项目目录之外的自定义(您创建的)Android 和 Java 库模块,并且能够从 Main 内部编辑所有三个模块的代码安卓项目。也就是说,我们希望最终得到:
...例如,我们有一个像这样的目录结构...
..\LibraryRefDemo\MyMainProject
..\LibraryRefDemo\Libraries\MyAndroidLibrary
..\LibraryRefDemo\Libraries\MyJavaLibrary
Run Code Online (Sandbox Code Playgroud)
换句话说,标题问题是“我们如何做到这一点?”。
现有类似的 stackoverflow 问题,例如......
但我发现这些问题和答案都只能部分解决我在这里提出的问题。
我将提供我自己的问题的答案,我相信,它列举了所有技巧和陷阱,并提供了一步一步的过程。
我将使用 Android Studio 3.0.1 ...
首先,我们建立命名和目录约定(如果您不喜欢以下约定,请建立您自己的约定)。
\n\n项目名称:PascalCase。
\n\n..\\LibraryRefDemo\\MyMainProject\n..\\LibraryRefDemo\\Libraries\\MyAndroidLibrary\n..\\LibraryRefDemo\\Libraries\\MyJavaLibrary\nRun Code Online (Sandbox Code Playgroud)\n\n创建新项目向导 > 创建 Android 项目:
\n\nApplication Name: MyMainProject
\nProject Location: ..\\MyMainProject
包名:小写。
\n\n// (package name for MyMainProject\'s root module: "app")\nau.com.example.mymainproject\n\n// (package name for MyAndroidLibrary\'s root module: "malmodule")\nau.com.example.malmodule\n\n// (package name for MyJavaLibrary\' module where we placed our \n// java code to be shared: "mjlmodule")\nau.com.example.mjlmodule \nRun Code Online (Sandbox Code Playgroud)\n\n模块名称:小写。以下之一:
\n\n..\\LibraryRefDemo\\MyMainProject\n..\\LibraryRefDemo\\Libraries\\MyAndroidLibrary\n..\\LibraryRefDemo\\Libraries\\MyJavaLibrary\nRun Code Online (Sandbox Code Playgroud)\n\n以下基本技巧和后续分步过程截至 2018 年 3 月 10 日适用于 Android Studio 3.0.1。\n在完成分步过程之前,请先了解完成此操作的基本技巧是:
\n\n在主 Android 项目(“MyMainProject”)中settings.gradle (Project Settings),我们使用类似以下内容引用库模块(而不是库项目本身):
include \':app\'\n\ninclude \':malmodule\'\n// Despite the name "project" we actually need to reference the module directory\n// of a project. It doesn\'t matter whether there is a trailing slash \'/\' or not.\nproject(\':malmodule\').projectDir =\n new File(settingsDir, \'../Libraries/MyAndroidLibrary/malmodule/\')\n\ninclude \':mjlmodule\'\n// Despite the name "project" we actually need to reference the module directory\n// of a project. It doesn\'t matter whether there is a trailing slash \'/\' or not.\nproject(\':mjlmodule\').projectDir =\n new File(settingsDir, \'../Libraries/MyJavaLibrary/mjlmodule/\')\nRun Code Online (Sandbox Code Playgroud)在 Android 项目(“MyMainProject”)根模块的 build.gradle 文件中,默认build.gradle (Module: app)添加:
dependencies {\n ...\n // Reference module rather than project.\n implementation project(\':malmodule\')\n implementation project(\':mjlmodule\')\n ...\n}\nRun Code Online (Sandbox Code Playgroud)分步过程如下。字符串中的正斜杠“/”将代表项目窗格、Android 视图中的路径(从组合框中选择)。字符串中的反斜杠“\\”将代表文件系统中的路径\xe2\x80\xa6
\n\n创建一个 Android 项目(“MyMainProject”):
\n\n向导“创建新项目”> |创建 Android 项目|:
\n\nMyMainProjectexample.com.au..\\LibraryRefDemo\\MyMainProjectau.com.example.mymainproject向导 |目标 Android 设备:选择外形尺寸和最低 SDK|。接受默认值(或根据需要)。[下一个]。
添加一些“hello world”资源和代码。在项目窗格中切换到 Android 视图(从组合框):
\n\nres/layout/activity_main.xml。添加 ID。
\n\n<TextView\n android:id="@+id/mymainproject_output"\n android:layout_width="wrap_content"\nRun Code Online (Sandbox Code Playgroud)res/values/strings.xml。添加字符串资源。
\n\n<string name="mymainproject_cool_string">From My Main Project</string>\nRun Code Online (Sandbox Code Playgroud)au.com.example.mymainproject.MainActivity。引用 java 代码中的字符串资源。
\n\npublic class MainActivity extends Activity {\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n\n TextView textView = (TextView) findViewById(R.id.mymainproject_output);\n String someString = getResources().getString(R.string.mymainproject_cool_string) + "\\r\\n";\n textView.setText(someString); \n }\n} \nRun Code Online (Sandbox Code Playgroud)\n\n要正确导入引用的类,您可能必须: 将光标插入 TextView 上;alt+输入;
针对模拟器运行以验证是否正常:运行 > 运行应用程序 (Shift+F10)...“选择部署目标”>(单击所需的可用虚拟设备)> [确定]。
等待 gradle 完成构建。观察应用程序中的字符串:
\n\nFrom My Main Project\nRun Code Online (Sandbox Code Playgroud)首先我们创建另一个普通的Android项目,然后将其变成一个Android库。
\n\n创建该库作为另一个普通 Android 项目:
\n\n打开{Android Studio},进入“欢迎使用Android Studio”界面>【启动新的Android Studio项目】:
\n\nMyAndroidLibraryexample.com.au..\\LibraryRefDemo\\Libraries\\MyAndroidLibraryau.com.example.malmodule我们使用这个名称而不是默认名称,以便稍后清楚地表明我们直接引用模块而不是项目)。等待 gradle 完成构建。
malmodule添加一些 hello-world 类型代码:
\n\n在 malmodule/res/values/strings.xml 中,添加:
\n\n<string name="myandroidlibrary_cool_string">From My Android Library</string>\nRun Code Online (Sandbox Code Playgroud)../res/layout/activity_main.xml(文本视图)。添加 ID。
\n\n<TextView\n android:id="@+id/myandroidlibrary_output"\n android:layout_width="wrap_content"\nRun Code Online (Sandbox Code Playgroud)在..\\LibraryRefDemo\\Libraries\\MyAndroidLibrary\\malmodule\\src\\main\\java\\au\\com\\example\\malmodule\\MainActivity.java中,添加到onCreate方法(并导入相关类,alt+enter 将光标悬停在 TextView 上):
\n\npublic class MainActivity extends Activity {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n\n TextView myText = (TextView) findViewById(R.id.myandroidlibrary_output);\n String someString = getResources().getString(R.string. myandroidlibrary_cool_string) + "\\r\\n";\n myText.setText(someString); \n }\n} \nRun Code Online (Sandbox Code Playgroud)\n\n要正确导入引用的类,您可能必须: 将光标插入 TextView 上;alt+输入;
从其他地方复制任何现有代码(和资源)。将 android 代码复制到:..\\LibraryRefDemo\\Libraries\\MyAndroidLibrary\\malmodule\\src\\main\\java\\au\\com\\example\\malmodule
验证它可以作为项目运行:运行 > 运行 \'malmodule\'。等待 gradle 完成构建。观察模拟器中的字符串:
\n\nFrom My Android Library\nRun Code Online (Sandbox Code Playgroud)(从上面开始)将项目的模块转换为 Android 库:
\n\n{Android Studio} > 项目面板 > Android 视图 > Gradle 脚本 > build.gradle(模块:malmodule)> 双击(打开)。然后 ...
\n\n// Change ...\napply plugin: \'com.android.application\'\n\n// To ...\napply plugin: \'com.android.library\'\nRun Code Online (Sandbox Code Playgroud)同样在build.gradle(模块:malmodule)中注释掉applicationID。
\n\n...\nandroid {\n compileSdkVersion 26\n defaultConfig {\n// applicationId "au.com.example.malmodule"\n ... \nRun Code Online (Sandbox Code Playgroud)\n\n注释掉而不是删除,以防我们想要将 malmodule 作为普通 Android 项目重新运行以进行测试。\n Android Studio 1.0 和错误“库项目无法设置 applicationId”
工具 > Android > 同步项目与 Gradle 文件(或单击黄色提示栏中的“立即同步”链接)。等待 gradle 构建完成。
要引用您的自定义 Android 库项目(位于主项目目录之外):
\n\n打开 MyMainProject 的settings.gradle (Project Settings)文件。添加:malmodule参考文献如下..
include \':app\'\n\ninclude \':malmodule\'\n// Despite the name "project" we actually need to reference the module directory\n// of a project. It doesn\'t matter whether there is a trailing slash \'/\' or not.\nproject(\':malmodule\').projectDir =\n new File(settingsDir, \'../Libraries/MyAndroidLibrary/malmodule/\')\nRun Code Online (Sandbox Code Playgroud)打开 MyMainProject 的根模块的 build.gradle 文件(即build.gradle (Module: app))。然后添加 ...
dependencies {\n ...\n // Reference module rather than project.\n implementation project(\':malmodule\')\n ...\n}\nRun Code Online (Sandbox Code Playgroud)工具 > Android > 同步项目与 Gradle 文件(或单击黄色提示栏中的“立即同步”链接)。
验证您可以引用库中的字符串资源:
\n\n检查 ..malmodule/res/values/strings.xml 中的以下字符串...
\n\n<string name="myandroidlibrary_cool_string">From My Android Library</string>\nRun Code Online (Sandbox Code Playgroud)检查 MyMainProject app/res/layout/activity_main.xml ...
\n\n<TextView\n android:id="@+id/mymainproject_output"\n android:layout_width="wrap_content"\n...\nRun Code Online (Sandbox Code Playgroud)在主项目 app/java/au.com.example.mymainproject/MainActivity 中添加对 myandroidlibrary_cool_string 的引用。
\n\npublic class MainActivity extends Actvity {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n\n TextView myText = (TextView) findViewById(R.id.mymainproject_output);\n String someString = getResources().getString(R.string.mymainproject_cool_string) + "\\r\\n";\n someString += getResources().getString(au.com.example.malmodule.R.string.myandroidlibrary_cool_string) + "\\r\\n";\n myText.setText(someString); \n\n }\n}\nRun Code Online (Sandbox Code Playgroud)从您的库清单中注释掉 MAIN/LAUNCHER 意图。否则,您的应用程序的两个图标将出现在应用程序抽屉中。
\n\n<activity android:name=".MainActivity">\n <!-- Don\'t make a main/launcher activity when being called by main app -->\n <!--<intent-filter>-->\n <!--<action android:name="android.intent.action.MAIN" />-->\n\n <!--<category android:name="android.intent.category.LAUNCHER" />-->\n <!--</intent-filter>-->\n</activity>\nRun Code Online (Sandbox Code Playgroud)运行您的主项目。运行 > 运行应用程序 (Shift+F10)。等待 gradle 构建并等待模拟器加载您的应用程序。观察来自您的库的字符串反映在您的模拟器中:
\n\nFrom My Main Project\nFrom My Anroid Library\nRun Code Online (Sandbox Code Playgroud)更改 ..malmodule/res/values/strings.xml 中的字符串
\n\n<string name="myandroidlibrary_cool_string">My Android Library XXX</string>\nRun Code Online (Sandbox Code Playgroud)运行 > 应用更改 (Ctrl+F10)。等待 gradle 构建并等待模拟器重新加载您的应用程序。观察更改后的字符串是否反映在您的模拟器中。
(Google,nd Gradle 插件用户指南) http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-projects \n(Goolge Android 工具,2014 年。开发 > 工具)https://developer.android.com/studio/projects/android-library.html
使用 Java 库模块创建项目...
\n\n首先创建一个带有 Android 模块的普通 Android 项目:
\n\n向导“创建新项目”
\n\n|创建Android项目|
\n\nMyJavaLibraryexample.com.au\\LibraryRefDemo\\Libraries\\MyJavaLibraryau.com.example.myjavalibrary这将是根模块的命名空间,我们的 java 代码将位于我们随后创建的另一个模块和命名空间中)。[下一个]|目标 Android 设备|。接受默认值(或根据需要)。[下一个]
打开项目窗格。从组合框中选择“Android”视图。
创建 Java 库模块:
\n\n向导“创建新模块”。
\n\n|Java 库| “配置您的新模块”。
\n\nmjlmoduleau.com.example.mjlmoduleStart不要删除 MyJavaLibrary 项目的根模块 app. 如果您这样做,默认情况下,您的 mjlmodule 将成功运行一次,但后续的代码更新将不会合并到第二次及后续运行中。
\n\n可能有一种方法可以正确整理 gradle 文件,但我的 gradle 知识有限。
\n\n在您的 Java 库模块中添加一些代码:
\n\n在新创建的 Java 类 Start 中,添加一个 main 方法。确保您的 main 方法具有正确的签名(特别是包括“String[] args”)。例如,使用以下类来实现 hello world 目的。mjlmodule/java/au.com.example.mjlmodule/开始 ...
\n\npackage au.com.example.mjlmodule;\n\npublic class Start {\n public static void main(String[] args) {\n System.out.println(getCoolString());\n }\n\n // Must be public because we want to reference the method directly later\n public static String getCoolString() {\n return "From My Java Library";\n }\n} \nRun Code Online (Sandbox Code Playgroud)项目窗格 > Android 视图(来自组合框) > Gradle 脚本 > build.gradle (Module: mjlmodule):
验证应用插件集属性:
\n\napply plugin: \'java-library\'\nRun Code Online (Sandbox Code Playgroud)验证依赖关系如下:
\n\ndependencies {\n implementation fileTree(dir: \'libs\', include: [\'*.jar\'])\n}\n\n// You can delete or comment out ...\n//sourceCompatibility = "1.7"\n//targetCompatibility = "1.7"\nRun Code Online (Sandbox Code Playgroud)使用独立安装的JDK,而不是Android的嵌入式JDK。
\n\n文件 > 项目结构 ... > |SDK 位置| > JDK 位置:
\n\n工具 > Android > 同步项目与 Gradle 文件(或单击黄色提示栏中的“立即同步”链接)。
运行 > 编辑配置 \xe2\x80\xa6
\n\nMyJavaRunau.com.example.mjlmodule.Start\\LibraryRefDemo\\Libraries\\MyJavaLibrarymjlmoduleDefault 验证模块可以运行...
\n\n(在 gradle 构建之后)在“运行”窗格中观察字符串输出:
\n\nFrom My Java Library\nRun Code Online (Sandbox Code Playgroud)从其他地方复制 java 代码文件(如果有的话):
\n\n将 java 代码复制到:..\\LibraryRefDemo\\Libraries\\MyJavaLibrary\\mjlmodule\\src\\main\\java\\au\\com\\example\\mjlmodule。\n您可能必须更改包名称复制的代码。
在 Android Studio 的“项目窗格”中,观察这些文件的显示(您可能需要等待刷新事件。如果需要强制刷新,请重新启动 {Android Studio})。
再次测试运行配置OK:
\n\n对您的代码进行一些更改,例如:
\n\npublic class Start {\n public static void main(String[] args) {\n System.out.println(getCoolString());\n }\n\n // Must be public because we want to reference the method directly later\n public static String getCoolString() {\n return "From My Java Library YYY";\n }\n}\nRun Code Online (Sandbox Code Playgroud)在运行三角形附近的工具栏上,确保选择“MyJavaRun”配置。单击运行绿色三角形。
观察“运行”窗格中出现的更新后的字符串。
\n\nFrom My Java Library YYY\nRun Code Online (Sandbox Code Playgroud)请注意,截至 2018 年 3 月 3 日,由于不支持最新的 gradle 插件,Intellij IDEA 2017.3 现在无法运行 MyJavaLibrary。我预计这将从 IDEA 2018.1 开始改变。
\n\n看 ...
\n\n\n\n\n有时在 3.0-alpha2 和插件的最终 3.0 版本之间它会停止工作。
\n\ngradle 插件导出到 Studio/IJ 以设置项目的模型正在发生突破性的变化。现在,它以旧方式和新方式发送信息,尽管前者并不总是准确,具体取决于构建设置。这很快就会改变,这将破坏 IJ,直到他们可以捆绑 Studio 插件 3.0。
\n
https://www.reddit.com/r/androiddev/comments/6c71av/using_android_gradle_plugin_300alpha1_with/dhssvlk/ \n无法将 Android Gradle Plugin 3.0.+ 与 IntelliJ IDEA 一起使用
\n\n从 MyMainProject 引用 MyJavaLibrary 模块(“mjlmodule”):
\n\n{Android Studio} > 文件 > 打开最近的文件 > [MyMainProject]。在[此窗口]中打开。
在 Android 项目(“MyMainProject”)中settings.gradle (Project Settings)添加 mjlmodule 信息:
include \':app\'\n\ninclude \':malmodule\'\n// Despite the name "project" we actually need to reference the module directory\n// of a project. It doesn\'t matter whether there is a trailing slash \'/\' or not.\nproject(\':malmodule\').projectDir =\n new File(settingsDir, \'../Libraries/MyAndroidLibrary/malmodule/\')\n\ninclude \':mjlmodule\'\n// Despite the name "project" we actually need to reference the module directory\n// of a project. It doesn\'t matter whether there is a trailing slash \'/\' or not.\nproject(\':mjlmodule\').projectDir =\n new File(settingsDir, \'../Libraries/MyJavaLibrary/mjlmodule/\') \nRun Code Online (Sandbox Code Playgroud)在 MyMainProject 的根模块中build.gradle (Module:app),添加 mjmodule 信息:
dependencies {\n implementation project(\':malmodule\')\n implementation project(\':mjlmodule\')\n...\n}\nRun Code Online (Sandbox Code Playgroud)工具 > Android > 同步项目与 Gradle 文件(或单击黄色提示栏“立即同步”)。
验证模块可以作为 Java 应用程序运行...
\n\n验证您可以从 MyMainProject 引用 MyJavaLibrary 中的字符串资源:
\n\n在 ..LibraryRefDemo\\Libraries\\MyJavaLibrary\\mjlmodule\\src\\main\\java\\au\\com\\example\\mjlmodule\\Start.java 中验证
\n\npackage au.com.example.mjlmodule;\n\npublic class Start {\n public static void main(String[] args) {\n System.out.println(getCoolString());\n }\n\n // Must be public because we want to reference the method directly later\n public static String getCoolString() {\n return "From MyJavaLibrary YYY";\n }\n} \nRun Code Online (Sandbox Code Playgroud)在 MyMainProject app/res/layout/activity_main.xml 中验证
\n\n<TextView\n android:id="@+id/mymainproject_output"\n android:layout_width="wrap_content"\n...\nRun Code Online (Sandbox Code Playgroud)在 MyMainProject 中添加一些代码以从 MyJavaLibary 中获取代码。在 MyMainProject/app/java/au.com.example.mymainproject/MainActivity 中添加对 mjlmodule 字符串的引用...
\n\npublic class MainActivity extends Activity {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n\n TextView textView = (TextView) findViewById(R.id.mymainproject_output);\n String someString = getResources().getString(R.string.mymainproject_cool_string) + "\\r\\n";\n someString += getResources().getString(au.com.example.malmodule.R.string.myandroidlibrary_cool_string) + "\\r\\n";\n someString += au.com.example.mjlmodule.Start.getCoolString() + "\\r\\n"; \n myText.setText(someString);\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)运行我的主项目。在工具栏上选择“应用程序”配置。单击绿色箭头。\n如果出现错误“无法安装”,则“构建”>“清理 MyMainProject”。
观察来自您的库的字符串是否反映在您的模拟器(或连接的设备)中。
\n\nFrom MyMainProject\nFrom My Android Library XXX\nFrom My Java Li
| 归档时间: |
|
| 查看次数: |
2301 次 |
| 最近记录: |